how do you revert 1 or more committed files in mercurial but NOT the entire changeset
Solution 1
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"
Solution 2
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.
Solution 3
hg revert FILE -r REVISION
you can revert it to last commit via
hg revert FILE -r .~1
Solution 4
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)
![gman](https://i.stack.imgur.com/KwGaJ.png?s=256&g=1)
gman
FYI: I no longer answer questions on stack overflow If you have a WebGL question you can try asking in the comments on one of the appropriate sites listed below or on the site's corresponding github issues. I was on the Google Chrome GPU team implementing Chrome's GPU subsystem including WebGL and Pepper 3D. I've shipped over 17 commercial games from Atari 800/Apple 2/Commodore 64 days all the way through PS3 and Xbox 360. Some links to things I've worked on: ThreeJSFundamentals,jsgist.org, jsbenchit.org, webgl-lint, games.greggman.com, github, twgl, WebGLFundamentals, WebGL2Fundamentals, vertexshaderart, happyfuntimes, Servez, unzipit, Virtual-WebGL, react-split-it, dekapng, check-all-the-errors @greggman PS: Any code I've posted on Stack Overflow is public domain / CC0. You do not have to credit me.
Updated on September 18, 2020Comments
-
gman almost 4 years
Say I have files
File_A
,File_B
andFile_C
. I editFile_A
andFile_B
on purpose butFile_C
I just added some debugging code that I don't plan to commit. Then by accident I do ahg commit -m "comment"
How do just revert/rollback/backout
File_C
? Basically I'd be happy to be able to gohg 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
andFile_B
. I can copyFile_C
somewhere,hg update
, then copy it back but that seems lame. Is there a way to do this directly in Mercurial? -
Ry4an Brase over 13 yearsbackout automatically commits. You can skip that line as I did in my answer.
-
Oben Sonne over 13 years@Ry4an:
backout
only commits automatically if the revision to backout is the parent of the working dir - here a simplerollback
, as suggested by VonC, usually does the job. In other cases (which I refer to in may answer) one needs to commit explicitly. However, thanks for the hint, I'll update my answer to distinguish these backout situations. -
Ry4an Brase over 13 yearsah, good to know. I thought it always committed but didn't always merge leaving two heads if the backed out cset wasn't the parent.
-
Oben Sonne over 13 years@Ry4an: Yep, it worked like that (leaving 2 heads) before Mercurial 1.7, now the backout command has a slightly (?) different behavior.
-
surfer01 almost 13 yearsWouldn't this stomp any subsequent commits to the same file?
-
Zorayr almost 9 yearsExample,
$ hg revert -r 7428de Libraries/*
. -
Ian Jones about 7 yearsIf you want to revert from your last commit (most common case) you can do this so you don't need to look up the rev number:
hg revert -r .^ File_C
-
Matt over 6 yearsAfter the backout how do I get the changes back on the local copy? I accidentally committed and pushed changes to a bunch of files whose changes I didn't want to commit. So I backed out the commit, but how do I get those files back to the modified state they were in before my accidental commit?
-
Julien Greard over 5 yearswow, thanks ! Very usefull when you want to revert a specific file during a merge !!
-
kevinji about 5 yearsIf you use zsh with
extendedglob
enabled, you'll need to escape the caret:'.^'
.