Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add and commit removals made with "rm" instead of "git rm"?

I deleted a bunch of files and directories from a Git repository using rm, the Finder, etc.

I'm looking for a Git command that'll record these to the index as marked for removal, as if I had called git rm on them.

I understand git add -u will do this, along with a bunch of other things. I'd like my command to exclusively handle removals.

like image 816
kch Avatar asked Dec 06 '09 21:12

kch


People also ask

What is difference between git rm and rm?

The git rm command removes the file from both the git repository and the local file system. The rm command, on the other hand, only removes the file from the file system.

Do I need to commit git rm?

After doing git rm you need to do git commit . Git says that Everything up-to-date is because you have not created a commit with this file removed. For example if you do git reset --hard you'll see that your file is back. In general git rm is no different from git add .

Can you undo git rm?

A reset will revert the current staging index and working directory back to the HEAD commit. This will undo a git rm . git checkout . A checkout will have the same effect and restore the latest version of a file from HEAD .

What is git add rm?

The command git rm is, naturally, the converse of git add. It removes a file from both the repository and the working directory. However, because removing a file tends to be more problematic (if something goes wrong) than adding a file, Git treats the removal of a file with a bit more care.


2 Answers

Without spaces in filenames:

$ git rm `git ls-files -d`

More robust:

$ git ls-files -z -d | xargs -0 --no-run-if-empty git rm
like image 147
Greg Bacon Avatar answered Oct 18 '22 14:10

Greg Bacon


Take a look what Jonio C Hamano wrote in "Re: [PATCH 1/2] Documentation: 'git add -A' can remove files" post at git mailing list, namely that this question looks like XY problem (you are asking about assumed solution Y to the problem, instead about the problem X itself). The solution to problem (if it is really "XY problem" situation) might be:

  • git commit -a, which would automatically pick up deletions, committing current state of tracked files in working directory

  • git add -A, which would add not ignored untracked files and remove no longer existing files, e.g. if you want to create commit from sideways update of working directory, e.g. unpacking a snapshot or result of rsync.

Nevertheless if what you ask is a problem (and not solution), then as you can see from other answers there are tools in place to do it.

like image 26
Jakub Narębski Avatar answered Oct 18 '22 15:10

Jakub Narębski