Is there a way to stage or unstage (git add
/ git rm
) files only based on what was the type of modification?
Like :
- add/rm only files that are deleted
- add/rm only files that are new
- add/rm only files that are modified
- add/rm only files that are renamed
CodePudding user response:
Based on the other linked answer you can do (in bash)
git diff --name-only --diff-filter=D | xargs git add
to e.g. only add deleted files.
You can use the other diff-filter options for modified, new, renamed files etc. And of course you can swap out git add
for git reset
etc.
--diff-filter=[ACDMRTUXB*]
Select only files that are
A
AddedC
CopiedD
DeletedM
ModifiedR
RenamedT
have their type (mode) changedU
UnmergedX
UnknownB
have had their pairing Broken*
All-or-noneAny combination of the filter characters may be used.
CodePudding user response:
Steven Fontanella has given most of the answer : git diff --diff-filter
will allow you to list files which are already tracked by git, or that have already been git add
ed.
To list files that are on disk but not added yet, you will need to use git ls-files
:
git ls-files --exclude-standard -o
Also, for those cases where files git
detects a renaming, you will want to either process the specific output of git diff, or use the --no-renames
option to list such files as a deletion an added file.