kaitoy
kaitoy

Reputation: 1675

How can I use a wildcard in git pathspec?

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?

Upvotes: 10

Views: 3627

Answers (1)

VonC
VonC

Reputation: 1329812

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

Upvotes: 4

Related Questions