0

Is there a way to remove all files from the index that are deleted ?

I usually use git add . -u to add all files to my staging zone that are tracked and modified. How would I do a command like git rm . -u ?

dallonsi
  • 1,299
  • 1
  • 8
  • 29
  • Are you trying to have `git` recognize that a file was deleted? `git` already tracks that. – Ali Samji Feb 25 '21 at 23:17
  • 1
    Does this answer your question? [Staging Deleted files](https://stackoverflow.com/questions/12373733/staging-deleted-files) – evolutionxbox Feb 25 '21 at 23:24
  • @Ali: no, I mean, when you delete a file, git still tracks it. You need to use `git rm` to definitely remove it from your tracked files. So how can I do `git rm` to all deleted files ? – dallonsi Feb 25 '21 at 23:26
  • @evolutionxbox: oh right, so just `git add deleted_file` would do the same as `git rm deleted_file` ! fantastic – dallonsi Feb 25 '21 at 23:29
  • Does `git rm` not already do what you want? I'm pretty sure it deletes the file and stages the deletion. – evolutionxbox Feb 25 '21 at 23:30
  • yes it does..... But I want to delete ALL files in just one command as `git add . -u` would add ALL tracked and modified files in one command :) – dallonsi Feb 25 '21 at 23:33
  • 1
    So you would like to add all files that are already deleted? Doesn't `git add .` already do that? – evolutionxbox Feb 25 '21 at 23:41

2 Answers2

3

In modern Git, git add -u . already removes from the index any file that you have removed from your working tree:

$ mkdir t1 && cd t1 && git init
Initialized empty Git repository in ...
$ echo example > readme
$ echo somefile > somefile
$ git add .
$ git commit -m initial
[master (root-commit) 95caaab] initial
 2 files changed, 2 insertions(+)
 create mode 100644 readme
 create mode 100644 somefile
$ ls
readme          somefile
$ rm somefile
$ git add -u .
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        deleted:    somefile

If you have an ancient Git, git add -u might not stage these removals—I can't recall the behavior of Git 1.7 here. If so, consider using the --all flag. You could also use git status -s to detect missing work-tree files, which will have SPACED as their two letter status:

$ git restore --staged somefile
$ git status -s
 D somefile

so that git status -s | grep '^ D' produces the list of such files. Others like to use cut and so on to generate a removal command, but I prefer to do:

git status -s >/tmp/x
vim /tmp/x

and then, in the editor, use commands to delete all the non-space-D files, then turn the remaining lines into git rm commands:

:v/^ D/d
:%s/^ D/git rm/

for instance. If some file names have white space, the second vim command would be:

:%s/^ D \(.*\)/git rm "\1"/

Some eyeball inspection will verify that this is indeed what I want to do, after which ZZ or :x exits vim with a file in /tmp/x that I can run using sh /tmp/x. Voila, mission accomplished.

torek
  • 448,244
  • 59
  • 642
  • 775
1

When you want to get brutal, the core commands are always an acceptable option:

git diff-files --diff-filter=D --name-only \ 
| git update-index --remove --stdin
jthill
  • 55,082
  • 5
  • 77
  • 137