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
我想添加一个文件,该文件具有唯一的文件名,但前面的路径很长(例如/ b / c / d / filename.java)。通常我会通过这样做将它添加到我的存储库中
git add *filename.java
.
git add * filename.java。
However I have also done this before:
不过我之前也这样做过:
git add a/b/c/d/filename*
git add a / b / c / d / filename *
So I tried to combine the two:
所以我试着将两者结合起来:
git add *filename*
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.
我的问题不在于如何使用文件名将文件添加到git存储库(尽管这会很有用)。我的问题是我对*操作的误解是什么让我认为上述应该有效。
Info:
信息:
I am using Git Bash for Windows, which is based on minGW.
我正在使用Git Bash for Windows,它基于minGW。
1 个解决方案
#1
11
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.
您正在查看globs(不是正则表达式,这是一种不同的模式匹配语言),它们是由shell扩展的,而不是由git扩展。
If you want to see how they're going to match, just pass the same glob to another command, eg.
如果你想看看它们将如何匹配,只需将相同的glob传递给另一个命令,例如。
$ ls -d *filename.java
vs
VS
$ ls -d *filename*
(I've just added the -d
so ls doesn't show the contents of any directories that match)
(我刚刚添加了-d,所以ls不会显示匹配的任何目录的内容)
Since you're using git bash, and it's possible that glob expansion behaves differently from a regular shell, try
由于你正在使用git bash,并且glob扩展可能与常规shell的行为不同,请尝试
$ git add --dry-run --verbose -- *filename*
for example: this should show you how it really expands the glob and what effect that has.
例如:这应该向您展示它如何真正扩展glob以及它具有什么效果。
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.
注意 - ...如果你使用的globs可能与带有前导的文件名匹配 - 那么确保git知道它是文件名而不是选项是很重要的。
Unfortunately, this will only show you the files which both match the glob, and have some difference between the index and working copy.
不幸的是,这只会显示匹配glob的文件,并且索引和工作副本之间有一些区别。
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.
我忘记了我没有添加的bin文件夹,所以当我执行干运行时,我意识到它找到了两个匹配:filename.java和filename.class。当我将glob更改为* filename.j *时,它工作正常。
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).
我的下一步是删除.class并再次尝试命令:它工作正常!仍然无法解释为什么git bash在发现两个匹配时添加了所有内容...因为干运行与实际运行不同我认为必定存在错误,但我认为讨论将在别处进行(除非有人认为不是一个bug)。
#1
11
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.
您正在查看globs(不是正则表达式,这是一种不同的模式匹配语言),它们是由shell扩展的,而不是由git扩展。
If you want to see how they're going to match, just pass the same glob to another command, eg.
如果你想看看它们将如何匹配,只需将相同的glob传递给另一个命令,例如。
$ ls -d *filename.java
vs
VS
$ ls -d *filename*
(I've just added the -d
so ls doesn't show the contents of any directories that match)
(我刚刚添加了-d,所以ls不会显示匹配的任何目录的内容)
Since you're using git bash, and it's possible that glob expansion behaves differently from a regular shell, try
由于你正在使用git bash,并且glob扩展可能与常规shell的行为不同,请尝试
$ git add --dry-run --verbose -- *filename*
for example: this should show you how it really expands the glob and what effect that has.
例如:这应该向您展示它如何真正扩展glob以及它具有什么效果。
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.
注意 - ...如果你使用的globs可能与带有前导的文件名匹配 - 那么确保git知道它是文件名而不是选项是很重要的。
Unfortunately, this will only show you the files which both match the glob, and have some difference between the index and working copy.
不幸的是,这只会显示匹配glob的文件,并且索引和工作副本之间有一些区别。
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.
我忘记了我没有添加的bin文件夹,所以当我执行干运行时,我意识到它找到了两个匹配:filename.java和filename.class。当我将glob更改为* filename.j *时,它工作正常。
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).
我的下一步是删除.class并再次尝试命令:它工作正常!仍然无法解释为什么git bash在发现两个匹配时添加了所有内容...因为干运行与实际运行不同我认为必定存在错误,但我认为讨论将在别处进行(除非有人认为不是一个bug)。