I want to add a file which has a unique file name but a long preceding path (e.g. a/b/c/d/filename.java). Normally I would add this to my repository by doing
git add *filename.java.
However I have also done this before:
git add a/b/c/d/filename*
So I tried to combine the two:
git add *filename*
but this does something weird. It adds every untracked file. I can see possible reasons for failure but they all should occur in one of the previous two commands so I don’t know why this is happening.
My question isn’t so much about how to add a file to a git repository with just its file name (although that would be useful).
My question is what is my misunderstanding of the * operation which makes me think the above should work.
Info:
I am using Git Bash for Windows, which is based on minGW.
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.
vs
(I’ve just added the
-dso 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
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).