11

Was trying to commit some changes. I used git add to add any new javascript files that I may have created using the wildcard *.js. Then I committed changes and pushed to github:

git add *.js
git commit -m "stuff"
git push github master

When I checked github, all the files that I had been editing were blank. They were there, just empty.

I then tried to commit again, but GIT said that everything was up to date.

Then I went back and noticed that after I did git commit -m "stuff", GIT displayed a message saying that a bunch of my ".js" files were not staged even though I had just added them using the wildcard: git add *.js. This is the message that was displayed when I attempted to commit.

# On branch master
# Changes not staged for commit:
#   (use "git add/rm ..." to update what will be committed)
#   (use "git checkout -- ..." to discard changes in working directory)
#
#       modified:   src/static/directory1/js/module1/file1.js
#       modified:   src/static/directory1/js/module1/file2.js
#       modified:   src/static/directory1/js/module2/file1.js

In order to fix this I had to go down a few directories when doing my git add:

git add src/static/directory1/*.js

This seemed to worked, because the files were there after I committed again and then pushed to github:

git commit -m "stuff"
git push github master

What was going on here, why did I have to navigate down a few directories to get the wild card to work?

Thanks!

Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258

3 Answers3

35

You need to use

git add '*.js'

You have to use quotes so git receives the wildcard before your shell does. If you do not have quotes the shell will only do the wildcard search within your current directory.

Aaron Gray
  • 11,283
  • 7
  • 55
  • 61
  • I can't seem to find any evidence that Git will recurse into subdirectories when given a wildcard (I tried it). – Greg Hewgill Aug 15 '12 at 19:55
  • Ok, it seems to work there but not on my local machine. Was this perhaps a recent feature addition to Git? – Greg Hewgill Aug 15 '12 at 19:57
  • Perhaps so. I just learned about it when that course came out in July 2012. – Aaron Gray Aug 15 '12 at 19:58
  • Curious. I tried with the latest Git 1.7.11.4 (on OS X) and could not replicate this recursive wildcard add behaviour. – Greg Hewgill Aug 15 '12 at 20:09
  • try.github.com claims to be running 1.7.11.3, but it behaves differently from what I'm seeing. – Greg Hewgill Aug 15 '12 at 20:16
  • 3
    Ok, I initially tried with a couple of files called `foo` in different directories and was using the wildcard `'f*'`. That did not work. When I renamed them both to `foo.txt` and used the wildcard `'*.txt'`, that worked. Using the original `foo` files with `'*o'` also worked. It appears that this recursive add support works only with the wildcard in the initial position. – Greg Hewgill Aug 15 '12 at 20:22
  • 2
    @Greg git seems to match against the full path so if I have a file "a/b/c/foo.c" `git add 'a/b*'` will add it but not `git add 'foo*'`. At lest that's how I understand it. – Alex Jasmin Aug 15 '12 at 20:31
  • @AlexandreJasmin: Yes, that explains it. Thanks! – Greg Hewgill Aug 15 '12 at 20:32
16

even though I had just added them using the wildcard: git add *.js

Shell wildcard expansion does not recurse into subdirectories. The wildcard is expanded before Git gets a chance to see it.

If you use git add '*.js', then Git will see the wildcard and will match it against all path names that end in .js. Because the * is in the initial position, this will end up recursively adding all .js files including in subdirectories.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 1
    Please don't steal my answer. :-) I am trying to get points on SO and you added my answer to yours 30 minutes after we had a discussion about my answer. – Aaron Gray Aug 15 '12 at 20:45
  • @AaronGray: not to worry, I upvoted your answer too, once I understood what was going on and why it worked. – Greg Hewgill Aug 15 '12 at 20:46
8

An alternative is to use the find command:

find . -name '*js' -exec git add {} \;

Running that without the exec will give you the list of files that you are working on; so, it is easy to tune this command to your liking.

Chris Dutrow
  • 48,402
  • 65
  • 188
  • 258
Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46