79

When I try to do a basic git add *.erb (or any simple wildcard expressions) git is not recognizing it (them). As a side-note, I have never done this before so I'm sure it's a rookie mistake, but I've found no help in other SO posts or my school's alumni listserv so I thought this post may be appropriate.

For (a different) example, git status is giving me:

#   modified:   config/routes.rb
#   modified:   spec/models/question_spec.rb

I wanted to only stage the routes file, so I tried git add *s.rb and no dice. I am in the root of the app...do I need to be in the directory that contains the file(s) I'm trying to apply the wildcard expression to? That would be painful, but...actually...it just worked.

Hope this doesn't have to be a separate post, but is there an easier way to use wildcards where you don't have to cd into the specific directory?

RudyOnRails
  • 1,819
  • 3
  • 17
  • 26
  • 1
    When using my shell (`zsh`), I type `git add *.cpp`, and the shell completes the wildcard to the full filenames. `git add **/*.cpp` does all `cpp` files under the current dir. Bash may have something similar. – simont Aug 22 '12 at 04:16
  • 3
    Bash v4 and higher supports `**` for recursive globbing (http://wiki.bash-hackers.org/syntax/expansion/globs), so if you're using a recent enough version of bash, `git add **/*.rb` should do what you want. – Michael Anderson Aug 22 '12 at 04:20
  • Thank you @simont @Michael. Both of these were barking up the correct tree. I used `git add **/*s.rb` and am all good! – RudyOnRails Aug 22 '12 at 04:45
  • @MichaelAnderson I tried `git add */*s.rb` as well and was successful. Does the `*/` act as a wildcard for one directory deeper, but `**/` denote recursive _infinite_ directories? If I should ask a new question, let me know! – RudyOnRails Aug 22 '12 at 05:00
  • 1
    Thats correct. `*/*s.rb` will match everything ending "s.rb" one directory deep (and not in the current directory and not deeper). Similarly `*/*/*s.rb` would be everything ending "s.rb" two directories deep, but nowhere else (deeper or shallower). – Michael Anderson Aug 22 '12 at 05:05

5 Answers5

115

Put it around single quotes. This should work.

git add '*s.rb'

UPDATE

After more testing, I find out that I can even do git add *s.rb without quotes. This might just work for me with my git version 1.7.10.4 (Mac OSX Lion, homebrew). My explanation is that if shell wildcards expansion doesn't match any file, it'd supply the original unexpanded argument '*s.rb' to git add.

sh-3.2$ git status
#
#   modified:   config/routes.rb
#   modified:   spec/models/products_spec.rb
#

Now I do git add without quotes.

sh-3.2$ git add *s.rb

Surprisingly it works!

sh-3.2$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   modified:   config/routes.rb
#
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   spec/models/products_spec.rb
#

If wildcard pattern doesn't match any file, git will give out this error.

sh-3.2$ git add *x.rb
fatal: pathspec '*x.rb' did not match any files
chifung7
  • 2,531
  • 1
  • 19
  • 17
  • 14
    Adding to your answer: this is because your shell is expanding the wildcard before the git command gets executed. If you put single quotes, it prevents the shell from expanding the wildcard. – Snowball Aug 22 '12 at 04:16
  • 1
    Yes, this is right. With single quote, git would take '*s.rb' and search for files matching this pattern. – chifung7 Aug 22 '12 at 04:20
  • I got excited for a second because I thought this would work, but I remember trying it in the past and again, no luck. – RudyOnRails Aug 22 '12 at 04:21
  • Works for me with git version 1.7.10.4 – chifung7 Aug 22 '12 at 04:55
  • Also, depending on the shell, `*s.rb` without quotes will work provided that there are no actual matches in the current directory. That is, if `*s.rb` doesn't match anything, it's passed as-is to the application. – Edward Falk May 03 '15 at 13:20
  • Bizarrely, until a few months ago you could do this fine without quotes on git-for-windows in mingw. Something changed and now quotes are required. – RJFalconer Nov 10 '20 at 18:20
8

I can't get recursive globs to work for me as my version of bash is too old. So what I do is

find . -name "*.rb" | xargs git add

Works a treat and I can add other filters there if I need - so if I want to add all but one file I can do this.

find . -name "*.rb" | grep -v "not_me.rb" | xargs git add

There's a couple of other cases that can be useful.

If you just want to add files that already exist in your repo you can

git add -u .

If you want to add every thing then

git add .
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
4

You can also try git's magic signature for matching a path via glob e.g.

git add ':(glob)**/*.ext'

see more here https://css-tricks.com/git-pathspecs-and-how-to-use-them/

Alvis
  • 590
  • 4
  • 8
1

In my experience (I may be missing an option or something), git add only looks at files in the current directory and subdirectory. So if you want to add everything, go to the root directory of the repo. Though if the repo is really big, this could take a while, especially if you don't pass the -u flag.

Antimony
  • 37,781
  • 10
  • 100
  • 107
0
git add '*.ext'

git add '*.prefix.ext'

// Lets say you just want to add js files use

git add '*.js'

// Or if you want to just add the tests

git add '*.test.js'

// The single quotes are important!

KISHORE K J
  • 186
  • 2
  • 4
  • Hi! Could you [edit] in some explanation of how these commands help solve the situation described in the question? At the moment, it's unclear what you're trying to add that isn't covered by other answers. – IMSoP Jul 16 '23 at 07:56