546

With git log, I get a list of commits that I have made so far.

commit f5c5cac0033439c17ebf905d4391dc0705dbd5f1
Author: prosseek 
Date:   Fri Sep 3 14:36:59 2010 -0500

    Added and modified the files.

commit c14809fafb08b9e96ff2879999ba8c807d10fb07
Author: prosseek 
Date:   Tue Aug 31 08:59:32 2010 -0500

    Just simple test for core.editor.

... etc ...
  • How can I revert it back to a specific commit? For example, what should I do if I want to go back to commit c14809fafb08b9e96ff2879999ba8c807d10fb07?

  • Is there any other/better way to go back to a specific commit with Git? For example, can I put some label of each commit to get it back with the label?

prosseek
  • 182,215
  • 215
  • 566
  • 871
  • 1
    Even though this question is actually older than the one it's now marked as a duplicate of, that one has a better answer. http://meta.stackexchange.com/questions/147643/should-i-vote-to-close-a-duplicate-question-even-though-its-much-newer-and-ha/147651#147651 – Ganesh Sittampalam Jun 28 '14 at 19:38
  • 6
    This is a perfect example of how f--k'd up git is (from bwawok's answer): *"... then if you wanted to push this to someone else who has the new history, it would fail"*. What good is it if you can't check it back into the remote repository??? I am absolutely amazed at how difficult Git has made simple operations. – jww Nov 23 '15 at 00:09
  • 3
    @jww I literally have been reading for an hour now on how to go back to a previous commit and I still don't know the answer. You are absolutely correct, git makes things more complicated than it should be. – robben May 15 '17 at 14:55
  • @robben Reset then force push – ReinstateMonica3167040 Jan 20 '20 at 20:30

4 Answers4

848

Do you want to roll back your repo to that state, or you just want your local repo to look like that?

If you reset --hard, it will make your local code and local history be just like it was at that commit. But if you wanted to push this to someone else who has the new history, it would fail:

git reset --hard c14809fa

And if you reset --soft, it will move your HEAD to where they were , but leave your local files etc. the same:

git reset --soft c14809fa

So what exactly do you want to do with this reset?

Edit -

You can add "tags" to your repo.. and then go back to a tag. But a tag is really just a shortcut to the sha1.

You can tag this as TAG1.. then a git reset --soft c14809fa, git reset --soft TAG1, or git reset --soft c14809fafb08b9e96ff2879999ba8c807d10fb07 would all do the same thing.

ivanleoncz
  • 9,070
  • 7
  • 57
  • 49
bwawok
  • 14,898
  • 7
  • 32
  • 43
  • 4
    I needed to roll back to that state. But it's good to know that I may have the option of 'look like that'. Thanks for letting me know. BTW, what option is the default? hard or soft? – prosseek Sep 03 '10 at 19:58
  • 2
    @prosseek the default is actually mixed. --mixed Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action. See http://www.kernel.org/pub/software/scm/git/docs/git-reset.html – bwawok Sep 03 '10 at 20:01
  • @prosseek: If the various modes of reset are a bit confusing to you, even after reading the man page (carefully!) you could try this other question http://stackoverflow.com/questions/2530060/can-you-explain-to-me-git-reset-in-plain-english – Cascabel Sep 03 '10 at 20:18
  • 19
    Peter's answer below makes more sense than this – Dr. Dan Aug 30 '13 at 11:48
  • git reset --hard c14809fa ( note: and not necessary to again do git pull ) –  Aug 20 '14 at 10:35
  • 5
    To clarify: "git reset --soft" does NOT leave the history the same, as it removes all the commit entries done after the one reset to and labels the files from those newer commits as "Changes to be committed". I guess it depends on how you define "history". The sentence "but leave your history etc. the same." made me interpret this answer as if the command doesn't affect any of the commits at all, which it does. See Peter A's answer on how to keep the commits done after the commit reverted to. – jomni Jan 29 '16 at 14:28
  • "at that commit" is ambiguous: if I do "git reset --hard X" is the repo state same as before commit X or after commit X ... – nyholku May 07 '17 at 09:23
  • Do not use soft or hard reset if you just want to have a look at your older commit, use Peter suggestion! For example: git checkout c14809fafb08b9e96ff2879999ba8c807d10fb07 – Philipp Schwarz Aug 15 '17 at 13:31
  • 1
    This worked, but then I needed "git push --force" in place of my normal "git push". – HoldOffHunger Aug 15 '18 at 18:53
  • @bwawok , why put a " c14809fa" and not the full hash? What is the min. no.of characters to include of the hash? will it work as long as it's different? – juztcode Aug 15 '20 at 08:48
  • Live + Time saver. – Abhimanyu Shekhawat Dec 21 '22 at 08:55
237

I think, bwawok's answer is wrong at some point:

if you do

git reset --soft c14809fa

It will make your local files changed to be like they were then, but leave your history etc. the same.

According to manual: git-reset, "git reset --soft"...

does not touch the index file nor the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git status would put it.

So it will "remove" newer commits from the branch. This means, after looking at your old code, you cannot go to the newest commit in this branch again, easily. So it does the opposide as described by bwawok: Local files are not changed (they look exactly as before "git reset --soft"), but the history is modified (branch is truncated after the specified commit).

The command for bwawok's answer might be:

git checkout <commit>

You can use this to peek at old revision: How did my code look yesterday?

(I know, I should put this in comments to this answer, but stackoverflow does not allow me to do so! My reputation is too low.)

Peter A
  • 2,501
  • 1
  • 13
  • 8
  • 4
    but `git checkout` is read only, therefore you can't commit an old revision. – robben May 15 '17 at 14:54
  • 2
    @Peter A: After "git reset --soft" followed by "git checkout", I don't see a change to a file I've been working on. Could it be because I'm not in the master branch? –  May 24 '17 at 23:06
  • 2
    Also you can then do 'git checkout ' to come to the original state after viewing different commits. – Ankit Bhatnagar Jan 25 '18 at 07:42
48

git reset c14809fafb08b9e96ff2879999ba8c807d10fb07 is what you're after...

Doches
  • 3,276
  • 2
  • 19
  • 26
24

If you want to force the issue, you can do:

git reset --hard c14809fafb08b9e96ff2879999ba8c807d10fb07

send you back to how your git clone looked like at the time of the checkin

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
kenzaraque
  • 298
  • 2
  • 7
  • 2
    in what cases would you need to force? – jacoballenwood May 06 '17 at 21:24
  • 2
    what does "at the time of the checkin" mean? ambiguous. if I do "git reset --hard X" is the repo state same as before commit X or after commit X ... – nyholku May 07 '17 at 09:21
  • 2
    git reset --hard X. This will roll back the repository state as before commit X. It will make your local code and local history be just like it was at that commit. But then if you wanted to push this to local master or remote master with different history, it would fail. So it will be read only version of your repository. – Malatesh Patil Jun 23 '17 at 11:34
  • You would want to force the issue if say you checkout a remote branch that you have never checked out before and when you do a `git pull` it forces you to do a merge when all you want is the code sitting at HEAD for that branch. – cwa Sep 26 '19 at 23:34