Reputation: 11579
Let's say I have committed many times. Now I want to come back to a certain commit and view how my working directory looked like at that time (I actually want my working directory to change when I do it). And finally go back to original head without messing up my commits?!
I know it should be easy to do it, I'm just can't seem to find the right command. Thank you.
Upvotes: 2
Views: 2275
Reputation: 139531
Creating a detached HEAD as in Jefromi's answer may do what you want.
We can give you more helpful answers if you tell us your underlying motivation. For example, if the commit in question was a significant milestone, then maybe you want to tag it to give it a meaningful name:
git tag -a -m 'Release 1.0' v1.0 <sha1>
Other possibilities:
git checkout -b my-branch-name <sha1>
git show <sha1>:foo.c
git diff <sha1> HEAD
git diff <sha1> HEAD -- xyz.cpp
Upvotes: 1
Reputation: 496972
To check out a given commit, use git checkout
:
git checkout <SHA1 of commit>
To return to your original branch, check it out:
git checkout master
A couple notes:
If you have uncommitted modifications, the checkout will probably fail. You might want to commit or stash them first.
When you check out something which is not a branch, you end up in detached HEAD state; that is, your HEAD doesn't refer to a branch - surprise! This means that in particular, if you make a commit, it won't be on a branch. Make sure to check out a branch if you want to commit. If your intent is to make a commit off of the one you're checking out, you can check it out and create a branch at the same time: git checkout -b new-branch <SHA1 of commit>
Upvotes: 8
Reputation: 77540
git checkout <sha of old commit>
Then when you're done, just git checkout master
or wherever you want to be.
Upvotes: 1