"git add" using wildcard is not functioning as I hoped - must I cd into specific directories?

GitBashWildcard

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

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

Solution 2 - Git

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 .

Solution 3 - Git

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/

Solution 4 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRudyOnRailsView Question on Stackoverflow
Solution 1 - Gitchifung7View Answer on Stackoverflow
Solution 2 - GitMichael AndersonView Answer on Stackoverflow
Solution 3 - GitAlvisView Answer on Stackoverflow
Solution 4 - GitAntimonyView Answer on Stackoverflow