Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove only intent-to-add files from index in git?

Tags:

git

Is there a way to remove from an index with a single command only those files that have been added with -N flag?

like image 235
Alexander Reshytko Avatar asked Oct 21 '12 01:10

Alexander Reshytko


2 Answers

No. Git doesn't keep track of which files were added with -N and which were simply empty.

like image 77
Amber Avatar answered Nov 15 '22 21:11

Amber


Yes, there is! Using only git:

git diff --name-only --diff-filter=A -z \
    | git restore --staged -q --pathspec-file-nul --pathspec-from-file=-

Files that are added without their content with -N show up as 'Added' (A) in the work tree, and that is what is listed with --diff-filter=A. This list is then piped into git restore which removes the intent-to-add.

I used git restore because git reset just resets the whole index if there are no files added with -N (because the pathspec is empty). You can also avoid this problem with

git diff --name-only --diff-filter=A -z | xargs -r0 git reset -q --

which requires GNU xargs.

like image 32
milhnl Avatar answered Nov 15 '22 20:11

milhnl