Logo
Published on

Git FAQ: How to Undo a Commit to a Branch

Authors
  • avatar
    Name
    Mike Tsamis
    Twitter

Suppose you accidently committed bad code to a branch that you want to revert. To undo your most recent commit, start by running:

git reset HEAD~

This command will unstage your last commit so that you can make any necessary corrections to the files last committed. After making your edits, you can then run:

git add .

to stage all of the modified/new files that you’d like to commit. Finally, to commit your updated changes, simply run this command:

git commit -c ORIG_HEAD   

This will allow you to edit your last commit message before committing your code.

TL;DR

git reset HEAD~

then make the corrections you need to make to the file(s)

git add .
git commit -c ORIG_HEAD

Related Articles