go back to a specific commit then go back to the present

I'd like to see the file from a specific commit in the past, I'm thinking of git reset --hard but I won't be able to see the commits beyond that commit. How do I go back to a commit (together with the state of the files), have a look at the files, the go to the present again?


Solution 1:

Suppose your current branch is master and the old commit is a1b2c3, then you can change all the files in your working tree back to the old commit with:

git checkout a1b2c3

... and return to master with:

git checkout master

This way of hopping about in your git history (i.e. checking out a commit with its object name, also known as its hash or SHA1sum) is very useful for finding a previous good commit for git bisect, for example, since it won't move your branches.

One thing to bear in mind is that you'll get a possibly confusing warning when doing this: if you check out a commit from its object name (a1b2c3) that will put you into a state known as "detached HEAD", where HEAD, which usually represents your current branch, instead points directly to a particular commit. This isn't something to worry about - it's very useful for moving about in your history - but it does mean that if you create new commits when HEAD is detached, they won't move a branch forward.


fork0 points out in the comment below the potentially useful shortcut git checkout -, which will checkout the previous branch or commit that HEAD pointed at.