Git Cheat Sheet · Branches
Go to an old commit
git switch --detach <commit>
To see or test the project as it was at a specific commit, run git switch --detach <commit>. You are then on no branch at all: this is the "detached HEAD" state. Nothing is lost, and git switch <branch> takes you back where you were.
What it does
Puts the working copy in the state of a specific commit, without a branch ("detached HEAD").
A use case
You want to run the application as it was last week to reproduce a bug.
Warning: Commits created in detached HEAD are lost from view as soon as you switch branches: create a branch to keep them.
On the command line
git switch --detach <commit>- Goes to a commit, a tag or a remote branch.
git checkout <commit>- Older syntax, equivalent.
git switch -- Goes back to the previous branch.
git switch -c <branch>- Keeps the work done in detached HEAD in a new branch.
In Merathon
- Right-click the commit in the graph → "Check out this commit (detached HEAD)".
- To go back, double-click a branch in "Local".
Frequently asked questions
What does "You are in 'detached HEAD' state" mean?
HEAD points directly to a commit, not to a branch. You can look around, compile, test. If you commit, create a branch before leaving, otherwise those commits will no longer be visible.
I committed in detached HEAD, then switched branches: how do I get them back?
git reflog gives the hash of the last commit; git branch <branch> <hash> attaches it to a branch.