有没有办法通过git checkout使用通配符? [英] Is there a way to use wildcards with git checkout?

查看:386
本文介绍了有没有办法通过git checkout使用通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

git checkout我想要做的是签出一个文件或一组文件myBranch * / myFile.md



git checkout myBranch - * / * Test * (不知道' - '部分)

而不是

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命令有一些有限的通配符功能,例如 diff 添加,但没有找到结帐的内容。有没有办法?



编辑:我在Linux上使用git 1.7.9.5。 git和shell命令的工作组合也是可以接受的。

处理通配符,使用 fnmatch(3)。请参阅 Git词汇表中的 pathspec 条目。



但要确保git能够看到通配符,它​​们必须被转义,否则您的 shell 将首先展开它们。通常,我在单引号之间使用通配符:

  git checkout myBranch  - '* / myFile.md'

通配符适用于包含目录的全名。

正如您在文档中看到的,pathspec还允许魔术签名,它改变了如何解释pathspec。例如,您可以使用 icase 来区分不区分大小写的路径(您可以键入':( icase)* readme *'以查找您的所有自述文件)。
$ b 我在这里引用@ bambams的评论,以防在Windows中遇到问题:


在2.8.1.windows.1中,这不适用于Windows,而我是
,它使用cmd.exe外壳中的Git,因此没有嵌套。但是有
解决方案,如果你处于这种抱歉状态。结合 git diff --name-only xargs 来实现您的目标:

  git diff --name-only< COMMIT> - < GLOB> ... xargs git checkout< COMMIT> 



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 -- */*Test* (not sure about the '--' part)

instead of

git checkout myBranch src/main/java/a/deep/package/structure/myFile.md and

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?

EDIT: I am using git 1.7.9.5 on Linux. A working combination of git and shell commmands would be acceptable as well.

解决方案

Git does deal with wildcards, using fnmatch(3). See the pathspec entry in Git glossary.

But to be sure that git can see the wildcards, they must be escaped otherwise your shell will expand them first. Typically, I use wildcards between single-quotes:

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

I quote @bambams's comment here, in case you have problems in 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 and xargs to achieve your goal:

git diff --name-only <COMMIT> -- <GLOB>... | xargs git checkout <COMMIT> 

这篇关于有没有办法通过git checkout使用通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆