Detached HEAD and old versions
Last updated
Sometimes you want to see the project as it was: last week's index script, or the adobo before the salt changed. You can switch to any commit, not just a branch. Git calls that "detached HEAD", which sounds alarming and isn't. In this lesson you'll visit an old commit, and learn the one thing to do if you make commits there.
By the end of this lesson you can
- Visit an old commit with git switch --detach
- Explain detached HEAD as HEAD pointing at a commit instead of a branch
- Keep commits made in detached HEAD by making a branch for them
Visiting an old commit
git switch --detach <commit> changes your working folder to match any commit: a hash, HEAD~3, or a tag. You can read the files, run the index script, and see how things were. Switch back to a branch when you're done.
$ git log --oneline -6 67e435d (HEAD -> main) Cover the leche flan with foil ba04ca2 Add lumpiang shanghai recipe 98a5edc Add a script that builds the index page d078487 Make the sinigang steps clearer 145dc1e Add pancit bihon recipe 52d1f4d Say how much salt goes in the adobo
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
To see just one old file without moving anywhere, git show HEAD~4:sinigang.md prints it (lesson 2.3).
What "detached" means
Normally HEAD points at a branch, and the branch points at a commit. When you commit, the branch moves.
In detached HEAD, HEAD points straight at a commit, with no branch in between. git status says HEAD detached at 1f359a2, and the sandbox's prompt shows the hash instead of a branch name.
Looking around is completely safe. git switch --detachSafe: only adds or reads; nothing is lost The only thing to know is what happens to new commits made there: no branch moves to hold them.
(Older instructions say git checkout 1f359a2, which does the same and prints a long note starting "You are in 'detached HEAD' state".)
Commits made there, and keeping them
You can commit in detached HEAD: HEAD moves to the new commit. But no branch points at it. When you switch back to main, nothing holds on to it, and Git warns you:
git switch main Warning: you are leaving 1 commit behind, not connected to any of your branches: 4be1c07 Try the index script with headings If you want to keep it by creating a new branch, this may be a good time to do so with: git branch <new-branch-name> 4be1c07 Switched to branch 'main'
To keep it, give it a branch, before switching away: git switch -c try-headings. Now a label holds the commit, and you're on that branch, no longer detached. (If you've already switched away, the warning's git branch <name> <hash> still works, as does the reflog.)
- Visit the commit before the index script was added (hint:
git log --oneline, thengit switch --detach) - Make a change and commit it there
- Keep it on a new branch called
old-idea - Switch back to main. The commit is safe on old-idea
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
You are in ~/kusina, which has ten commits from two weeks of work.
Commit graph
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Spot the trap: committing and switching away
Joaquin visits an old commit to try something, likes the result, commits it, and runs git switch main. Git prints a long warning. He doesn’t read it.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Git documentation: git-switch, --detach: switching to a commit for inspection
- Git documentation: git-checkout, Detached HEAD: what happens to commits made while detached
- Git documentation: gitglossary, detached HEAD: the official meaning