Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

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?

like image 627
Jürgen Paul Avatar asked Aug 16 '12 13:08

Jürgen Paul


People also ask

Can I go back to a specific commit in git?

Go back to the selected commit on your local environmentUse git checkout & the ID (in the same way you would checkout a branch) to go back: $ git checkout <commit-id> . Don't forget the final ' .

How do I go back to a specific commit in Visual Studio?

The original commit is still in the Git history. To do the same in Visual Studio, right-click the commit you want to revert and then select Revert. After you confirm your action and the operation is complete, Visual Studio displays a success message and a new commit appears in the Outgoing section.


1 Answers

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.

like image 66
Mark Longair Avatar answered Sep 22 '22 06:09

Mark Longair