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?
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?
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
).
-d "\n"
to xargs. –
Clonus git ls-files -z --deleted | xargs -0 git add
for filenames with special characters (even newlines). –
Ladanum .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 git ls-files --deleted | xargs -d '\n' git add --all
–
Adoptive 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 ls-files
. –
Scarbrough For PowerShell
git ls-files --deleted | % {git add $_}
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
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.
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.
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.
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!
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
© 2022 - 2024 — McMap. All rights reserved.
git status
also tells you how to stage and unstange the new/modified/deleted files. – Osric