Dziamid
Dziamid

Reputation: 11579

Walk the history with git?

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

Answers (3)

Greg Bacon
Greg Bacon

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:

  • Create and switch to a branch at that point
git checkout -b my-branch-name <sha1>
  • View the contents of a particular file from that commit
git show <sha1>:foo.c
  • See everything that has changed since that commit
git diff <sha1> HEAD
  • See changes in a particular file
git diff <sha1> HEAD -- xyz.cpp

Upvotes: 1

Cascabel
Cascabel

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

dahlbyk
dahlbyk

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

Related Questions