Skip to main content

How to Check Out an Earlier Commit in Git

· 10 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
git checkout <commit>
git switch <branch>
PartWhat 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 --version to see which one you have; this guide uses Git 2.56.1.

Step 1: Find the Commit's Hash​

In the repository's folder:

Terminal
git log --oneline
Output
1f21b39 (HEAD -> main) Add the third line
ad31c93 Add the second line
d3162e9 Start the poem

The terminal after git log --oneline: three commits, 1f21b39 Add the third line marked HEAD -> main, ad31c93 Add the second line and 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​

Terminal
git checkout d3162e9
Output
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 terminal after git checkout d3162e9: Git's note about the detached HEAD state, its advice on git switch -c and git switch -, and the last line, 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:

Terminal
cat poem.txt
Output
Roses are red,

git status says where you are:

Terminal
git status
Output
HEAD detached at d3162e9
nothing to commit, working tree clean

And git log shows history only up to the commit you're on:

Terminal
git log --oneline
Output
d3162e9 (HEAD) Start the poem

The terminal after cat poem.txt prints Roses are red, git status says HEAD detached at d3162e9 and nothing to commit, and git log --oneline shows only d3162e9 (HEAD) Start the poem.

The two later commits aren't gone. They're on main, and naming the branch shows them:

Terminal
git log --oneline main
Output
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​

Terminal
git switch main
Output
Previous HEAD position was d3162e9 Start the poem
Switched to branch 'main'
Terminal
cat poem.txt
Output
Roses are red,
Violets are blue,
Sugar is sweet,

The terminal after git switch main, which prints Previous HEAD position was d3162e9 Start the poem and Switched to branch 'main', and cat poem.txt, which prints all three lines again.

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:

Terminal
git checkout HEAD~1
Output (last line)
HEAD is now at ad31c93 Add the second line
Terminal
cat poem.txt
Output
Roses are red,
Violets are blue,

The terminal after git checkout HEAD~1, which ends with HEAD is now at ad31c93 Add the second line, and cat poem.txt, which prints the first two lines.

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:

Terminal
git switch main
Output
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:

Terminal
echo "And so are you." >> poem.txt
Terminal
git checkout d3162e9
Output
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

The terminal after a new line is added to poem.txt and git checkout d3162e9 fails: error: Your local changes to the following files would be overwritten by checkout: poem.txt, then Please commit your changes or stash them before you switch branches, and 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:

Terminal
git restore poem.txt
git checkout d3162e9
Output (last line)
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​

MistakeWhat happensDo this instead
Committing in detached HEAD, then switching backThe new commits belong to no branch, and are easy to loseCreate a branch first, with git switch -c <new-branch-name>
Thinking the later commits are gonegit log --oneline stops at the commit you're ongit log --oneline main
Checking out with uncommitted changes to the same fileserror: Your local changes to the following files would be overwritten by checkoutCommit, stash or git restore the changes first

Troubleshooting​

SymptomLikely causeFix
git status says HEAD detached at …You checked out a commit and haven't gone backgit switch main
error: pathspec '…' did not match any file(s) known to gitThe hash is mistyped, or from another repositoryCopy 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​