43

I have a bunch of files deleted from the fs and listed as deleted in git status.

How can I stage this changes faster then running git rm for each file?

2

7 Answers 7

89

You can do this with:

git ls-files --deleted -z | xargs -0 git rm

Whenever this question is asked, people suggest git add -u, but the problem with that answer is that it also stages other modifications in your working copy, not just deletions. That might be OK in many situations, but if you want to just stage the deletion of files that have been deleted from the working copy, the suggestion I've made is more precise.

There's actually a section of the git rm documentation that discusses how to do what you want - I believe that the command suggested in the "Other ways" section is equivalent to what I've suggested here.

5
  • 1
    +1. Just out of interest, why the -z/-0? Looks as though git ls-files --deleted | xargs git rm will work just as well? Commented May 22, 2011 at 21:13
  • 4
    @Simon Whitaker: that makes the command work even if there are spaces or newlines in the filenames, by saying to output the listed files separated with NUL bytes rather than newlines. Commented May 22, 2011 at 21:16
  • 3
    Thanks! This makes a nice alias: 'rd = "!git ls-files --deleted -z | xargs -0 git rm"' (put that in .gitconfig without the outer single quotes, under [alias]. Then just run 'git rd' next time).
    – Sean
    Commented Jan 23, 2013 at 8:51
  • This does not work on Windows unless you have Cygwin with xargs installed Commented Jan 6, 2015 at 3:04
  • On windows w/out Cygwin you can use powershell like so: git rm $(git ls-files --deleted ). (Note: this actually works in Bash too assuming a reasonably sized changeset). Commented May 22, 2015 at 13:29
15

Use the -u-flag: man git-add

git add -u .
2
7

You can use git rm --cached "path/to/file" to stage a single deleted file.

Use git rm -r --cached -- "path/to/directory" to stage a complete deleted directory.

2

git commit -a would stage deleted files (as well as modified) and prompt you for your commit message. I usually execute git commit -av (verbose) to also see diffs of modified files.

From the manual page:

by using the -a switch with the commit command to automatically "add" changes from all known files (i.e. all files that are already listed in the index) and to automatically "rm" files in the index that have been removed from the working tree, and then perform the actual commit;

1

git add -A

will do the job for you

1
  • 2
    The downside is that it adds untracked files too. Isn't it? Commented May 22, 2011 at 21:03
0

On windows w/out Cygwin or powershell you can use like so:

git ls-files --deleted -z | for /f %f in ('more') do git rm --cached "%f"
0

If the deleted file was in a parent folder that exists then you can simply:

git add parent_folder_path/*

If that does not work try adding the parent of the parent:

git add parent_of_parent_path/parent_folder/*

If this is not what you want, see other's answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.