Home > database >  git add/rm files based on the type of change (of the files)
git add/rm files based on the type of change (of the files)

Time:12-02

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 Added
  • C Copied
  • D Deleted
  • M Modified
  • R Renamed
  • T have their type (mode) changed
  • U Unmerged
  • X Unknown
  • B have had their pairing Broken
  • * All-or-none

Any 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 added.

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.

  • Related