86

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
  • Strange, git diff doesn't seem to support wildcards for me, though git add and commit do. Jun 4, 2013 at 15:27

9 Answers 9

95

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> 
6
  • 2
    A bit late to the party but undoubtedly correct. Thanks @coredump, time to move the green checkmark :)
    – kostja
    Oct 24, 2015 at 14:34
  • @kostja Yes, definitely late ;-) Thanks
    – coredump
    Oct 25, 2015 at 16:06
  • 4
    But no way to wildcard the branch name, e.g. git checkout my*?
    – dumbledad
    Mar 4, 2016 at 14:03
  • 6
    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> --
    – bambams
    Jul 14, 2016 at 21:34
  • 1
    Git v2.13.2.windows.1 didn't want the escape apostrophe, git checkout */myFile.md worked fine and went through subfolders.
    – CAD bloke
    Oct 27, 2017 at 5:49
24

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.

6
  • Works as advertised. Thank you Intrepidd
    – kostja
    Mar 1, 2013 at 16:01
  • 12
    Note that this only considers files that the shell sees, e.g., checked out files. It does not checkout files that aren't already present.
    – vonbrand
    Mar 2, 2013 at 4:20
  • 1
    @vonbrand: any way to do it with files that are in the commit but not currently in the file system?
    – Gauthier
    Aug 27, 2013 at 12:27
  • 3
    Which shell? I'm using Cygwin bash and this doesn't work for me.
    – bacar
    Oct 13, 2014 at 15:45
  • 1
    @MarcusLeon Maybe depends on the shell but I tried with zsh and it works.
    – Intrepidd
    Dec 4, 2015 at 12:30
2

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

2

Windows git bash with xargs

git difftool myBranch --name-only *.java | xargs git checkout myBranch
0

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

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:

git ls-files '*/myFile.md' | tr '\n' '\0' | xargs -0 -L1 -I '$' git checkout -- '$'
0

Knowing that git returns branch list names from 3rd char, one can just switch to needed branch defined via [mask]:

BRANCH_NAME=$(git branch --list | grep [mask] | cut -c3-)
git checkout ${BRANCH_NAME}
0

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

If you need to get the files from a commit regardless of whether they were changed in that commit, you can use the following:

git ls-tree --name-only -r myBranch | grep myFilePattern | xargs git checkout myBranch

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.