How do I undo the most recent local commits in Git?
Asked 07 September, 2021
Viewed 1.2K times
  • 62
Votes

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

How can I undo those commits from the local repository?

The only way seems to be to copy the edits in some kind of GUI text editor, then wipe the whole local clone, then re-clone the repository, then re-applying the edits. However,

  • This can cause data loss.
  • It's very hard to do this when only an accidental git commit was run.

Is there a better way?

30 Answer