git 使用两个星号在git中添加文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11968747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:20:33  来源:igfitidea点击:

Using two asterisks to add a file in git

regexgitwildcardglobgit-bash

提问by Paul

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

我想添加一个文件,该文件具有唯一的文件名但前面的路径很长(例如 a/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.

我正在使用基于minGW 的Git Bash for Windows

回答by Useless

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

对比

$ ls -d *filename*

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

(我刚刚添加了-dso 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 mightmatch a filename with a leading -, it's important to make sure git knows it's a filename and not an option.

请注意--... 如果您使用的 glob可能将文件名与前导匹配-,那么确保 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 在找到两个匹配项时添加了所有内容......由于试运行与实际运行的行为不同,我认为肯定存在错误,但我认为该讨论将在其他地方进行(除非有人认为)不是错误)。