views:

36

answers:

3

How do I recursively add files by a pattern (or glob) located in different directories?

For example, I'd like to add A/B/C/foo.java and D/E/F/bar.java (and several other java files) with one command:

git add '*.java'

Unfortunately, that doesn't work as expected.

+3  A: 

A bit off topic (not specifically git related) but if you're on linux/unix a workaround could be:

find . -name '*.java' | xargs git add

And if you expect paths with spaces:

find . -name '*.java' -print0 | xargs -0 git add

But I know that is not exactly what you asked.

Sergio Acosta
+2  A: 

With zsh you can run:

git add **/*.java

and all your *.java files will be added recursively.

Olivier
Thanks for that, but I'm working with msysgit on Windows and it has a bash only.
Michel Krämer
Well, I see that there is a `zsh` shell for windows... you will do yourself a favour to use it instead of bash, imho.
Olivier
The Windows port of zsh is based on a very old version and crashes all the time (for example when I enter `ls`).
Michel Krämer
+1  A: 

Sergio Acosta's answer is probably your best bet if some of the files to be added may not already be tracked. If you want to limit yourself to files git already knows about, you could combine git-ls-files with a filter:

git ls-files [path] | grep '\.java$' | xargs git add

Git doesn't provide any fancy mechanisms for doing this itself, as it's basically a shell problem: how do you get a list of files to provide as arguments to a given command.

Jefromi
Thanks! I improved your command slightly and now it's doing what I was looking for: `git ls-files -co --exclude-standard | grep '\.java$' | xargs git add`
Michel Krämer
Oh, I forgot it could show untracked files. Good catch.
Jefromi