Sub topic 10
Rescuing commits made on a detached HEAD
This chapter assumes you know how to commit, branch and switch, and that you can read git reflog output and use HEAD@{n} selectors. It covers one case: commits made while HEAD points at a commit instead of a branch.
What a detached HEAD does to your commits
Normally HEAD points at a branch name, and the branch points at a commit. When you commit, the branch moves forward. In a detached HEAD state, HEAD points straight at a commit. New commits move HEAD, but no branch moves with it.
Switching away leaves those commits with no branch pointing at them. git log on any branch will not show them. They are not deleted, though. The HEAD reflog recorded every move, so the commits are still listed there.
Reproducing the situation
The following commands create a commit on a detached HEAD and then switch away.
git switch --detach HEAD~1
git commit -m "Experiment"
git switch mainGit warns when you leave a detached HEAD that has commits. The warning includes the hash, but it is easy to miss. If you did not copy it, use the reflog.
Finding the commit
Run the reflog and look for your commit message.
git reflogThe output looks like this:
a1b2c3d HEAD@{0}: checkout: moving from 7e8f9a0 to main
7e8f9a0 HEAD@{1}: commit: Experiment
5c4d3e2 HEAD@{2}: checkout: moving from main to 5c4d3e2Read it from the bottom up. Entry HEAD@{2} is where you detached. Entry HEAD@{1} is the commit you made. Entry HEAD@{0} is the switch back to main. The hash on the commit: Experiment line, here 7e8f9a0, is the commit to rescue.
Before you restore it, check what it contains, as you did in the earlier chapter on inspecting entries.
git show --stat HEAD@{1}Keeping the commit with a branch
Any branch name that points at the commit makes it reachable again. Create one:
git branch rescue 7e8f9a0Replace 7e8f9a0 with the hash from your own reflog. The branch rescue now holds the commit and its history. You can switch to it, merge it, or cherry-pick from it.
git switch rescue
git log --oneline -3If you only want one commit on main, cherry-pick it instead:
git cherry-pick 7e8f9a0Common errors
- Searching branch history.
git log --alldoes not list the commit, because no reference points to it. Usegit reflog. - Reading the wrong entry. The
checkout: moving from ...line names where you came from, not the commit you made. Use the line that begins withcommit:. - Waiting too long. Reflog entries expire. Unreachable entries are pruned after 30 days by default, so create the branch soon after you notice the problem.
- Using
main's reflog. The commit was made while HEAD was detached, so it appears in the HEAD reflog, not ingit reflog show main.
Exercise
Exercise
You ran git switch --detach HEAD~1, made a commit called "Experiment", then ran git switch main. The commit no longer appears in git log. How do you get it back?
Show answer
Run git reflog and find the entry that reads commit: Experiment, then copy its hash. Optionally check it with git show --stat <hash>. Then run git branch rescue <hash> to create a branch that points at it. The commit is then reachable from rescue.
Exercise
Why does the commit show up in git reflog but not in git reflog show main?
Show answer
HEAD was detached when you committed, so only HEAD moved. The main branch tip did not change, so its own reflog has no entry for the commit.