linux
Git rm -vs- rm
git rm
-vs- rm
If working on a project you removed a file using:
you may be faced with a message similar to this when using git and issing the command
rm <file>
:
git status
# Changes not staged for commit:
# (use "git add/rm <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# deleted: parsezip.php
# deleted: parsezipagent.php
#
What to do if I deleted my file when it was under git source control?
First, stay calm if git is new to you. To delete this file and have it also be removed from your branch in your local repository, you must again check it out:
$ git checkout parsezip.php parsezipagent.php
We used the
to get two files for our money.
git checkout <file> <file>
Your files may be back now. Run
to see. And if you wish to go ahead and delete them from your repository you need but 13 characters:
git status
$ ^checkout^rm^
And after that if you run
the files will be gone, and your working directory will be clean again!
git commit
Wait, what?
Using a handy replacement technique that acts upon the previous history item,
, the top hat character separates "a", the previous command's string to replace with "b", the desired replacement. And so now, instead of fixing our accident of
^a^b^
, we do a one-two punch on the problem with fewer characters than you can shake a stick at.
rm <file>
$ echo hello;
hello
$ ^hello^world^
echo world;
world
Note when using the top hat history replacement technique to run a previous command with a string replacement that the final command that will be run is printed to the screen before any output from that command. That's why the output immediately after
is the actual command
$ ^hello^world^
which then, in turn, echoes
echo world;
to end the interaction.
world
Deal with "changed but not updated" "deleted" files in git
In short, and in the future, try not to delete files using
if the files are under git source control. Instead, one should
rm <file>
so as to not create the
git rm <file>
response in "changed but not updated" where one or more files have been "deleted".
git status
Aw man, i forgot to
git rm
my files.
git rm
There is a great StackOverflow answer regarding "deleted files from 'changed but not updated' in git". The solution:
$ git ls-files --deleted -z | xargs -0 git rm
$ git rm $(git ls-files --deleted)
Last Updated: 2014-08-04 15:47:45