Remove file from a commit in Mercurial
Asked Answered
D

2

55

I have a commit onto which I have amended some files. Some of these files that were part of the amend I do not want in this commit. Is there a way in Mercurial to remove certain files from the commit without losing the changes I have made to them? Thank you.

Steps:

  1. Made some changes
  2. hg commit -m
  3. Made some more changes (some of these file accidentally amended)
  4. hg amend
Drud answered 13/9, 2013 at 1:54 Comment(0)
P
89

Try out:

hg forget somefile.txt
hg commit --amend

If the file was new (i.e. you had used hg add).

If that file already existed try:

cp somefile.txt somefile.txt.bak
hg revert somefile.txt --rev .~1
hg commit --amend

Which is basically telling mercurial to revert the file (somefile.txt) back to the state it was one revision ago (--rev .~1).

Just make sure to back up the file you are reverting before entering the command so that you do not lose your changes. I was under the impression mercurial does this automatically for you, but after testing it quickly I'm not so sure.

Peri answered 16/9, 2013 at 10:2 Comment(3)
Works like a charm. The -X somefile.txt argument does not help if the file was already commited, but reverting the changes to the previous version do the trick.Barbur
I searched before on how to remove a file while amending, and all answered pointed to that it was impossible. This proves them wrong.Tenatenable
TortoiseHG doesn't seem to be capable of removing all changes from a file in an amend, but clearly hg itself has no problem with it.Rickyrico
E
5

hg uncommit somefile.txt does exactly this for me.

Like plain git reset, it removes the change from the commit but leaves the file contents unchanged, so now hg diff shows the change you just uncommitted.

The uncommit command claims to come from the uncommit extension, but may actually be coming from the evolve extension, I admit I'm not 100% sure!

Echolalia answered 10/9, 2020 at 2:35 Comment(1)
Worked pretty fine. But, as you said it keeps the content unchanged, so it is necessary to run hg revert <file> to discard it. And, if your commit is in the middle of a thread, it also requires hg evolve --any afterTruckload

© 2022 - 2024 — McMap. All rights reserved.