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

时间:2016-10-26 21:34:54

标签: git git-checkout

我通过Git Bash使用Git for Windows 2.9.3.windows.1。

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

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

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

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

但这很麻烦。相反,我想使用像:

这样的通配符
git checkout master path/to/*/file.txt

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

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

然后,我学会了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'

由于同样的错误,所有这些都没有奏效。即使我在master和pathspec之间添加--,它们也无法工作。 如何在pathspec中使用通配符?

1 个答案:

答案 0 :(得分:0)

在Git 2.23中,您可以尝试新的命令({实验})git restore,该命令确实接受pathspec

git restore --source=master --staged

示例(就我而言,我只是还原工作树,源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