开发者

how do you revert 1 or more committed files in mercurial but NOT the entire changeset

开发者 https://www.devze.com 2023-02-13 06:34 出处:网络
Say I have files File_A, File_B and File_C. I edit File_A and File_B on purpose but File_C I just added some debugging code that I don\'t plan to commit. Then by accident I do a

Say I have files File_A, File_B and File_C. I edit File_A and File_B on purpose but File_C I just added some debugging code that I don't plan to commit. Then by accident I do a

hg commit -m "comment"

How do just revert/rollback/backout File_C? Basically I'开发者_C百科d be happy to be able to go

hg update -r <oneRevBack> File_C
hg commit -m "put C back"

but update doesn't take a filter AFAIK so it's also going to revert File_A and File_B. I can copy File_C somewhere, hg update, then copy it back but that seems lame. Is there a way to do this directly in Mercurial?


The exact command you wanted to use would have worked if you used hg revert instead of hg update

hg revert -r .^ File_C
hg commit -m "put C back"


In most cases VonC's solution is just perfect. However, if rollback is no option (e.g. because the commit in question is not the last one or because you did a complex partial commit), then you can do a new commit with the reversed diff of the unwanted changes to File_C:

hg diff -c REVISION --reverse File_C | hg import -m "Revert changes to File_C" -

Where REVISION refers to the commit where File_C has been committed accidentally. The first command produces the reversed diff for the file in question. It's output is piped to hg import (don't forget the trailing -).

UPDATE: You can also use the backout command:

hg backout -I File_C REVISION
hg commit -m "Revert changes of File_C in REVISION" File_C

The backout command changes your working copy in that it undoes the changes to File_C commited in rev. REVISION. Afterwards the revert is committed explicitly.


You could use hg rollback to undo your last commit, and then re-do it again, this time without the file C.
(as mentioned in Mercurial FAQ, and also in this FAQ entry where it is noted that "there is no backup when you rollback a transaction", so use it with caution)


hg revert FILE -r REVISION

you can revert it to last commit via

hg revert FILE -r .~1


You can also use interactive mode:

hg revert -i -r .^

Select what you want reverted and confirm.

hg amend
0

精彩评论

暂无评论...
验证码 换一张
取 消