Undo a remove action in Mercurial
Suppose that I have made some changes in the working directory and accidentally marked several files (that include some of the modified ones)开发者_JAVA百科 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
,
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.
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 *
精彩评论