如何在git pathspec中使用通配符?

kai*_*toy 5 git git-checkout

我正在通过Git Bash使用Windows 2.9.3.windows.1的Git。

现在dev分支已签出,我想从master分支签出一些不在工作目录中的文件。这些文件具有相同的名称,并存储在类似的目录中,例如:

path/to/a/file.txt
path/to/that/file.txt
path/to/the/file.txt
path/to/this/file.txt
Run Code Online (Sandbox Code Playgroud)

我确定可以通过一一指定文件来做到这一点:

git checkout master path/to/a/file.txt path/to/that/file.txt path/to/the/file.txt path/to/this/file.txt
Run Code Online (Sandbox Code Playgroud)

但这很麻烦。相反,我想使用通配符,例如:

git checkout master path/to/*/file.txt
Run Code Online (Sandbox Code Playgroud)

当我尝试此命令时发生错误:

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

然后,我学习了pathspec并尝试:

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'
Run Code Online (Sandbox Code Playgroud)

由于相同的错误,所有这些都不起作用。即使我--在master和pathspec之间添加,它们也不起作用。如何在pathspec中使用通配符?

Von*_*onC 4

使用 Git 2.23,您可以尝试新的(目前处于实验阶段)命令git restore,它接受pathspec

git restore --source=master --staged
Run Code Online (Sandbox Code Playgroud)

示例(在我的例子中,我只是恢复工作树,源 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
Run Code Online (Sandbox Code Playgroud)