How to get rid of some changeset in Hg?

前端 未结 5 1242
青春惊慌失措
青春惊慌失措 2020-12-29 14:50

Hey, I want to revert some changesets in Hg but I\'m having a hard time doing so.

I commited some changes by accident, so I wanted to undo that. After trying a littl

5条回答
  •  悲&欢浪女
    2020-12-29 15:28

    Here is how to do git reset --soft. Let us assume you have a graph like this:

    ... --- [A] --- [B] --- [X] --- [-X]
    

    where X is the bad commit and -X is the backout you made. You now want to get rid of X and -X while leaving the working copy looking like it did when you committed X. You do

    $ hg update B
    $ hg revert --all --rev X # working copy now looks just like in X
    $ hg strip --force X      # get rid of X and -X
    

    If you want, you can make an alias for this with Mercurial 1.7:

    [alias]
    softreset = !hg update 'p1($1)' &&
                 hg revert --all --rev $1 &&
                 hg strip --force $1
    

    The dirty working copy after hg revert makes it necessary to use hg strip --force. You use this new command as

    $ hg softreset 10
    

    which will remove 10 and any descendents while leaving the changes in 10 in the working copy. You can of course take this further and implement a hg reset command:

    [alias]
    reset = !test "$1" = "--hard" && hg strip $2 || hg softreset $2
    

    The biggest problem with these aliases is the poor error handling. An extension written in Python would be much more robust and maintainable -- perhaps you could make such a "reset" extension and publish it on Bitbucket :)

提交回复
热议问题