75

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?

rok
  • 9,403
  • 17
  • 70
  • 126

8 Answers8

134

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).

Steve
  • 6,618
  • 3
  • 44
  • 42
  • 7
    If the names of the deleted files contain spaces you should pass the parameter `-d "\n"` to xargs. – wortwart Jul 17 '18 at 14:33
  • 7
    You could also use `git ls-files -z --deleted | xargs -0 git add` for filenames with special characters (even newlines). – Steve Aug 07 '18 at 21:35
  • Thanks steve - Special characters and space in filenames – Salathiel Genese Dec 26 '18 at 09:18
  • 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"` – Paul Razvan Berg Nov 23 '19 at 16:02
  • 1
    To account for spaces in the filenames, use `git ls-files --deleted | xargs -d '\n' git add --all` – Phillip Dec 06 '19 at 06:24
  • 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.` – cbdeveloper Jan 30 '20 at 07:39
  • Powerful stuff there! First I have heard of `ls-files`. – CodeFinity Nov 30 '21 at 00:31
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
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
Eduardo Perez
  • 131
  • 2
  • 5
8

For PowerShell

git ls-files --deleted | % {git add $_}
pavol.kutaj
  • 401
  • 1
  • 7
  • 14
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.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
2

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.

august0490
  • 777
  • 1
  • 9
  • 15
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.

DimP
  • 216
  • 3
  • 9
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!

dpacman
  • 3,683
  • 2
  • 20
  • 35
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
jthill
  • 55,082
  • 5
  • 77
  • 137