Suppose that I have made some changes in the working directory and accidentally marked several files (that include some of the modified ones) for removal. How do I unmark the files for removal without losing the changes I have made?
Just hg add
the files.
I don't know why you're getting some many answers that modify the working directory. If you've accidentally marked some files for removal you can undo it with add.
ry4an@four:~/hgtest$ hg status --all
M another_file
C a_file
ry4an@four:~/hgtest$ hg remove --after --force *
ry4an@four:~/hgtest$ hg status --all
R a_file
R another_file
ry4an@four:~/hgtest$ hg add *
ry4an@four:~/hgtest$ hg status --all
M another_file
C a_file
That said, don't use --force
with hg remove
or ever really. Also try to get in the habit of using hg forget
instead of hg remove --after
,
hg remove [file]
on an unmodified file removes the file from the filesystem. In this case, hg add [file]
fails and to add it back you have to do hg revert [file]
. hg add [file]
is the correct answer for files that have been modified prior to removal. –
Snout hg forget
is always a better option. Leave the deletion to good old (non-mercurial) rm
. –
Door Team -> Revert
would restore the old version of the file and override your changes. So you should backup your changes before doing Revert
. (sorry - found this is the best place to put this info when googleing) –
Pharyngo hg forget
does not preserve the file history, at least for me it didn't. hg revert -a
sorted that out and the file history was preserved. –
Lui there are two options using hg revert :
hg revert -a
which will go back to the previous revision and put all your changes in new files with .orig appended to the names
hg revert [names of files to unremove] to just revert those files
i'd probably go with the latter
hg revert
I'm pretty sure Mercurial even makes backups of your changes by default.
hg revert
on a file I indirectly removed with hg mv
. It didn't did NOT store my changes, and reverted back to the copy from the last commit. –
Palenque hg revert
worked perfectly to undo that change. –
Pomeranian hg add
is the better and easier fix. –
Door unamend
before doing revert
and it works! done it few times.. –
Narrows If the file exists, (likely if you've marked it for removal with hg forget
or if you've modified it then hg remove
d it), do hg add [file]
to add it back with any changes made after the last commit and before forgetting the file.
If the file does not exist (likely if the file was unmodified and you've marked the file for removal using hg remove
), do hg revert [file]
to revert it back to its state in the parent of the working directory.
I had the exact same problem. hg add
is the inverse to hg forget
(just as the opposite is true). However, attempting to re-add the directory itself did not work. Instead, I had to use hg add
on each file:
hg st | egrep "^R" | sed -e "s/R //" | xargs hg add
Hope that helps. Note that in my case, there was nothing I legitimately wanted to remove. If you have files you definitely want to remove, adjust the grep accordingly.
Following your comment to jk, I checked hg forget
. It seems to be just a shortcut for hg remove -Af
, meaning that this is the real opposite of hg add
.
Following that, if you've used hg remove -Af
, then you should be able to revert that using hg add
(I just tried it and seems to work).
The markers are stored in .hg/dirstate file. All you need to do i to get a one from before issuing hg remove -Af
. It may look like this (not tested):
hg clone bad-repo orig-repo
cp orig-repo/.hg/dirstate bad-repo/.hg/dirstate
cd bad-repo
hg status
The last command should show the status from before removing files.
I removed a bunch of unmodified files:
hg remove *
This is what I had to do to get them back:
hg revert --all
Nothing else worked. Not hg add
not hg add *
nor hg revert *
© 2022 - 2024 — McMap. All rights reserved.