Mercurial: roll back an “hg commit --amend”.

倾然丶 夕夏残阳落幕 提交于 2019-12-20 17:37:43

问题


I accidentally did a "hg commit --amend" instead of just a commit. How can I roll back the commit to before the amend?


回答1:


NOTE: This answer is now deprecated. See the answer from @Sorina Sandu instead.


See hg help commit, where it says:

The --amend flag can be used to amend the parent of the working directory with a new commit that contains the changes in the parent in addition to those currently reported by "hg status", if there are any. The old commit is stored in a backup bundle in ".hg/strip-backup" (see "hg help bundle" and "hg help unbundle" on how to restore it).




回答2:


You can use hg reflog (from the journal extension) and hg reset <hash>.

hg reflog -v

should give something like:

<old-hash> -> <new-hash> <user> <timestamp>  commit --amend <some-path>

if that is the amend you want to revert, just use:

hg reset <old-hash>

The commit will be reverted to what is previously was and the changes that were amended should now be uncommitted changes (check using hg status and hg diff).




回答3:


  1. Find the latest saved backup in .hg/strip-backup directory
  2. hg unbundle .hg/strip-backup/<latest backup>
  3. Now you should have two heads - one with the amended commit, other one with two commits (first one - old commit before amending, second one caled: "temporary amend commit for (old commit hash)".
  4. if you have histedit extension, you can do hg histedit on it in order to change it (e.g. select edit in order to achieve a state just before the commit, i.e. when you can see all changes using hg diff).

Don't forget to strip the old head.




回答4:


If your version of Mercurial is new enough, I believe you should be able to use the hg unamend command from the uncommit extension that ships with Mercurial. This may require that obsolescence markers are enabled, I'm not sure.

  1. Enable the uncommit extension, add this to your ~/.hgrc:

    [extensions]
    uncommit =
    
  2. Actually run the unamend:

    hg unamend
    


来源:https://stackoverflow.com/questions/16697393/mercurial-roll-back-an-hg-commit-amend

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!