Alexander Reshytko
Alexander Reshytko

Reputation: 2236

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

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

Upvotes: 9

Views: 1046

Answers (3)

llakala
llakala

Reputation: 1

This is in reply to a previous answer:

Yes, there is! Using only git: git diff --name-only --diff-filter=A -z | git restore --staged -q --pathspec-file-nul --pathspec-from-file=-

This is really great, but git add -AN also stages file deletions, and this command doesn't unstage those file deletions. We can fully reverse the git add -AN command with this command:

cat <(git diff --name-only --diff-filter=A -z) <(git diff --staged --name-only --diff-filter=D -z) | git restore --staged -q --pathspec-file-nul --pathspec-from-file=-

We need to run two different commands and cat them, because we can only see the deleted files when using git diff --staged.

Upvotes: 0

milhnl
milhnl

Reputation: 21

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.

Upvotes: 2

Amber
Amber

Reputation: 526573

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

Upvotes: 4

Related Questions