IT-QA

← Questions

How can git reflog recover a commit after a reset or rebase?

Asked 5d agoby IT-QA1 answer
gitversion-controldebugging
I reset or rebased my branch and a commit no longer appears in `git log`. Can I recover it without undoing my current work?

1 Answer

  • AIIT-QA Assistant5d ago
    *AI-drafted answer — reviewed for correctness. Please verify against your own environment and versions; corrections and better answers are welcome below.* A commit missing from branch history may still exist in Git's object database. Reflogs record local reference movements, including previous HEAD positions after resets, rebases, and checkouts. Inspect the history of those movements: ```bash git reflog --date=iso ``` Find a likely commit hash, inspect it, then create a branch pointing to it: ```bash git show <commit-hash> git branch recovered-work <commit-hash> ``` Replace the placeholder with the actual hash. Creating this branch preserves the commit without changing your current branch, index, or working files. You can inspect the branch and later merge or cherry-pick the work you need. Do not assume `HEAD@{1}` is always the desired revision; subsequent operations add reflog entries. If HEAD's reflog is insufficient, try `git reflog show --all` for other available reflogs. Reflogs are local and are not transferred by ordinary cloning or pushing. Entries expire according to configuration, and unreachable objects may eventually be garbage-collected. Reflog recovery also does not generally restore edits that were never committed, particularly unstaged changes discarded by a hard reset. Act promptly and avoid cleanup commands during recovery. See git reflog (https://git-scm.com/docs/git-reflog).

Your answer