Using two asterisks to add a file in git

You're looking at globs (not regular expressions, which are a different pattern-matching language), and they're expanded by your shell, not by git.

If you want to see how they're going to match, just pass the same glob to another command, eg.

$ ls -d *filename.java

vs

$ ls -d *filename*

(I've just added the -d so ls doesn't show the contents of any directories that match)


Since you're using git bash, and it's possible that glob expansion behaves differently from a regular shell, try

$ git add --dry-run --verbose -- *filename*

for example: this should show you how it really expands the glob and what effect that has.

Note the -- ... if you're using globs that might match a filename with a leading -, it's important to make sure git knows it's a filename and not an option.

Unfortunately, this will only show you the files which both match the glob, and have some difference between the index and working copy.


Answer from author: The dry run helped a lot, here is what I found:

I was forgetting about the bin folder which I haven't added, so when I performed the dry run I realised it was finding two matches: filename.java and filename.class. When I changed the glob to *filename.j* it worked.

My next step was to remove the .class and try the command again: it worked! It is still unexplained why git bash added everything when it found two matches... since the dry run behaves differently from the actual run I think there must be a bug, but I think that discussion is to be held elsewhere (unless somebody thinks it isn't a bug).


You could try with git add ./**/*.java

Note: I tested with zsh, it should also work for bash as well.