Git add files which do not match a pattern
Asked Answered
S

1

3

In Git, while adding files to a commit, we can add all files matching a pattern like so:

git add **Component**

I find this feature pretty useful to quickly add lots of files with similar names.

For example, If all my files are named based on components, then I can add all changes I did to a component quickly.

Similarly, is there a way in git to add all files to commit excluding files matching a pattern?

Something like:

git add *.java --exclude **Component1**

So that I can all my java file changes except the changes that I made to the files of component1?

Stillness answered 3/5, 2019 at 21:14 Comment(2)
that sounds more like something you would solve with bash to call xargs. Something like blah blah | produce list of files | xargs git addCrossly
Sounds like a job for find. Bash might have a no match glob; zsh almost certainly doesSunflower
C
14

Try one of

git add *.java ':(exclude):**Component1**'
git add *.java ':!**Component1**'

Any pathspec beginning with a colon is a magic pathspec. exclude is one of them.

Changeover answered 3/5, 2019 at 22:6 Comment(1)
Thanks! This worked for me: git add *.java ':!**Component1**'Stillness

© 2022 - 2024 — McMap. All rights reserved.