How do I undo the most recent commits in Git ?

How do I undo the most recent commits in Git ?

Asked on December 14, 2018 in Git.
Add Comment

  • 3 Answer(s)

    Undo a commit and redo

    $ git commit -m "Something terribly misguided" # (1)
    $ git reset HEAD~ # (2)
    << edit files as necessary >> # (3)
    $ git add ... # (4)
    $ git commit -c ORIG_HEAD # (5)
    • This is what you want to undo
    • This leaves your working tree the state of your files on disk unchanged but undoes the commit and leaves the changes you committed unstaged  so they will appear as “Changes not staged for commit” in git status, and you will need to add them again before committing. If you only want to add more changes to the previous commit, or change the commit message 1, you could use git reset –soft HEAD~ instead, which is like git reset HEAD~ where HEAD~ is the same as HEAD~1 but leaves your existing changes staged.
    • Make corrections to working tree files.
    • git add  anything that you want to include in your new commit.

                  Commit the changes, reusing the early commit message. reset copied the old head to .git/ORIG_HEAD; commit with –c ORIG_HEAD which initially contains the log message from the early commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

                 Beware however that if you have added any new changes to the index, using commit –amend will add them to your previous commit.

                 If the code is already pushed to your server and you have permissions to overwrite history rebase then:

    git push origin master --force

    You can  also try this answer:

                The above answer will show you git reflog which is used to find out what is the SHA-1 which you wish to revert to. Once you found the point to which you wish to undo to use the sequence of commands as explained above.


         whatever, that  do not need to reset to an earlier commit if you just made a mistake in your commit message. The easier option is to git reset to upstage any changes you’ve made since and then git commit –amend, which will open your default commit message editor pre-populated with the last commit message.

    Answered on December 14, 2018.
    Add Comment

    Try this solution :

    Undoing a commit is a little scary if you don’t know how it works. But it’s actually amazingly easy if you do understand.

    Say you have this, where C is your HEAD and (F) is the state of your files.


    You want to nuke commit C and never see it again. You do this:

    git reset --hard HEAD~1

    The result is:


    Now B is the HEAD. Because you used –hard, your files are reset to their state at commit B.

    Ah, but suppose commit C wasn’t a disaster, but just a bit off. You want to undo the commit but keep your changes for a bit of editing before you do a better commit. Starting again from here, with C as your HEAD:


    You can do this, leaving off the –hard:

    git reset HEAD~1

    In this case the result is:


    In both cases, HEAD is just a pointer to the latest commit. When you do a git reset HEAD~1, you tell Git to move the HEAD pointer back one commit. But unless you use –hard you leave your files as they were. So now git status shows the changes you had checked into C. 

    For the lightest touch, you can even undo your commit 

    git reset --soft HEAD~1

    This not only leaves your files alone, it even leaves your index alone. When you do git status, you will see that the same files are in the index as before. In fact, right after this command, you could do git commit and you’d be redoing the same commit you just had.

    One more thing: Suppose you destroy a commit as in the first example, but then discover you needed it after all.

    No way, there is still a way to get it back. Type git reflog and you’ll see a list of (partial) commit shas that is, hashes that you’ve moved around in. Find the commit you destroyed,

    git checkout -b someNewBranchName shaYouDestroyed

    You have now resurrected that commit. Commits don’t actually get destroyed in Git

    Answered on December 14, 2018.
    Add Comment

    You can try this:

    This took me a while to figure out, so maybe this will help someone…

    There are two ways to “undo” your last commit, depending on whether or not you have already made your commit public pushed to your remote repository:

     Undo a local commit

    Let is say I committed locally, but now want to remove that commit.

    git log
    commit 101: bad commit # latest commit, this would be called 'HEAD'
    commit 100: good commit # second to last commit, this is the one we want

    To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD:

    git reset --soft HEAD^ # use --soft if you want to keep your changes
    git reset --hard HEAD^ # use --hard if you don't care about keeping the changes you made

     Undo a public commit:

    You have already made your commits public, you will want to create a new commit which will “revert” the changes you made in your previous commit.

    git revert HEAD

    Your changes will now be reverted and ready to commit:

    git commit -m 'restoring the file I removed by accident'
    git log
    commit 102: restoring the file I removed by accident
    commit 101: removing a file we don't need
    commit 100: adding a file that we need



    Answered on December 14, 2018.
    Add Comment

  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.