How to revert a Git repository to a previous commi

2018-12-30 22:53发布

How do I revert from my current state to a snapshot made on a certain commit?

If I do git log, then I get the following output:

$ git log
commit a867b4af366350be2e7c21b8de9cc6504678a61b`
Author: Me <me@me.com>
Date:   Thu Nov 4 18:59:41 2010 -0400

blah blah blah...

commit 25eee4caef46ae64aa08e8ab3f988bc917ee1ce4
Author: Me <me@me.com>
Date:   Thu Nov 4 05:13:39 2010 -0400

more blah blah blah...

commit 0766c053c0ea2035e90f504928f8df3c9363b8bd
Author: Me <me@me.com>
Date:   Thu Nov 4 00:55:06 2010 -0400

And yet more blah blah...

commit 0d1d7fc32e5a947fbd92ee598033d85bfc445a50
Author: Me <me@me.com>
Date:   Wed Nov 3 23:56:08 2010 -0400

Yep, more blah blah.

How do revert to the commit from November 3, i.e. commit 0d1d7fc?

30条回答
高级女魔头
2楼-- · 2018-12-30 23:04

If you want to "uncommit", erase the last commit message, and put the modified files back in staging, you would use the command:

git reset --soft HEAD~1
  • --soft indicates that the uncommitted files should be retained as working files opposed to --hard which would discard them.
  • HEAD~1 is the last commit. If you want to rollback 3 commits you could use HEAD~3. If you want to rollback to a specific revision number, you could also do that using its SHA hash.

This is an extremely useful command in situations where you committed the wrong thing and you want to undo that last commit.

Source: http://nakkaya.com/2009/09/24/git-delete-last-commit/

查看更多
有味是清欢
3楼-- · 2018-12-30 23:05

Reverting Working Copy to Most Recent Commit

To revert to a previous commit, ignoring any changes:

git reset --hard HEAD

where HEAD is the last commit in your current branch

Reverting The Working Copy to an Older Commit

To revert to a commit that's older than the most recent commit:

# Resets index to former commit; replace '56e05fced' with your commit code
git reset 56e05fced 

# Moves pointer back to previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to 56e05fced"

# Updates working copy to reflect the new commit
git reset --hard

Credits go to a similar Stack Overflow question, Revert to a commit by a SHA hash in Git?.

查看更多
余生无你
4楼-- · 2018-12-30 23:07

I have tried a lot of ways to revert local changes in Git, and it seems that this works the best if you just want to revert to the latest commit state.

git add . && git checkout master -f

Short description:

  • It will NOT create any commits as git revert does.
  • It will NOT detach your HEAD like git checkout <commithashcode> does.
  • It WILL override all your local changes and DELETE all added files since the last commit in the branch.
  • It works only with branches names, so you can revert only to latest commit in the branch this way.

I found a much more convenient and simple way to achieve the results above:

git add . && git reset --hard HEAD

where HEAD points to the latest commit at you current branch.

It is the same code code as boulder_ruby suggested, but I have added git add . before git reset --hard HEAD to erase all new files created since the last commit since this is what most people expect I believe when reverting to the latest commit.

查看更多
忆尘夕之涩
5楼-- · 2018-12-30 23:07

Say you have the following commits in a text file named ~/commits-to-revert.txt (I used git log --pretty=oneline to get them)

fe60adeba6436ed8f4cc5f5c0b20df7ac9d93219
0c27ecfdab3cbb08a448659aa61764ad80533a1b
f85007f35a23a7f29fa14b3b47c8b2ef3803d542
e9ec660ba9c06317888f901e3a5ad833d4963283
6a80768d44ccc2107ce410c4e28c7147b382cd8f
9cf6c21f5adfac3732c76c1194bbe6a330fb83e3
fff2336bf8690fbfb2b4890a96549dc58bf548a5
1f7082f3f52880cb49bc37c40531fc478823b4f5
e9b317d36a9d1db88bd34831a32de327244df36a
f6ea0e7208cf22fba17952fb162a01afb26de806
137a681351037a2204f088a8d8f0db6e1f9179ca

Create a Bash shell script to revert each of them:

#!/bin/bash
cd /path/to/working/copy
for i in `cat ~/commits-to-revert.txt`
do
    git revert $i --no-commit
done

This reverts everything back to the previous state, including file and directory creations, and deletions, commit it to your branch and you retain the history, but you have it reverted back to the same file structure. Why Git doesn't have a git revert --to <hash> is beyond me.

查看更多
骚的不知所云
6楼-- · 2018-12-30 23:08

There is a command (not a part of core Git, but it is in the git-extras package) specifically for reverting and staging old commits:

git back

Per the man page, it can also be used as such:

# Remove the latest three commits
git back 3
查看更多
冷夜・残月
7楼-- · 2018-12-30 23:09

Revert to most recent commit and ignoring all local changes:

git reset --hard HEAD
查看更多
登录 后发表回答