TSL
TSL

Reputation: 936

Is there negation syntax for git add's filepattern?

Suppose I want to use git add (or some other command line directive - I cannot use .gitignore) to add all files EXCEPT for a *.hi file. Is there a way to do this? So far I have tried:

git add '!*.hi'

git add '!(*.hi)'

As I understand it, this is how you specify a negation in glob syntax, and it's also how you do it in .gitignore. Yet for both these commands, I receive the error fatal: [pattern] did not match any files. For what it's worth, I'm running these commands from Windows Powershell.

Upvotes: 8

Views: 1085

Answers (3)

CervEd
CervEd

Reputation: 4253

The exclusion pattern is

git add ':!*.hi'

note the colon.

Tested in PowerShell with git version 2.28.0.windows.1

I found this answer to how to use the exclusion pattern in git useful

Upvotes: 5

poke
poke

Reputation: 387765

A pure Git way would be to add them all and then remove those files you don’t want from the index again:

git add .
git rm --cached *.hi

The --cached is required to remove them only from the index; otherwise you would delete those files. Note that git rm will always “add a removal” to the index, so this is probably not what you want in case the files are tracked. Then you should use git reset as Klas Mellbourn suggested in the comments:

git add .
git reset -- *.hi

Upvotes: 4

Klas Mellbourn
Klas Mellbourn

Reputation: 44377

In PowerShell, this works:

git add $(Get-ChildItem -Recurse -Exclude '*.hi')

Upvotes: 1

Related Questions