Branches · lesson 5 of 7
Commands and ideas in this lesson
Agit switch --detachvisit a commit
BDetached HEADHEAD on a commit, not a branch
Cgit switch -ckeep what you made there

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

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 3 On main. Joaquin wants to see the sinigang recipe before he made the steps clearer.

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:

Terminal
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.)

Git sandbox
  1. Visit the commit before the index script was added (hint: git log --oneline, then git switch --detach)
  2. Make a change and commit it there
  3. Keep it on a new branch called old-idea
  4. 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

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe

Spot the trap: committing and switching away

Spot the trapJoaquin

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

What does “detached HEAD” mean?
You made a commit in detached HEAD and want to keep it. What do you run before switching away?

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.

All sources for Simple Git