Stage only deleted files with git add
Asked Answered
S

8

81

I have run git status and see several modified files and several deleted files.

Is it possible to stage only deleted or only modified files?

Saxena answered 1/4, 2017 at 19:59 Comment(1)
git status also tells you how to stage and unstange the new/modified/deleted files.Osric
L
146

If you have a mix of modified and deleted files and only want to stage deleted files to the index, you can use git ls-files as a filter.

git ls-files --deleted | xargs git add

If you only want this to apply to part of the file tree, give one or more subdirectories as arguments to ls-files:

git ls-files --deleted -- lib/foo | xargs git add

To do the same for only modified files, use the --modified (-m) option instead of --deleted (-d).

Ladanum answered 30/8, 2017 at 11:36 Comment(7)
If the names of the deleted files contain spaces you should pass the parameter -d "\n" to xargs.Clonus
You could also use git ls-files -z --deleted | xargs -0 git add for filenames with special characters (even newlines).Ladanum
Thanks steve - Special characters and space in filenamesKoontz
In case you want to add this as an alias to .gitconfig, here's a caveat: you must stringify the command and prefix with it ! like this: sd = !"git ls-files --deleted | xargs git add"Coati
To account for spaces in the filenames, use git ls-files --deleted | xargs -d '\n' git add --allAdoptive
Windows Powershell I got: xargs : The term 'xargs' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.Erupt
🤩 Powerful stuff there! First I have heard of ls-files.Scarbrough
R
11

For PowerShell

git ls-files --deleted | % {git add $_}
Rumanian answered 16/7, 2020 at 10:46 Comment(0)
R
10

Same as the @steve answer, but adding a little change:

Add --all to the end of the command to add all the files returned by the ls-files command to the index

git ls-files --deleted | xargs git add --all
Reenareenforce answered 31/1, 2019 at 22:7 Comment(0)
P
5

For all the love ls-files is getting here, it seems to me

git add --all $(git diff --diff-filter=D --name-only)

is more straightforward.

Palingenesis answered 16/7, 2019 at 22:50 Comment(1)
This does not work if a path needs to be quoted.Lacagnia
R
3

Another way:

git diff --name-only --diff-filter=D | sed 's| |\\ |g' | xargs git add

I use sed here because the paths could have whitespace characters.

Rilda answered 11/8, 2021 at 3:11 Comment(1)
This works on OSX!Tend
C
0

As an alternative to the accepted answer, you could use the interactive mode git add -i, then select 2 to update which files you want to stage and pick only the deleted ones (for example, use a range 1-30).

It's easier to remember sometimes.

Crankcase answered 25/4, 2022 at 11:15 Comment(0)
S
0

You can use this command to stage only the deleted files

git diff --diff-filter=D --name-only -z | xargs -0 git add

Hope it helps!

Sterile answered 21/7, 2023 at 19:48 Comment(0)
D
0

To do it with just core commands, built for scripting:

git diff-files -z --diff-filter=D --name-only | git update-index -z --remove --stdin
Depress answered 21/7, 2023 at 19:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.