By Michel Krämer


2010-05-18 07:11:40 8 Comments

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.

11 comments

@selalerer 2018-12-19 08:09:49

Adding a Windows command line solution that was not yet mentioned:

for /f "delims=" %G in ('dir /b/s *.java') do @git add %G

@Hong Emrys 2019-06-09 06:51:30

put line in ~/.gitconfig

[alias] addt = !sh -c 'git ls-files | grep \"\\.$1*\" | xargs git add' -

If you want to add all modified java file can just do: git addt java

Samely, if you want to add all modified python file can just do: git addt py

@Sergey Glotov 2012-02-24 14:20:46

You can use git add [path]/\*.java to add java files from subdirectories,
e.g. git add ./\*.java for current directory.

From git add documentation:

Adds content from all *.txt files under Documentation directory and its subdirectories:

$ git add Documentation/\*.txt

Note that the asterisk * is quoted from the shell in this example; this lets the command include the files from subdirectories of Documentation/ directory.

@Alberto 2012-03-14 19:08:30

@MichelKrämer probably it wasn't, but it is now.

@TheTFo 2014-10-15 14:58:49

This is the best solution, works in windows as well.

@gwideman 2018-03-06 06:07:30

"asterisk * is quoted from the shell". I think you mean "escaped". In any case, the backslash prevents the shell from expanding it.

@Sergey Glotov 2018-03-06 06:45:19

@gwideman I think you're right, but it's not me. It's quote from the docs git-scm.com/docs/git-add#_examples

@Jan Lovstrand 2017-12-20 13:40:42

Just use git add *\*.java. This will add all .java files in root directory and all subdirectories.

@Philip Rego 2019-06-17 16:02:50

Great answer. I did git add */pom.xml to add all files names pom.xml.

@Dmitrii Pisarenko 2020-02-29 19:36:45

This one worked for me, too. Thanks!

@bdanin 2017-07-26 23:06:07

I wanted to only add files that had a certain string based on git status:

git status | grep string | xargs git add

and then was able to git commit -m 'commit msg to commit all changed files with "string" in the title of the file

@VonC 2017-04-10 20:34:24

As mentioned in "git: How do I recursively add all files in a directory subtree that match a glob pattern?", if you properly escape or quote your pathspec globbing (like '*.java'), then yes, git add '*.java'

Git 2.13 (Q2 2017) improves that for interactive add:

See commit 7288e12 (14 Mar 2017) by Jeff King (peff).
(Merged by Junio C Hamano -- gitster -- in commit 153e0d7, 17 Mar 2017)

add --interactive: do not expand pathspecs with ls-files

When we want to get the list of modified files, we first expand any user-provided pathspecs with "ls-files", and then feed the resulting list of paths as arguments to "diff-index" and "diff-files".
If your pathspec expands into a large number of paths, you may run into one of two problems:

  1. The OS may complain about the size of the argument list, and refuse to run. For example:

    $ (ulimit -s 128 && git add -p drivers)
    Can't exec "git": Argument list too long at .../git-add--interactive line 177.
    Died at .../git-add--interactive line 177.
    

That's on the linux.git repository, which has about 20K files in the "drivers" directory (none of them modified in this case). The "ulimit -s" trick is necessary to show the problem on Linux even for such a gigantic set of paths.
Other operating systems have much smaller limits (e.g., a real-world case was seen with only 5K files on OS X).

  1. Even when it does work, it's really slow. The pathspec code is not optimized for huge numbers of paths. Here's the same case without the ulimit:

    $ time git add -p drivers
      No changes.
    
    real  0m16.559s
    user    0m53.140s
    sys 0m0.220s
    

We can improve this by skipping "ls-files" completely, and just feeding the original pathspecs to the diff commands.

Historically the pathspec language supported by "diff-index" was weaker, but that is no longer the case.

@Olivier Verdier 2010-05-18 07:30:18

With zsh you can run:

git add "**/*.java"

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

@Michel Krämer 2010-05-18 07:38:31

Thanks for that, but I'm working with msysgit on Windows and it has a bash only.

@Olivier Verdier 2010-05-18 07:41:08

Well, I see that there is a zsh shell for windows... you will do yourself a favour to use it instead of bash, imho.

@Michel Krämer 2010-05-18 08:09:37

The Windows port of zsh is based on a very old version and crashes all the time (for example when I enter ls).

@Tomáลก Votruba 2016-11-19 18:14:53

Wow, perfect. I didn't know about that feature!

@Dmitry Minkovsky 2018-01-19 17:18:31

This works fine on bash too. Just make sure the files you're targeting are below your current working directory. So you may have to do something like git add ../../**/*.java (You don't need quotes surrounding the glob on bash)

@gwideman 2018-03-06 06:05:28

This is nothing to do with zsh. I think this answer misses the point that you want to pass the path with wildcards intact to git and let git process it. You therefore want to prevent whichever shell you are using from expanding the wildcard(s). To do this you need to quote it some way that's appropriate for the shell you're using. See other answers for details.

@Ram 2012-04-13 04:58:06

If you are already tracking your files and have made changes to them and now you want to add them selectively based on a pattern, you can use the --modified flag

git ls-files --modified | grep '<pattern>' | xargs git add

For example, if you only want to add the CSS changes to this commit, you can do

git ls-files --modified | grep '\.css$' | xargs git add

See man git-ls-files for more flags

@Alberto 2012-03-14 19:07:40

Sergey's answer (don't credit me) is working:

You can use git add [path]/\*.java

with a recent git:

$git version
git version 1.7.3.4

Files for the test:

$find -name .git -prune -o -type f -print | sort
./dirA/dirA-1/dirA-1-1/file1.txt
./dirA/dirA-1/dirA-1-2/file2.html
./dirA/dirA-1/dirA-1-2/file3.txt
./dirA/dirA-1/file4.txt
./dirB/dirB-1/dirB-1-1/file5.html
./dirB/dirB-1/dirB-1-1/file6.txt
./file7.txt

Git status:

$git status -s
?? dirA/
?? dirB/
?? file7.txt

Adding *.txt:

$git add \*.txt

Updated status:

$git status -s
A  dirA/dirA-1/dirA-1-1/file1.txt
A  dirA/dirA-1/dirA-1-2/file3.txt
A  dirA/dirA-1/file4.txt
A  dirB/dirB-1/dirB-1-1/file6.txt
A  file7.txt
?? dirA/dirA-1/dirA-1-2/file2.html
?? dirB/dirB-1/dirB-1-1/file5.html

@Cascabel 2010-05-18 13:57:44

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.

@Michel Krämer 2010-05-19 10:52:56

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

@Sergio Acosta 2010-05-18 07:15:29

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.

@yoyo 2012-03-01 07:15:19

Minor issue with this approach -- if you "find" a file that's matched by your .gitignore then git complains that you're trying to add a file you told it to ignore.

@Maslow 2013-08-20 15:04:42

@yoyo - I just added -f on the end, and that fixed that

@yoyo 2013-09-01 22:38:37

@Maslow, that will forcibly add the file, even though it was supposed to be ignored (based on .gitignore). I'd prefer to get the complaint and not add the file (which is the default behaviour.)

@gwideman 2018-03-06 06:08:20

How is this in any way better than letting git perform the wildcard expansion?

Related Questions

Sponsored Content

13 Answered Questions

[SOLVED] How do I show the changes which have been staged?

14 Answered Questions

[SOLVED] Move the most recent commit(s) to a new branch with Git

26 Answered Questions

[SOLVED] How to make Git "forget" about a file that was tracked but is now in .gitignore?

  • 2009-08-13 19:23:22
  • Ivan
  • 1188924 View
  • 5200 Score
  • 26 Answer
  • Tags:   git gitignore git-rm

36 Answered Questions

[SOLVED] How to remove local (untracked) files from the current Git working tree

  • 2008-09-14 09:06:10
  • Readonly
  • 2217523 View
  • 6744 Score
  • 36 Answer
  • Tags:   git branch git-branch

17 Answered Questions

[SOLVED] How do you clone a Git repository into a specific folder?

43 Answered Questions

[SOLVED] How do I force "git pull" to overwrite local files?

19 Answered Questions

[SOLVED] How can I delete a file from a Git repository?

  • 2010-01-12 07:48:52
  • webminal.org
  • 2272931 View
  • 1881 Score
  • 19 Answer
  • Tags:   git git-rm

32 Answered Questions

[SOLVED] How do I undo 'git add' before commit?

26 Answered Questions

[SOLVED] How can I reconcile detached HEAD with master/origin?

  • 2011-04-24 17:51:01
  • Ben Zotto
  • 947668 View
  • 1543 Score
  • 26 Answer
  • Tags:   git

Sponsored Content