gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 13:27:11 27 4
gpt4 key购买 nike

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

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

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 之间添加 -- 它们也不起作用。如何在路径规范中使用通配符?

最佳答案

使用 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

关于git - 如何在 git pathspec 中使用通配符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40272472/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com