Search code examples
mercurial

Mercurial: how to undo last unpushed commit?


I have accidentally made a commit to my local repository. To be more specific, I committed changes to lots of files all at once, when I meant to commit them one at a time.

How do I undo this commit and get my working copy back to the state it was before the commit so I can do what I should have done in the first place?

I have not pushed or pulled or anything else since the commit.


You may suspect this is a duplicate, so I will explain why the following questions are different, or do not answer my question:

Mercurial undo last commit

An answer to this one states that you can do this with hg commit --amend, but does not explain how or give an example of doing this. The mercurail help does not spell it out for me either.

How do you "rollback" last commit on Mercurial?

States to use hg rollback. This command is apparently deprecated, I tried using it anyway, but I got the messge: no rollback information available. A shame this doesn't work as this would be a really intuitive way to achieve what I want.


Solution

  • Based on information from this question:

    hg strip --keep --rev .
    --keep: do not modify working directory during strip
    --rev . (the dot denotes the last commit. read sid0's answer regarding descendants)


    For people more familiar with git language, you are looking for git reset --mixed HEAD^

    hard which would discard your changes, making your work "disappear" (I assume that is not an option)

    soft would undo the commit, but keep previously committed files indexed (that is, tracked)

    mixed keeps your changed files in place, but tells the index to undo the commit. in git-speak: git st would say Changes not staged for commit


    See also git-reset docs , Difference git reset soft/mixed, git/hg Command equivalence table