AdvancedLesson 27 of 306 min read

Disaster Recovery: Reflog, ORIG_HEAD and Dangling Commits

Rescue a deleted branch from the reflog, undo a reset with ORIG_HEAD, and see exactly when work is truly gone.

In this lesson you will

  • Rescue a deleted branch from the reflog
  • Undo a reset with ORIG_HEAD
  • Explain when recovery stops working
  • Follow a calm recovery checklist

About 30 minutes

“I deleted the branch with all my work”

It is late, and Nia tidies her branches with a delete command. A minute later she realises the herb branch had two commits that were never merged. Her stomach drops. This lesson is the calm after that moment. You already know why committed work is hard to lose: lesson 5 showed the reflog and lesson 21 showed that unreachable objects stay in the store. Now we put it together into a short recovery routine.

Recovery tool one: the reflog

The reflog is Git’s private diary of where your branches and your current position have been. It is kept on your computer only, and it is never sent to a remote. After a branch is deleted, the commits still exist, and the reflog still remembers where you stood just before. The recipe is three steps: look at the reflog, find the entry for the last commit on the lost branch, and give it a new branch name.

$ mkdir garden-notes
$ cd garden-notes
$ git init
Initialized empty Git repository in /home/nia/garden-notes/.git/
$ echo "Beans: row 1" > plan.txt
$ git add plan.txt
$ git commit -m "Add planting plan"
[main (root-commit) cd4328a] Add planting plan
 1 file changed, 1 insertion(+)
 create mode 100644 plan.txt
$ git switch -c add-herbs
Switched to a new branch 'add-herbs'
$ echo "Herbs: pots by the door" >> plan.txt
$ git commit -am "Add herb pots"
[add-herbs 0af969d] Add herb pots
 1 file changed, 1 insertion(+)
$ echo "Herbs: water daily" >> plan.txt
$ git commit -am "Add herb watering"
[add-herbs 95671c5] Add herb watering
 1 file changed, 1 insertion(+)
$ git switch main
Switched to branch 'main'
$ git branch -D add-herbs
Deleted branch add-herbs (was 95671c5).
$ git branch
* main
$ git reflog | head -4
cd4328a HEAD@{0}: checkout: moving from add-herbs to main
95671c5 HEAD@{1}: commit: Add herb watering
0af969d HEAD@{2}: commit: Add herb pots
cd4328a HEAD@{3}: checkout: moving from main to add-herbs
$ git branch rescued HEAD@{1}
$ git log --oneline rescued
95671c5 Add herb watering
0af969d Add herb pots
cd4328a Add planting plan

Read the reflog lines. The entry numbered one is the herb watering commit, the last thing done on the lost branch. The command to create a branch at that entry brings both commits back, and the log of the new branch proves it. The delete command also helped: it printed the old tip of the branch in brackets when it deleted it. Writing that code down is another way to recover.

Recovery tool two: the undo for your undo

Git remembers the position before many risky operations, such as a reset or a rebase, in a special name, ORIG_HEAD. If you reset away work and regret it, resetting to ORIG_HEAD puts you back. The next session resets the rescued branch back one commit, so the watering commit disappears from the log, and then restores it.

$ git switch rescued
Switched to branch 'rescued'
$ git reset -q --hard HEAD~1
$ git log --oneline
0af969d Add herb pots
cd4328a Add planting plan
$ git reset -q --hard ORIG_HEAD
$ git log --oneline
95671c5 Add herb watering
0af969d Add herb pots
cd4328a Add planting plan

Recovery tool three: dangling objects

When a commit has no branch and no reflog entry pointing at it, it is dangling, as in lesson 21. The check command can list such objects, and you can create a branch from one. This is the last resort, and it only works while the objects still exist.

When work really is lost

Objects do not live for ever. The reflog entries expire, and a cleanup removes unreachable objects after its grace period. Normally you never notice. The final session does what a patient cleanup would do eventually, but at once, with options that say “expire everything now” and “prune everything now”. First it saves the commit code to a file and proves the commit exists. Then it deletes the branch, expires the reflog and prunes. Afterwards Git cannot find the object at all, and the check command lists nothing.

$ git rev-parse HEAD > ../lost.txt
$ git cat-file -t $(cat ../lost.txt)
commit
$ git switch main
Switched to branch 'main'
$ git branch -D rescued
Deleted branch rescued (was 95671c5).
$ git reflog expire --expire=now --all
$ git gc -q --prune=now
$ git cat-file -t $(cat ../lost.txt)
fatal: git cat-file: could not get object info
$ git fsck --lost-found | wc -l
0

Those two options exist for cleaning up after a secret leak or a huge mistake, and they are rarely what you want. The lesson is not “be afraid”. It is that recovery has a window. The sooner you look, the better your chances. And a commit that was pushed or copied to a teammate’s computer exists elsewhere too, which can save you when your own store is gone.

A recovery checklist

  1. Stop. Do not run cleanup commands, and do not make new commits that could confuse the picture.
  2. Look. Run the reflog and the status. Write down what you see.
  3. Mark. Create a branch at the commit you want, so a name protects it.
  4. Check. Read the log and the files before you carry on.
  5. Ask. If the work was pushed, a teammate’s copy or the remote may still hold it.
Common mistake: panic commands

Running a cleanup or a hard reset again in a panic, hoping it will fix things. Each extra command makes the picture harder to read. Look first, then act.

A window, not a guarantee

The branch and files are made up. How long the reflog and unreachable objects survive depends on your settings and Git version, so recovery times are not guaranteed. Only committed work is covered: changes that were never committed cannot be recovered by any of these tools.

Delete a practice branch on purpose and rescue it from the reflog. Then reset a branch and use ORIG_HEAD to undo it. Do it once calmly, so you can do it in a hurry.

Practice for this lesson

Check what you learned

Five quick questions. Pick an answer and you will see straight away whether it is right and why.

What is the reflog?

  1. A list of remote servers
  2. A log of GitHub visitors
  3. A private record of where your branches and position have been
  4. A file that Git sends to teammates

The reflog entry numbered one showed the lost tip.

How did Nia bring the deleted branch back?

  1. By creating a new branch at the reflog entry for its last commit
  2. By downloading it from GitHub
  3. By editing .git by hand
  4. By restarting the computer

The new rescued branch showed both herb commits.

What does ORIG_HEAD help with?

  1. Renaming a remote
  2. Signing commits
  3. Undoing a reset or rebase by returning to the earlier position
  4. Merging two repositories

Resetting to it restored the watering commit.

What happened after the reflog was expired and unreachable objects pruned?

  1. Git could no longer find the commit at all
  2. The commit moved to GitHub
  3. The commit was renamed
  4. Nothing changed

The type lookup failed and the check found nothing.

Which work can none of these tools recover?

  1. A deleted branch with commits
  2. Changes that were never committed
  3. A reset away commit
  4. A dangling commit

Git only remembers what was committed.

Summary

  • The reflog remembers where your branches were, on your computer only.
  • Create a branch at the lost commit so that a name protects it.
  • ORIG_HEAD is the undo for a reset or rebase.
  • Recovery has a window, and uncommitted work cannot be recovered.

Next lesson

Lesson 28 turns Git skills into professional work.

Questions

Does the reflog show other people's work?

No. It is local to your own repository. A teammate has their own reflog.

How long do I have to recover something?

It depends on settings and your Git version. The defaults are weeks to months for the reflog, and a shorter grace period for unreachable objects. Act quickly and do not rely on it.

Can I recover a force-pushed branch on a remote?

The remote may not keep it. A teammate's clone, or your own reflog, may still hold the old tip, so ask around before anything is cleaned up.

Progress is saved in this browser.