Reverting to a specific commit based on commit id with Git?

Git

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.)

Solution 3 - Git

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

Solution 4 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionprosseekView Question on Stackoverflow
Solution 1 - GitbwawokView Answer on Stackoverflow
Solution 2 - GitPeter AView Answer on Stackoverflow
Solution 3 - GitDochesView Answer on Stackoverflow
Solution 4 - GitkenzaraqueView Answer on Stackoverflow