Is there a way to use wildcards with git checkout?

前端 未结 8 854
遇见更好的自我
遇见更好的自我 2020-12-24 00:40

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

<

相关标签:
8条回答
  • 2020-12-24 01:09

    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;
    }
    
    0 讨论(0)
  • 2020-12-24 01:12

    For me the task was to checkout several files named after one pattern:

    • ./a/b/c/FirstTest.java
    • .d/e/f/SecondTest.java
    • ./g/h/i/ThirdTest.java

    I've used this bash command:

    # 1. Find files named after "*Test.java" pattern.
    # 2. Execute checkout command on every found file.
    
    find . -name "*Test.java" -exec git checkout master {} \;
    

    Also firstly you can test the find command with simple echo, which just prints a given text ({} in our case, which will be replaced with current found filename by shell):

    find . -name "*Test.java" -exec echo {} \;
    
    0 讨论(0)
提交回复
热议问题