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?
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With