Mohammed Noureldin Mohammed Noureldin - 3 months ago 6x
Bash Question

staging all files except specific files using glob patterns in git

I am trying to stage some new created files and some others edited files to my staging area in git using git add command. What I exactly want is to stage everything except *.java files (in my current directory and in every sub directory in my project). I tried to get that work with NO success.

pwd: /d/myProject

Nothing of these worked:

git add !(*.java)
git add \!\(\*.java\)
git add \!\(*.java\)
git add !\(\*.java\)

Could somebody help me to get that work please? And is it somehow possible to use regular expressions instead of globs here?


You could use find:

find . -type f ! -iname '*.java' -exec git add {} +

You can also skip paths matching a pattern by using ! -ipath <pattern>, e.g:

find . -type f ! -iname '*.java' ! -ipath './a/b/*c*' -exec git add {} +