Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 939
kaitoy Avatar asked Oct 26 '16 21:10

kaitoy


People also ask

What is pathspec in git?

The pathspec is the mechanism that git uses for limiting the scope of a git command to a subset of the repository. If you have used much git, you have likely used a pathspec whether you know it or not. For example, in the command git add README.md , the pathspec is README.md .

What does git reset head do?

The git reset HEAD~2 command moves the current branch backward by two commits, effectively removing the two snapshots we just created from the project history. Remember that this kind of reset should only be used on unpublished commits.

Did not match any file known to git checkout?

Git errors: cannot checkout branch - error: pathspec 'branch_name' did not match any file(s) known to git. To fix that you can remove remote origin and link it again. After this you should be bale to switch between the branches as usual.


1 Answers

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
like image 135
VonC Avatar answered Oct 29 '22 21:10

VonC