1

I have a bunch of Java files in my current Java project that I keep modifying and I want to add them all in one fell swoop from the root folder of the project (in Ubuntu).

I tried:

git add ./*.java

but that doesn't do it. I tried escaping the asterisk like so:

git add ./\*.java

but that doesn't do it. Then I tried quoting it like so:

git add "./*.java"

but that doesn't do it.

The man page for "git add" gives this as an example:

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

           $ git add Documentation/\*.txt

I tried using a directory such as "Documentation" above to qualify this but that doesn't work either.

What am I missing?

[Update] I tried out @GoZoner's suggestion below and it works, but only if the files are new, and not if they are pre-existing ones that have been updated. This is very odd.

scorpiodawg
  • 5,612
  • 3
  • 42
  • 62
  • What error do you get? Either `git add ./*.java` or `git add *.java` should add all the Java files in the current directory. If it is successful, you won't see any information. What does `git status` say? – Jonathan Leffler Apr 05 '12 at 18:28
  • 1
    Replace the words 'git add' with 'ls' and try again. Specifically, 'ls ./*.java'. If nothing shows up in the 'ls' listing then 'git add' won't find anything either. – GoZoner Apr 05 '12 at 19:17
  • +1 to @GoZoner. Fileglobbing is done by the shell, not the program that's being called. Test out what you're adding by using ls on that same pattern. – Dan Ray Apr 05 '12 at 19:59
  • @JonathanLeffler: I don't get any errors, I just see via 'git status' that what was unstaged earlier remains unstaged. – scorpiodawg Apr 05 '12 at 20:19
  • @GoZoner (and Dan Ray): 'ls' does not list any files of course because it does not recurse unless you use the --recurse option. This is not the right analogy since 'git add .' recurses and adds all files whereas 'ls .' only shows the current directory. In the snippet I quoted from the documentation, it says "and its *subdirectories*" – scorpiodawg Apr 05 '12 at 20:20
  • Are your Java files in the current directory or in subdirectories? Have you added the directory containing the files? That will add all the files in the directory. Or use `git add -A`; that will add the files. You could even futz with `.gitignore` so that only the `.java` files are added (or, more easily, so known non-Java files are not added). – Jonathan Leffler Apr 05 '12 at 20:36
  • @scorpiodawg. Good point. If you perform 'ls -R ./*.java' and it shows nothing, then we will know why 'git add ...' does nothing. If it does show something, then we'll have to provide other suggestions to solve your posed problem. Does 'ls -R ./*.java' show stuff? – GoZoner Apr 05 '12 at 20:39
  • @JonathanLeffler: They are in subdirectories and they appear to get added automatically with the `git add \./\*.java` technique that GoZoner pointed out (the -A appears unnecessary), but only if the files have never been added before. – scorpiodawg Apr 05 '12 at 21:21

4 Answers4

4

You need to escape the '.'

git add \./\*.java

works.

$ git add \./\*.c
$ git status
# On branch br1
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#   new file:   bang/boo.c
#   new file:   bing/one.c
#
GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • This is interesting: your solution works, but *only* for new files. If you commit your change above and now update boo.c, the same git add statement no longer picks up the change. – scorpiodawg Apr 05 '12 at 21:10
  • Not for me. I committed the above index, adding two files. Then I changed bang/boo.c. 'git status' showed the files as 'not staged for commit'. Then I used the same 'git add' command as above. A 'git status' shows bang/boo.c as 'changes to be committed.' – GoZoner Apr 05 '12 at 22:02
  • Strange indeed. What shell/OS are you on? – scorpiodawg Apr 05 '12 at 22:05
  • bash Anyway, I learned something as I've never used 'git add' to add everything below a directory. We never got to the point of understanding if your posed problem is due to info/exclude or a core.excludesfile config setting. You'll find that in the git add help page. Good luck. – GoZoner Apr 05 '12 at 22:18
2
git add -u *.java

Where
-u adds modified tracked files
*.java is pathspec

Abi Taneja
  • 41
  • 2
0

Instead of doing 'git add' use:

git status

That will show you what files are new (unknown by git) and what files have been modified. More importantly for your case, 'git status' will show the full path to the reported files. Knowing the full path you will be able to add them one-by-one with 'git add full-path-to.java' or, if everything looks committable, 'git add -A' will add them all.

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • I'm aware of this; I usually say "git add ." and stage my changes. I'm looking for a way to skip a couple of files that have a different extension than the one I'm interested in... – scorpiodawg Apr 05 '12 at 20:15
  • Right now, the important thing is to *see* the .java files that are to be added. Are you saying that 'git status' shows a list of files and when you use 'git add' on any one of them, with the full path, it won't be added? – GoZoner Apr 05 '12 at 20:31
0

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

Here you have to specify the path to your folders.

OR

Here you give instruction to show cached c and other o files while excluding standard git exclusions: .git/info/exclude, .gitignore in each directory, and the user’s global exclusion file.

git ls-files -co --exclude-standard | grep '\.java$'| xargs git add

From this answer Recursively add files by pattern.

MSS
  • 3,306
  • 1
  • 19
  • 50