How do I undo the most recent commits in Git?

2018-12-30 23:21发布

I accidentally committed the wrong files to Git, but I haven't pushed the commit to the server yet.

How can I undo those commits from the local repository?

30条回答
零度萤火
2楼-- · 2018-12-30 23:52

This took me a while to figure out, so maybe this will help someone...

There are two ways to "undo" your last commit, depending on whether or not you have already made your commit public (pushed to your remote repository):

How to undo a local commit

Let's say I committed locally, but now want to remove that commit.

git log
    commit 101: bad commit    # latest commit, this would be called 'HEAD'
    commit 100: good commit   # second to last commit, this is the one we want

To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD:

git reset --soft HEAD^     # use --soft if you want to keep your changes
git reset --hard HEAD^     # use --hard if you don't care about keeping the changes you made

Now git log will show that our last commit has been removed.

How to undo a public commit

If you have already made your commits public, you will want to create a new commit which will "revert" the changes you made in your previous commit (current HEAD).

git revert HEAD

Your changes will now be reverted and ready for you to commit:

git commit -m 'restoring the file I removed by accident'
git log
    commit 102: restoring the file I removed by accident
    commit 101: removing a file we don't need
    commit 100: adding a file that we need

For more info, check out Git Basics - Undoing Things

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

If you are planning to undo a local commit entirely, whatever you change you did on the commit, and if you don't worry anything about that, just do the following command.

git reset --hard HEAD^1

(This command will ignore your entire commit and your changes will be lost completely from your local working tree). If you want to undo your commit, but you want your changes in the staging area (before commit just like after git add) then do the following command.

git reset --soft HEAD^1

Now your committed files come into the staging area. Suppose if you want to upstage the files, because you need to edit some wrong content, then do the following command

git reset HEAD

Now committed files to come from the staged area into the unstaged area. Now files are ready to edit, so whatever you change, you want to go edit and added it and make a fresh/new commit.

More

查看更多
春风洒进眼中
4楼-- · 2018-12-30 23:55

To change the last commit

Replace the files in the index:

git rm --cached *.class
git add *.java

Then, if it's a private branch, amend the commit:

git commit --amend

Or, if it's a shared branch, make a new commit:

git commit -m 'Replace .class files with .java files'


(to change a previous commit, use the awesome interactive rebase)


ProTip™:   Add *.class to a gitignore to stop this happening again.


To revert a commit

Amending a commit is the ideal solution if you need to change the last commit, but a more general solution is reset.

You can reset git to any commit with:

git reset @~N

Where N is the number of commits before HEAD, and @~ resets to the previous commit.

So, instead of amending the commit, you could use:

git reset @~
git add *.java
git commit -m "Add .java files"

Check out git help reset, specifically the sections on --soft --mixed and --hard, for a better understanding of what this does.

Reflog

If you mess up, you can always use the reflog to find dropped commits:

$ git reset @~
$ git reflog
c4f708b HEAD@{0}: reset: moving to @~
2c52489 HEAD@{1}: commit: added some .class files
$ git reset 2c52489
... and you're back where you started


查看更多
回忆,回不去的记忆
5楼-- · 2018-12-30 23:55

For a local commit

git reset --soft HEAD~1

or if you do not remember exactly in which commit it is, you might use

git rm --cached <file>

For a pushed commit

The proper way of removing files from the repository history is using git filter-branch. That is,

git filter-branch --index-filter 'git rm --cached <file>' HEAD

But I recomnend you use this command with care. Read more at git-filter-branch(1) Manual Page.

查看更多
明月照影归
6楼-- · 2018-12-30 23:56

Another way:

Checkout the branch you want to revert, then reset your local working copy back to the commit that you want to be the latest one on the remote server (everything after it will go bye-bye). To do this, in SourceTree I right-clicked on the and selected "Reset BRANCHNAME to this commit".

Then navigate to your repository's local directory and run this command:

git -c diff.mnemonicprefix=false -c core.quotepath=false push -v -f --tags REPOSITORY_NAME BRANCHNAME:BRANCHNAME

This will erase all commits after the current one in your local repository but only for that one branch.

查看更多
不再属于我。
7楼-- · 2018-12-30 23:57

If you want to permanently undo it and you have cloned some repository

The commit id can be seen by

git log 

Then you can do -

git reset --hard <commit_id>

git push origin <branch_name> -f
查看更多
登录 后发表回答