Is there a way to use wildcards with git checkout?

此生再无相见时 提交于 2019-11-28 20:46:57

问题


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.


回答1:


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> 



回答2:


Git does not deal with the wildcard, but your shell does.

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.




回答3:


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




回答4:


None of the other answers worked for me, but bambams comment worked. I made it into a bash function that accepts two args. Usage:

gitcheckout myBranch '*file*'

gitcheckout()
{
    GIT_DIR=$(git rev-parse --git-dir 2>/dev/null); 
    pushd .;
    cd $GIT_DIR/../;
    git diff --name-only $1 -- $2 | xargs git checkout $1 --;
    popd;
}


来源:https://stackoverflow.com/questions/15160978/is-there-a-way-to-use-wildcards-with-git-checkout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!