How do I undo the last commit in Git?

Asked

Viewed 98,339 times

32

I accidentally committed the wrong files to Git.

How to undo this?

6 answers

47


Reset is only recommended if the last commit has not yet been sent ("pushed") to the server. Otherwise, undoing the last commit will invalidate the local copy.

If you’ve already pushed, it’s best to "reverse" the last commit, rather than undo it. "Revert" in this context means creating a new commit that deletes the lines entered/inserts the deleted lines in the last commit.

git revert HEAD~1

Or HEAD~2 to reverse the last 2 commits.

Source: Git revert manpage.


If the commit nay has been published, you can undo the last commit via the command git reset - see reply from @paulomartinhago.

  • 2

    Thank you for complementing dcastro :)

  • How about editing this answer to include the content of others and become more complete? It would make life easier for the future reader...

  • @Thiagoarrais good idea! I added the option of reset and credit due to @paulomartinhago

  • I just don’t know if Stackoverflow-mind would be more appropriate to do what you did, edit the @paulomartinhago response or edit your response as if you had originally done the content including the reset history. I’ll open a question at the finish line...

  • @Thiagoarrais I made it clear that the history of the reset is 'the authorship of Paul, but also do not know what is the best option.. Can you link to the meta question here? Thank you

  • Let the admin people understand the situation. http://meta.pt.stackoverflow.com/questions/644/o-que-e-aceitavel-ou-recomendado-ao-editar-uma-resposta-para-incluir-conteudo-de

  • @Thiagoarrais decided to delete the part of reset, and leaving only the link to the @paulomartinhago response ;) seems fairer

Show 2 more comments

41

You can do it this way when you eliminate the activities done in the Stage:

git reset HEAD~1 --hard

or to return to activities:

git reset HEAD~1 --soft

10

After fixing the files run a

git add

and then do:

git commit --amend

Actually this command will redo the last commit

7

You can use this command:

git reset --soft HEAD~1

0

If your last commit is your first commit (initial commit) execute the command git update-ref -d HEAD to retrieve changes from your working tree and execute a new commit

-2

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

For example:

Giving a git log, you’ll see the last commits you’ve done and a hash identifier of it. See:

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

Debugando

Soon, you’ll have to identify in the list, the commit before the one you want to undo. So, just give git checkout:

git checkout c67f03af1701f5c8e47319ae5ad6fc7a2a38151f
  • 4

    This does not answer the' question: checkout a previous commit, will not undo the latest commit.

  • 3

    Also, if you check out a previous commit, and then do a new commit, you’ll get one detached head. This means that any commit made will not belong to any branch.

  • 1

    Um... I hadn’t thought of that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.