Stage only deleted files with git add

GitGit AddGit Stage

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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

Solution 3 - Git

For PowerShell

git ls-files --deleted | % {git add $_}

Solution 4 - Git

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.

Solution 5 - Git

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.

Solution 6 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrokView Question on Stackoverflow
Solution 1 - GitSteveView Answer on Stackoverflow
Solution 2 - GitEduardo PerezView Answer on Stackoverflow
Solution 3 - Gitpavol.kutajView Answer on Stackoverflow
Solution 4 - GitMark AdelsbergerView Answer on Stackoverflow
Solution 5 - Gitaugust0490View Answer on Stackoverflow
Solution 6 - GitDimPView Answer on Stackoverflow