2

I have been reading other questions on stack overflow but nothing simply explains what I need. I am pushing to a remote repo with one other collaborator. I want to check out a past commit from a few weeks ago. How do I do this?

Alex Borsody
  • 1,908
  • 9
  • 40
  • 74

2 Answers2

5
git checkout -b <branch_name> <sha1>

(this will create a new branch and checkout at that particular commit that you want)

If you just want to reset current branch to it:

git reset --hard <sha1>

Update on sha1:

What I meant by Sha1 is this long hash that is associated with each commit and identifies it. You can easily find it by doing a git log

Sample git log output:

commit 10a45e0f0680b8fd493ed0264fe24be2648af1b3
Author: manojlds <manojlds@gmail.com>
Date:   Thu Oct 13 19:04:23 2011 -0600

    some other commit

10a45e0f0680b8fd493ed0264fe24be2648af1b3 is the sha1 hash.

manojlds
  • 290,304
  • 63
  • 469
  • 417
2
 git log # to check what SHA1 you need
 git checkout -b tmp SHA1 # to checkout the right SHA1 and create a tmp branch

If you checkout directly a SHA1, you would end up with a DETACHED HEAD, with the risk of making some unreferenced commits.
See for instance "git: how to retrieve all files as of a certain date".

Note that you have various syntax to checkout by date:

<refname>@{<date>}, e.g. master@{yesterday}, HEAD@{5 minutes ago}
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Um, why did you edit and add so much???? First of all, `fetch` to go back to a commit?? And checkout by date when OP asks for **a** commit and not some commit few weeks ago. This answer is just noise now, IMO. – manojlds Oct 14 '11 at 06:39
  • @manojlds: You're right that the fetch is not necessary in this particular context (though it would be in others), but otherwise the answer's quite reasonable: it explains why you shouldn't simply check out an SHA1, and provides some potentially helpful extra information. +1. – Cascabel Oct 15 '11 at 04:01
  • @Jefromi - I didn't mean any disrespect to VonC. Actually, I am a fan of his answers :). Just that, he had added so many things ( I was about to delete my answer when I saw that I had given the same answer as him!) , which may confuse a newbie. – manojlds Oct 15 '11 at 04:12