Alex Rivera | Logout

How do I undo the most recent local commits in Git?

Asked 2009-05-29T18:09:14.627
27210

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

How do I undo those commits from the local repository?

Edit
Report

2 Answers

860

Use git revert <commit-id>.

To get the commit ID, just use git log.

answered 2012-05-25T16:04:29.480
186

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.

answered 2013-05-13T17:12:25.717

Your Answer