Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two asterisks to add a file in git

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.

like image 837
Paul Avatar asked Aug 15 '12 11:08

Paul


People also ask

What is difference git add * git add?

Hi@akhtar, The difference lies in which files get added. git add -A command will add all modified and untracked files in the entire repository. Whereas git add . will only add modified and untracked files in the current directory and any sub-directories.

How do I add files to a git file?

Add only one file, or one part of the changed file: git add README.md. Commit the first set of changes: git commit -m "update the README to include links to contributing guide" Add another file, or another part of the changed file: git add CONTRIBUTING.md.

What is git add star?

add * means add all files in the current directory, except for files whose name begin with a dot. This is your shell functionality and Git only ever receives a list of files.

How do you add multiple files for commit in git?

To add multiple files in Git, first, navigate to the directory where the untracked files are present and execute the “$ git add” command with the required files name. Then, use the “$ start” command to open added files one by one, make changes and save them.


1 Answers

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).

like image 100
Useless Avatar answered Sep 18 '22 08:09

Useless