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?
Asked
Active
Viewed 3,298 times
2 Answers
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
-
+1 (more concise, and the `git reset` can also help). I'll keep mine because I think it is important to illustrate the issue of DETACHED HEAD when being tempted to checkout a SHA1 without creating a branch. – VonC Oct 14 '11 at 07:03
-
We are running git very simply, I make changes, and push. Then it's his turn. What is sha1 – Alex Borsody Oct 15 '11 at 02:11
-
It looks like it is number of commit, can you give an example. – Alex Borsody Oct 15 '11 at 02:20
-
@alex_borsody - Updated my answer on sha1 hash – manojlds Oct 15 '11 at 03:50
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}
-
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