regex - 如何在 git checkout 命令中使用正则表达式来指定文件子集

标签 regex git

假设我正在尝试 check out 一组特定的 git 文件,例如app/*^(.tmpl).html,

如何将此正则表达式放入 git checkout 分支 -- (regex)

最佳答案

如“Is there a way to use wildcards with git checkout?”中所述

Git does not deal with the wildcard, but your shell does.

作为noted below ,您需要转义通配符以便 git 对其进行解释:afile\*,或使用简单的引号。

example section of git checkout显示通配符用法:

rm hello.c
git checkout -- '*.c'

Note the quotes around *.c.
The file hello.c will also be checked out, even though it is no longer in the working tree, because the file globbing is used to match entries in the index (not in the working tree by the shell).

例如,另一种方法是使用查找(如“git add *.js” did not add the files in sub-directories ”)

find . -name '*js' -exec git checkout {} \;

你可以用 find -regex 试试你的正则表达式

例如,Tukaz使用(来自 the comments ):

find ./packages/* -maxdepth 1 -name 'package.json' \
     -exec git checkout {} \; 

in order to checkout only the package.json of each project I have and omit dist or nodemodules sub-folders.


更现代的命令(自 2014 年起)是 Git 2.23+ (2019 年第三季度)git restore :

git restore -s=aBranch -- '*.c'

“*.c”遵循 pathspec 中描述的 glob 模式.

关于regex - 如何在 git checkout 命令中使用正则表达式来指定文件子集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24879041/

相关文章:

根据重叠模式删除部分字符串

c# - 正则表达式:很少匹配 * 量词

java - 有没有办法使用字符串参数列表和正则表达式(带组)来构造新字符串?

regex - 删除批处理或 PoSh 的引号之间的逗号

git 阻止模拟另一个用户

java - 如何使用正则表达式模式分割字符串

git - 为什么 Git 在我尝试推送到原点时告诉我 "No such remote ' origin'"?

git - 将一个存储库的提交 merge 到另一个存储库

Git - 在提交时查找文件名

git push 通过 git-lfs 给出 fatal error