有没有办法在 git checkout 中使用通配符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15160978/
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
Is there a way to use wildcards with git checkout?
提问by kostja
What I would like to do is to checkout a single file or a set of files with a common name part like this
我想要做的是检出单个文件或一组具有这样的通用名称部分的文件
git checkout myBranch */myFile.md
and
git checkout myBranch */myFile.md
和
git checkout myBranch -- */*Test*
(not sure about the '--' part)
git checkout myBranch -- */*Test*
(不确定“--”部分)
instead of
代替
git checkout myBranch src/main/java/a/deep/package/structure/myFile.md
and
git checkout myBranch src/main/java/a/deep/package/structure/myFile.md
和
git checkout myBranch src/test/java/a/deep/package/structure/TestOne.java
git checkout myBranch src/test/java/a/deep/package/structure/TestTwo.java
git checkout myBranch src/test/java/a/deep/package/structure/resources/TestData.sql
git checkout myBranch src/test/java/a/deep/package/structure/TestOne.java
git checkout myBranch src/test/java/a/deep/package/structure/TestTwo.java
git checkout myBranch src/test/java/a/deep/package/structure/resources/TestData.sql
I know there is some limited wildcard functionality for some git command like diff
and add
but have found nothing for checkout
. Is there a way?
我知道某些 git 命令有一些有限的通配符功能diff
,add
但没有找到checkout
. 有办法吗?
EDIT: I am using git 1.7.9.5 on Linux. A working combination of git and shell commmands would be acceptable as well.
编辑:我在 Linux 上使用 git 1.7.9.5。git 和 shell 命令的工作组合也是可以接受的。
回答by coredump
Git doesdeal with wildcards, using fnmatch(3). See the pathspecentry in Git glossary.
Git确实使用fnmatch(3)处理通配符。请参阅Git 词汇表中的pathspec条目。
But to be sure that git can see the wildcards, they must be escaped otherwise your shellwill expand them first. Typically, I use wildcards between single-quotes:
但是为了确保 git 可以看到通配符,它们必须被转义,否则你的shell将首先扩展它们。通常,我在单引号之间使用通配符:
git checkout myBranch -- '*/myFile.md'
The wildcards are applied to the whole name, directories included.
通配符应用于整个名称,包括目录。
As you can see in the documentation, the pathspec also allows magic signaturewhich change how to interpret the pathspec. For example, you can have case-insensitive paths with icase(you can type ':(icase)*readme*'
to find all your readme's).
正如您在文档中看到的,pathspec 还允许改变解释 pathspec 的方式的魔术签名。例如,您可以使用icase 设置不区分大小写的路径(您可以键入':(icase)*readme*'
以查找所有自述文件)。
I quote @bambams's comment here, in case you have problems in Windows:
我在这里引用@bambams 的评论,以防您在 Windows 中遇到问题:
This is not working for me in Windows with 2.8.1.windows.1 and I'm utilizing Git from the cmd.exe shell so no globbing built in. There is however a solution if you're in such a sorry state. Combine
git diff --name-only
andxargs
to achieve your goal:git diff --name-only <COMMIT> -- <GLOB>... | xargs git checkout <COMMIT>
这在带有 2.8.1.windows.1 的 Windows 中对我不起作用,我从 cmd.exe shell 中使用 Git,因此没有内置 globbing。但是,如果您处于如此糟糕的状态,有一个解决方案。结合
git diff --name-only
并xargs
实现您的目标:git diff --name-only <COMMIT> -- <GLOB>... | xargs git checkout <COMMIT>
回答by Intrepidd
Git does not deal with the wildcard, but your shell does.
Git 不处理通配符,但您的 shell 会处理。
Try this :
尝试这个 :
git checkout myBranch **/myFile.md
and
和
git checkout myBranch **/*Test*
With the **
, your shell will look for files in all the subdirectories starting from the current working directory.
使用**
,您的 shell 将在从当前工作目录开始的所有子目录中查找文件。
回答by 9Rune5
Powershell
电源外壳
@(gci -Recurse *test* | Resolve-Path -Relative) | %{git checkout mybranch -- $_)
gci generates a list of all files matching the criteria (*test*
) then pipes it to Resolve-Path to get the relative path. Finally the flattened (@) list of filenames is piped into the git invokation (executes once per file found).
gci 生成与条件 ( *test*
)匹配的所有文件的列表,然后将其通过管道传输到 Resolve-Path 以获取相对路径。最后,扁平化的 (@) 文件名列表通过管道传输到 git 调用中(每个找到的文件执行一次)。
回答by Sarath
Windows git bash with xargs
带有 xargs 的 Windows git bash
git difftool myBranch --name-only *.java | xargs git checkout myBranch
回答by ferdymercury
If you have a gitignore or untracked files and you want to use wildcards with git checkout, then it might give you problems, and a solution could be:
如果您有一个 gitignore 或未跟踪的文件,并且您想在 git checkout 中使用通配符,那么它可能会给您带来问题,解决方案可能是:
git ls-files '*/myFile.md' | tr '\n' 'BRANCH_NAME=$(git branch --list | grep [mask] | cut -c3-)
git checkout ${BRANCH_NAME}
' | xargs -0 -L1 -I '$' git checkout -- '$'
回答by Ivan M
Knowing that git returns branch list names from 3rd char, one can just switch to needed branch defined via [mask]:
知道 git 从第三个字符返回分支列表名称,您可以切换到通过 [mask] 定义的所需分支:
gitcheckout()
{
GIT_DIR=$(git rev-parse --git-dir 2>/dev/null);
pushd .;
cd $GIT_DIR/../;
git diff --name-only -- | xargs git checkout --;
popd;
}
回答by Klik
None of the other answers worked for me, but bambams comment worked. I made it into a bash function that accepts two args. Usage:
其他答案都不适合我,但 bambams 评论有效。我把它变成了一个接受两个参数的 bash 函数。用法:
gitcheckout myBranch '*file*'
gitcheckout myBranch '*文件*'
##代码##