eshirvana
eshirvana

Reputation: 24568

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

Is there a way to stage or unstage (git add / git rm) files only based on what was the type of modification?

Like :

Upvotes: 1

Views: 210

Answers (2)

LeGEC
LeGEC

Reputation: 51850

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.

Upvotes: 2

Steven Fontanella
Steven Fontanella

Reputation: 784

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.

Upvotes: 2

Related Questions