Undo a commit in git

I\’ve been using git for a while now. And I\’ve happend upon a pattern: I commit, and then have to undo my commit because I either missed a file or a change that should go with the commit.

So, how do you undo a commit in git..?

But what is git?

If you don\’t know what git is..

Git is a free and open source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.

Eureka! The solution:

I searched for a solution and happened upon a solution on Stock Overflow.

Undo a commit and redo

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

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\”.

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.

This has been super helpful.

One thought on “Undo a commit in git”

  1. There is a shorter way for doing this.

    git commit # the "broken" commit
    
    git add missing_file
    git commit --amend -m "New commit message" missing_file

    which is according to git docs rough equivalent for your solution.

Leave a Reply

Your email address will not be published. Required fields are marked *