10

I'm using Git for Windows 2.9.3.windows.1 via Git Bash.

Now dev branch is checked out and I want to checkout some files which are now not in the working directory from master branch. The files have the same name and are stored in similar directories like:

path/to/a/file.txt
path/to/that/file.txt
path/to/the/file.txt
path/to/this/file.txt

I'm sure I can do that by specifying the files one by one:

git checkout master path/to/a/file.txt path/to/that/file.txt path/to/the/file.txt path/to/this/file.txt

But it's a hassle. Instead I want to use a wildcard like:

git checkout master path/to/*/file.txt

When I tried this command an error occurred:

error: pathspec 'path/to/*/file.txt' did not match any file(s) known to git.

Then, I learned pathspec and tried:

git checkout master path/to/**/file.txt
git checkout master 'path/to/*/file.txt'
git checkout master 'path/to/**/file.txt'
git checkout master */file.txt
git checkout master '*/file.txt'
git checkout master **/file.txt
git checkout master '**/file.txt'
git checkout master ':(glob)path/to/*/file.txt'
git checkout master ':(glob)path/to/**/file.txt'
git checkout master ':(glob)**/file.txt'

All of them didn't work due to the same error. They didn't work even if I add -- between master and pathspec. How can I use a wildcard in pathspec?

4
  • You say "all of them didn't work", but how, exactly?
    – Roman
    Oct 26, 2016 at 22:58
  • Yes. I just want to easily checkout that files using a wildcard which matches directories.
    – kaitoy
    Oct 27, 2016 at 0:37
  • 1
    You say "I'm sure I can do that" but have you actually tried to checkout those files individually? I'm not at my machine to test, but those glob commands should work, so I'm inclined to think your files don't exist. One other thing you could try is with the option terminator -- with git checkout master -- path/to/*/file.txt Oct 27, 2016 at 1:37
  • I've actually tried the individual way and I could. Adding -- didn't solve. I added note about --.
    – kaitoy
    Oct 27, 2016 at 2:46

1 Answer 1

4

With Git 2.23, you can try the new (experimental for now) command git restore, which does accept a pathspec.

git restore --source=master --staged

Example (in my case, I just restore the working tree, source HEAD):

C:\Users\vonc\git\git\Documentation\technical>echo a>> shallow.txt

C:\Users\vonc\git\git\Documentation\technical>echo a >> rerere.txt

C:\Users\vonc\git\git\Documentation\technical>git st
On branch master
Your branch is up to date with 'origin/master'.

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   rerere.txt
        modified:   shallow.txt

no changes added to commit (use "git add" and/or "git commit -a")

C:\Users\vonc\git\git\Documentation\technical>cd ..

C:\Users\vonc\git\git\Documentation>cd ..

C:\Users\vonc\git\git>git restore Documentation/**/*.txt

C:\Users\vonc\git\git>git st
On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean
1
  • 1
    It is good to know that a * has to follow Documentation/**/ in order for the command to work!
    – zyy
    Nov 18, 2020 at 2:16

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.