How to undo the last Git commit?
32
I accidentally added the wrong directory containing my files in Git. Instead of adding a .java file, I added the directory containing the .class file. How can I undo this action?
improve this question | comment
Russell Hane Created at: 2013-11-13 17:07:47 UTC By Russell Hane
By the number of upvotes here and in the answers, it looks like git needs to add a git commit --undo option - Russ Hayes
@KyleHeironimus: Easy enough: git config --global alias.undo-commit 'reset --soft HEAD^' Then just type git undo-commit as needed. - Ryan Dare
Starting with Git v1.8.4, all the answers below that use HEAD or head can now use @ in place of HEAD instead. See this answer (last section) to learn why you can do that. - Guest
git reset --soft HEAD~1 - Camille Kassulke
Amend the commit, and use a gitignore to stop it happening again. - Gaetano Steuber
20 Answers
0
Use Source tree (graphical tool for git) to see your comments and Tree and manually you can reset it directly by right clicking it.
0
If you are planning undoing a local commit entirely, whatever you changes 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 undo your commit, but you want changes you did on the commit into the staging area (before commit just like after git add called the staging area) then do the following command.

git reset --soft HEAD^1


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

git reset HEAD


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

More
0
Undoing a commit is a little scary if you don't know how it works.  But it's actually amazingly easy if you do understand.

Say you have this, where C is your HEAD and (F) is the state of your files.

   (F)
A-B-C
    ↑
  master


You want to nuke commit C and never see it again.  You do this:

git reset --hard HEAD~1


The result is:

 (F)
A-B
  ↑
master


Now B is the HEAD.  Because you used --hard, your files are reset to their state at commit B.

Ah, but suppose commit C wasn't a disaster, but just a bit off.  You want to undo the commit but keep your changes for a bit of editing before you do a better commit.  Starting again from here, with C as your HEAD:

   (F)
A-B-C
    ↑
  master


You can do this, leaving off the --hard:

git reset HEAD~1


In this case the result is:

   (F)
A-B-C
  ↑
master


In both cases, HEAD is just a pointer to the latest commit.  When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit.  But (unless you use --hard) you leave your files as they were.  So now git status shows the changes you had checked into C.  You haven't lost a thing!

For the lightest touch, you can even undo your commit but leave your files and your index:

git reset --soft HEAD~1


This not only leaves your files alone, it even leaves your index alone.  When you do git status, you'll see that the same files are in the index as before.  In fact, right after this command, you could do git commit and you'd be redoing the same commit you just had.

One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all?  Tough luck, right?

Nope, there's still a way to get it back.  Type git reflog and you'll see a list of (partial) commit shas that you've moved around in.  Find the commit you destroyed, and do this:

git checkout -b someNewBranchName shaYouDestroyed


You've now resurrected that commit.  Commits don't actually get destroyed in Git for some 90 days, so you can usually go back and rescue one you didn't mean to get rid of.
0
Add/remove files to get things the way you want:

git rm classdir
git add sourcedir


Then amend the commit:

git commit --amend


The previous, erroneous commit will be edited to reflect the new index state - in other words, it'll be like you never made the mistake in the first place :)

Note that you should only do this if you haven't pushed yet. If you have pushed, then you'll just have to commit a fix normally.
0
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

Lets 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 on accident'
git log
    commit 102: restoring the file I removed on accident
    commit 101: removing a file we dont need
    commit 100: adding a file that we need


For more info, check out Git Book - Reset, Checkout and Revert
0
git rm yourfiles/*.class
git commit -a -m "deleted all class files in folder 'yourfiles'"


or

git reset --hard HEAD~1


The hard reset to HEAD-1 will set your working copy to the state of the commit before your wrong commit.
0
Replace the files in the index:

git rm --cached file.class
git add file.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'


You may also want to add *.class to a gitignore to stop this happening again.
0
Use git revert commit-id

To get the commit ID, just use git log
0
If you have Git Extras installed, you can run git undo to undo the latest commit. git undo 3 will undo the last 3 commits.
0
I wanted to undo the lastest 5 commits in our shared repository. I looked up the revision id that I wanted to rollback to. Then I typed in the following.

prompt> git reset --hard 5a7404742c85
HEAD is now at 5a74047 Added one more page to catalogue
prompt> git push origin master --force
Total 0 (delta 0), reused 0 (delta 0)
remote: bb/acl: neoneye is allowed. accepted payload.
To git@bitbucket.org:thecompany/prometheus.git
 + 09a6480...5a74047 master -> master (forced update)
prompt>

0
http://git-scm.com/docs/git-reset

Undo a commit and redo


$ git commit ...              (1)
$ git reset --soft "HEAD^"    (2)
$ edit                        (3)
$ git add ....                (4)
$ git commit -c ORIG_HEAD     (5)

  
  This is what you want to undo
  This is most often done when you remembered what you just committed is incomplete, or you misspelled your commit message, or both. Leaves working tree as it was before "reset". (The quotes are required if you use zsh)
  Make corrections to working tree files.
  Stage changes for commit.
  "reset" copies the old head to .git/ORIG_HEAD; redo the commit by starting with its log message. If you do not need to edit the message further, you can give -C option instead.
  
0
I prefer to use git rebase for this job, because a nice list pops up where I can choose the commits to get rid of. It might not be as direct as some other answers here, but it just feels "right".

Choose how many commits you want to list, then invoke like this

git rebase -i HEAD~3


Sample list

pick aa28ba7 Sanity check for RtmpSrv port
pick c26c541 RtmpSrv version option
pick 58d6909 Better URL decoding support


Then git will remove commits for any line that you remove.
0
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

0
how to fix the previous local commit

Use git-gui (or similar) to perform a git commit --amend.  From the GUI you can add or remove individual files from the commit.  You can also modify the commit message.

how to undo the previous local commit

Just reset your branch to the previous location (e.g. using gitk or git rebase), then reapply your changes from a saved copy.  After garbage collection in your local repo, it will be like the unwanted commit never happened.  To do all of that in a single command, use git reset HEAD~1.

Word of warning: Careless use of git reset is a good way to get your working copy into a confusing state.  I recommend that Git novices avoid this if they can.

how to undo a public commit

Perform a reverse cherry pick (git-revert) to undo the changes.

If you haven't yet pulled other changes onto your branch, you can simply do...

git revert --no-edit HEAD


Then push your updated branch to the shared repo.
0
"Reset the working tree to the last commit"

git reset --hard HEAD^ 


"Clean unknown files from the working tree"

git clean    


see -http://jonas.nitro.dk/git/quick-reference.html

NOTE: This command will delete your previous commit, so use with caution! git reset --hard is safer –
0
On SourceTree (GUI for GitHub), you may Right-Click the commit and do a 'Reverse Commit'. This should undo your changes.

On terminal: 
You may alternatively use  :    

git revert


or

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

0
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.
0
In my case I accidentally committed some files I did not want to. So I did the following and it worked:

git reset --soft HEAD^
git rm --cached [files you do not need]
git add [files you need]
git commit -c ORIG_HEAD


Verify the results with gitk or git log --stat
0
first run: 

git reflog

It will show you all the possible actions you have performed on your repo e.g. commit, merge, pull etc

then:

git reset --hard ActionIdFromRefLog
0
type git log and find the last commit hash code and then give
git reset 
Your Answer