How do I undo the last commit in git?

19

I accidentally committed the wrong files to git.

How to undo this?

    
asked by anonymous 30.01.2014 / 12:17

5 answers

28

Reset so is recommended if the last commit has not yet been pushed to the server. Otherwise, undoing the last commit will invalidate the local copy.

If you've already pushed, it's best to "roll back" the last commit instead of undoing it. "Revert" in this context means to create a new commit that deletes the entered lines / inserts the deleted lines in the last commit.

git revert HEAD~1

Or HEAD~2 to revert the last 2 commits.

Source: Git revert manpage .

If the not commit has been published, you can undo the last commit using the command git reset - see @paulomartinhago's answer .

    
30.01.2014 / 12:32
20

You can do the following when you delete the activities done on the stage:

git reset HEAD~1 --hard

or to go back to the stage activities:

git reset HEAD~1 --soft
    
30.01.2014 / 12:26
6

After fixing the files run

git add

Then do:

git commit --amend

In fact this command will redo the last commit

    
30.01.2014 / 12:20
4

You can use this command:

git reset --soft HEAD~1
    
30.01.2014 / 12:21
-2

In addition to git revert, you can use the previous commit hash to go back to the version.

For example:

By giving a git log, you will see the last completed commit and a hash identifier. See:

commit c67f03af1701f5c8e47319ae5ad6fc7a2a38151f
Author: Nome <user@server>
Date:   Thu Jan 30 10:50:18 2014 -0200

Debugando

Soon you will have to identify the previous commit to the list you want to undo. So just give a git checkout:

git checkout c67f03af1701f5c8e47319ae5ad6fc7a2a38151f
    
30.01.2014 / 14:17