'Stage only deleted files with git add
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?
Solution 1:[1]
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:[2]
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:[3]
For PowerShell
git ls-files --deleted | % {git add $_}
Solution 4:[4]
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:[5]
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:[6]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Steve |
Solution 2 | Mark Adelsberger |
Solution 3 | pavol.kutaj |
Solution 4 | |
Solution 5 | august0490 |
Solution 6 | DimP |