Search code examples
gitgit-commitgit-add

Git add files which do not match a pattern


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?


Solution

  • 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.