How to Check Out an Earlier Commit in Git
git checkout <commit> puts the files of an earlier commit back in your project folder, so you can look at them, or run them, exactly as they were. Git calls the state you're then in a "detached HEAD", and git switch main takes you back to the newest version.
This guide goes back two commits and one commit in a small repository, with real output from Git 2.56.1.
Syntax
git checkout <commit>
git switch <branch>
| Part | What it means |
|---|---|
git checkout <commit> | Puts the files of <commit> in your project folder |
<commit> | The commit to go to: its hash, such as d3162e9, or a name counted back from the commit you're on, such as HEAD~1 |
git switch <branch> | Goes back to a branch, such as main, and its newest files |
Git's git checkout documentation says that git checkout <commit> works like checking out a branch, except that instead of pointing HEAD at the branch, it points HEAD at the commit ID. HEAD is Git's name for "the commit you're on now".
The Example
A small repository called poem has three commits on its main branch. Each adds one line to poem.txt.
Prerequisites
- Git. Run
git --versionto see which one you have; this guide uses Git 2.56.1.
Step 1: Find the Commit's Hash
In the repository's folder:
git log --oneline
1f21b39 (HEAD -> main) Add the third line
ad31c93 Add the second line
d3162e9 Start the poem

Each line is one commit, newest first: a short hash, then the commit's message. HEAD -> main means you're on the main branch, at its newest commit. The first commit, Start the poem, has the hash d3162e9. Your hashes will be different, because a hash depends on who made the commit and when. How to Show Git History One Line per Commit with git log --oneline explains this view.
Step 2: Check Out the Commit
git checkout d3162e9
Note: switching to 'd3162e9'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
HEAD is now at d3162e9 Start the poem

The last line is the one that matters: HEAD is now at d3162e9 Start the poem. The files in your folder are now the ones from that commit.
The rest is advice. "Detached HEAD" means you're on a commit, not on a branch. In the documentation's words, It means simply that HEAD refers to a specific commit, as opposed to referring to a named branch. Looking around is safe. Commits you'd make here belong to no branch, which is why Git suggests git switch -c <new-branch-name> if you want to keep them.
Step 3: Look Around
The file now has only the first line:
cat poem.txt
Roses are red,
git status says where you are:
git status
HEAD detached at d3162e9
nothing to commit, working tree clean
And git log shows history only up to the commit you're on:
git log --oneline
d3162e9 (HEAD) Start the poem

The two later commits aren't gone. They're on main, and naming the branch shows them:
git log --oneline main
1f21b39 (main) Add the third line
ad31c93 Add the second line
d3162e9 (HEAD) Start the poem
(main) marks where the branch is, and (HEAD) where you are. The commands are the same in PowerShell.
Step 4: Go Back to Your Branch
git switch main
Previous HEAD position was d3162e9 Start the poem
Switched to branch 'main'
cat poem.txt
Roses are red,
Violets are blue,
Sugar is sweet,

You're back on main, with all three lines. git switch only switches branches, which makes it the clearer command for going back. git checkout main does the same and printed the same two lines.
Step 5: Count Back from Where You Are with HEAD~1
You don't need a hash to go back a few commits. HEAD~1 means the commit before the one you're on, HEAD~2 the one before that, and so on:
git checkout HEAD~1
HEAD is now at ad31c93 Add the second line
cat poem.txt
Roses are red,
Violets are blue,

Git printed the same detached HEAD note as in Step 2, then HEAD is now at ad31c93 Add the second line: one commit back from main's newest. Git's gitrevisions documentation describes ~<n> as the commit object that is the <n>th generation ancestor of the named commit object, following only the first parents: <n> commits back. HEAD~1 works the same in PowerShell.
Go back to main before the next step:
git switch main
Previous HEAD position was ad31c93 Add the second line
Switched to branch 'main'
Step 6: See What Happens with Changes You Haven't Committed
Add a line to the poem, without committing it, then try to check out the first commit:
- bash / zsh
- PowerShell
echo "And so are you." >> poem.txt
Add-Content poem.txt "And so are you."
git checkout d3162e9
error: Your local changes to the following files would be overwritten by checkout:
poem.txt
Please commit your changes or stash them before you switch branches.
Aborting

Git stopped before changing anything. Checking out the first commit means replacing poem.txt with its first version, which would lose the new line, because no commit holds it. Git names two ways out: commit the change, or stash it, which puts it aside for later. Here the line wasn't wanted, so throw it away with git restore, which puts the file back as it was in the last commit:
git restore poem.txt
git checkout d3162e9
HEAD is now at d3162e9 Start the poem
git restore prints nothing, and the checkout now works. Go back to main with git switch main.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
| Committing in detached HEAD, then switching back | The new commits belong to no branch, and are easy to lose | Create a branch first, with git switch -c <new-branch-name> |
| Thinking the later commits are gone | git log --oneline stops at the commit you're on | git log --oneline main |
| Checking out with uncommitted changes to the same files | error: Your local changes to the following files would be overwritten by checkout | Commit, stash or git restore the changes first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
git status says HEAD detached at … | You checked out a commit and haven't gone back | git switch main |
error: pathspec '…' did not match any file(s) known to git | The hash is mistyped, or from another repository | Copy it from git log --oneline |
FAQ
Is a detached HEAD dangerous?
No. Your branch and its commits stay as they were, and git switch main takes you back. Only commits you make while detached need care, because no branch points to them.
Does git checkout change my history?
No. It only changes which commit's files are in your folder. The commits, and the main branch, stay exactly as they were, as git log --oneline main showed in Step 3.
Conclusion
git checkout d3162e9 put the first version of poem.txt back in the folder, and git checkout HEAD~1 went back one commit without a hash. Git called it a detached HEAD, git log --oneline main showed that the later commits were still there, and git switch main brought the newest version back. With uncommitted changes in the way, Git refused until they were committed, stashed or restored.
Additional Resources
- git checkout: every form of the command, and the detached HEAD section.
- git switch: switching branches.
- gitrevisions:
HEAD~<n>and the other ways to name a commit.
