Sub topic 8
Restoring a deleted branch
This chapter assumes you know that a branch is a movable pointer to a commit, and that you can run basic Git commands in a terminal. It also assumes you have read the earlier chapters on reading git reflog output and using selectors.
Why a deleted branch can come back
Deleting a branch removes only the pointer. The commits stay in the object database until garbage collection removes them. If you know the hash of the branch's last commit (its tip), you can create a new branch that points to it.
Case 1: you have the hash
When you delete a branch with git branch -D, Git prints the old tip. Copy it and recreate the branch.
git branch -D feature
# Deleted branch feature (was 4f9e2ab).
git branch feature 4f9e2abThe form is git branch <name> <commit>. The new branch contains everything the old one did, because the tip commit still links to its parents.
Case 2: you did not note the hash
The HEAD reflog records each checkout. When you switched away from feature, Git logged a line such as checkout: moving from feature to main. The commit hash on that line is the commit HEAD was on just before the switch, which is usually the branch tip.
git reflog | grep 'checkout: moving from feature'
git branch feature-restored <hash-from-reflog>Replace <hash-from-reflog> with the hash at the start of the matching line. If the branch was checked out several times, you get several lines. The newest one is at the top.
Do not rely on the first match alone. You may have committed on feature and switched away again later, so check that the entry is the one you want.
Check before you restore
Inspect the candidate commit first, as shown in the previous chapter.
git log --oneline -5 <hash-from-reflog>
git show --stat <hash-from-reflog>If the last commit message and the changed files match what you remember, create the branch. If not, try another entry. Creating a branch is cheap, so you can also make a temporary branch, look at it, and delete it again.
When this does not work
- Commits made after you left the branch. The checkout entry shows the tip at the time you switched away. If you later checked out
featureagain and committed more, look for the newer checkout entry. - You never checked the branch out. A branch that was created and used only by other commands, such as a branch you fetched but never switched to, has no checkout entry in your HEAD reflog. Per-branch reflogs are removed when the branch is deleted.
- Expired entries. Once an entry expires and garbage collection runs, the commits may be gone.
- Uncommitted work. Changes that were never committed are not in any reflog.
If the HEAD reflog has nothing useful, the last resort is git fsck --lost-found, which is covered in a later chapter.
Exercise
You ran git branch -D feature and did not note the hash. You were last on feature before switching to main. Which commands find the tip and restore the branch?
Show answer
Run git reflog | grep 'checkout: moving from feature' to find the commit hash on the newest matching line. Check it with git log --oneline -5 <hash>. Then run git branch feature <hash> to recreate the branch at that commit.
Exercise
Why does deleting a branch usually not destroy its commits right away?
Show answer
Deleting a branch removes only the branch pointer. The commits remain in the object database until they are unreachable and garbage collection prunes them after the reflog entries expire.