Support People In Ukraine
Ukraine was invaded by Russia in the early morning of 24.02.2022, explosions thundered in Ukrainian cities, many civilians died, tens of millions are affected. Our army protects all Europe from the occupiers and it needs help now to restrain one of the biggest armies in the world. Please, donate a small amount.
I Am
Volodymyr Hudyma
<FrontEndDeveloper />
You Are Here: Home/How To Undo The Last Commit In Git?

How To Undo The Last Commit In Git?

August 11, 2021

Table Of Contents

    One of the most common tasks when collaborating with other developers using Git is adding and removing commits.

    While adding commits is a relatively simple task, undoing incorrect or unnecessary commits can cause problems.

    Before you undo a commit, make sure you really need to do so, and not just edit the commit message.

    If you want to edit a commit message, read one of my previous articles that explains how to change a commit message in Git.

    Undo The Last Commit

    To undo the last commit, use one of the following commands:

    git reset --soft HEAD~1

    Or

    git reset --hard HEAD~1

    Or

    git reset HEAD~1 / git reset --mixed HEAD~1

    They are pretty similar, with only one difference - the use of --soft, --hard or --mixed flag.

    --soft

    Does not touch the index file or working tree at all (but resets the head to the previous commit, like all modes).

    This leaves all changed files as "Changes to be committed":

    Git Soft Reset

    --hard

    Resets the index and working tree.

    All changes to tracked files in the working tree for the previous commit are discarded:

    Git Hard Reset

    --mixed

    Mixed is the default option, so if you simply run git reset HEAD~1, this option will be used.

    Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what was not updated:

    Git Mixed Reset

    Undo Multiple Commits

    If you want to revert multiple commits, you can specify either the number of commits or the commit hash you want to revert to.

    Let's say we want to revert the last 3 commits:

    git reset --hard HEAD~3

    Or

    git reset --hard <hash>

    Let's use the first command:

    Git Hard Reset Last 3 Commits

    And the second one:

    Git Hard Reset Last 3 Commits [2]

    Summary

    In this article, we learned how to undo one or more commits in Git by using the git reset command and giving it different flags, depending on whether you want to keep the changes or not.

    Before you undo a commit, make sure you really need to do so, and not just edit the commit message.

    If you want to edit a commit message, read one of my previous articles that explains how to change a commit message in Git.

    Read my other articles about Git:

    Newsletter
    Receive all new posts directly to your e-mail
    No spam, only quality content twice a week
    Let me know what you think about this article
    Click here to write response...