簡體   English   中英

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

[英]How can I use a wildcard in git pathspec?

我正在通過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

我確定可以通過一一指定文件來做到這一點:

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'

由於相同的錯誤,所有這些都不起作用。 他們甚至如果我添加不工作--主機和pathspec之間。 如何在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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM