tags:

views:

4841

answers:

6

OK, say I have a git repo that I have deleted four files from using rm (not "git rm"), and my git status looks like this:

#    deleted:    file1.txt
#    deleted:    file2.txt
#    deleted:    file3.txt
#    deleted:    file4.txt

How do I remove these files from git without having to manually go through and add each file like this:

git rm file1 file2 file3 file4

Ideally, I'm looking for something that works in the same way that git add . does, if that's possible.

+58  A: 

You can use

git add -u

To add the deleted files to the staging area, then commit them

git commit -m "Deleted files manually"
Cody Caughlan
A: 

something like

git-status | sed -s "s/^.*deleted: //" | xargs git rm

may do it.

Jeremy French
+3  A: 

If those are the only changes, you can simply do

git commit -a

to commit all changes. That will include deleted files.

SpoonMeiser
Not sure why I got down-voted; git does a good job of identifying files that have been deleted, even if you've not explicitly told it by using git rm.
SpoonMeiser
+16  A: 

You're probably looking for -A:

git add -A

this is similar to git add -u, but also adds new files. This is roughly the equivalent of hg's addremove command (although the move detection is automatic).

Dustin
+11  A: 

By using git-add with '--all' or '--update' options you may get more than you wanted. New and/or modified files will also be added to the index. I have a bash alias setup for when I want to remove deleted files from git without touching other files:

alias grma='git ls-files --deleted | xargs git rm'

All files that have been removed from the file system are added to the index as deleted.

zobie
A: 

None of the flags to git-add will only stage removed files; if all you have modified are deleted files, then you're fine, but otherwise, you need to run git-status and parse the output.

Working off of Jeremy's answer, this is what I got:

git status |  sed -s "s/^.*deleted: //" | grep "\(\#\|commit\)" -v | xargs git rm
  1. Get status of files.
  2. For deleted files, isolate the name of the file.
  3. Remove all the lines that start with #s, as well as a status line that had the word "deleted" in it; I don't remember what it was, exactly, and it's not there any longer, so you may have to modify this for different situations. I think grouping of expressions might be a GNU-specific feature, so if you're not using gnutils, you may have to add multiple grep -v lines.
  4. Pass the files to git rm.

Sticking this in a shell alias now...

Xiong Chiamiov