按模式递归添加文件
如何通过位于不同目录中的模式(或全局)递归添加文件?
例如,我想添加 A/B/C/foo.java
和 D/E/F/bar.java
(以及其他几个 java 文件)一个命令:
git add '*.java'
不幸的是,这并没有按预期工作。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
我只想添加具有基于 git status 的特定字符串的文件
: grep 字符串 | xargs git 添加
然后能够
git commit -m 'commit msg
提交文件标题中带有“string”的所有已更改文件I wanted to only add files that had a certain string based on
git status
:git status | grep string | xargs git add
and then was able to
git commit -m 'commit msg
to commit all changed files with "string" in the title of the file正如“git:如何递归添加目录子树中与 glob 模式匹配的所有文件?”中提到的,如果你正确地转义或引用你的路径规范通配符(如
'*.java'
),那么是的,git add'*.java'
Git 2.13(Q2 2017)改进了这一点交互式添加:
请参阅 提交 7288e12(2017 年 3 月 14 日),作者:杰夫·金 (
peff
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 153e0d7,2017 年 3 月 17 日)从历史上看,“
diff-index
”支持的路径规范语言较弱,但现在情况已不再如此。As mentioned in "git: How do I recursively add all files in a directory subtree that match a glob pattern?", if you properly escape or quote your pathspec globbing (like
'*.java'
), then yes, git add'*.java'
Git 2.13 (Q2 2017) improves that for interactive add:
See commit 7288e12 (14 Mar 2017) by Jeff King (
peff
).(Merged by Junio C Hamano --
gitster
-- in commit 153e0d7, 17 Mar 2017)Historically the pathspec language supported by "
diff-index
" was weaker, but that is no longer the case.将行放入 ~/.gitconfig
[alias]
addt = !sh -c 'git ls 文件 | grep \"\\.$1*\" | xargs git add' -
如果你想添加所有修改过的java文件可以这样做:
git addt java
同样,如果你想添加所有修改过的Python文件可以这样做:
git addt py
put line in ~/.gitconfig
[alias]
addt = !sh -c 'git ls-files | grep \"\\.$1*\" | xargs git add' -
If you want to add all modified java file can just do:
git addt java
Samely, if you want to add all modified python file can just do:
git addt py
我在 GNUmakefile 中使用的变体
A variation that I used in a GNUmakefile
您可以使用
git add [path]/\*.java
从子目录添加java文件,例如
git add ./\*.java
当前目录。来自
git add
文档:You can use
git add [path]/\*.java
to add java files from subdirectories,e.g.
git add ./\*.java
for current directory.From
git add
documentation:如果某些要添加的文件可能尚未被跟踪,Sergio Acosta 的答案可能是您最好的选择。如果你想将自己限制在 git 已经知道的文件中,你可以将 git-ls-files 与过滤器结合起来:
Git 本身没有提供任何花哨的机制来执行此操作,因为它基本上是一个 shell问题:如何获取文件列表作为给定命令的参数提供。
Sergio Acosta's answer is probably your best bet if some of the files to be added may not already be tracked. If you want to limit yourself to files git already knows about, you could combine
git-ls-files
with a filter:Git doesn't provide any fancy mechanisms for doing this itself, as it's basically a shell problem: how do you get a list of files to provide as arguments to a given command.
使用
zsh
您可以运行:并且所有
*.java
文件将被递归添加。With
zsh
you can run:and all your
*.java
files will be added recursively.有点偏离主题(不是特别与 git 相关),但如果您使用的是 linux/unix,解决方法可能是:
如果您期望路径带有空格:
但我知道这并不完全是您所要求的。
A bit off topic (not specifically git related) but if you're on linux/unix a workaround could be:
And if you expect paths with spaces:
But I know that is not exactly what you asked.
谢尔盖的回答(不要相信我)正在起作用:
使用最近的 git:
用于测试的文件:
Git 状态:
添加 *.txt:
更新状态:
Sergey's answer (don't credit me) is working:
with a recent git:
Files for the test:
Git status:
Adding *.txt:
Updated status:
由于此处未提及,请考虑“魔法签名”
:/
以递归方式添加项目根目录下匹配的任何内容:Tx 至: https://stackoverflow.com/ a/64210713/2586761
As it's not mentioned here, consider also the "magic signature"
:/
to recursively add anything matching below the project root:Tx to: https://stackoverflow.com/a/64210713/2586761
只需使用 git add *\*.java 即可。这将添加根目录和所有子目录中的所有 .java 文件。
Just use
git add *\*.java
. This will add all .java files in root directory and all subdirectories.如果您已经在跟踪文件并对它们进行了更改,现在您想根据模式有选择地添加它们,则可以使用
--modified
标志例如,如果您只想添加此提交的 CSS 更改,您可以
查看
man git-ls-files
了解更多标志If you are already tracking your files and have made changes to them and now you want to add them selectively based on a pattern, you can use the
--modified
flagFor example, if you only want to add the CSS changes to this commit, you can do
See
man git-ls-files
for more flags添加尚未提及的 Windows 命令行解决方案:
Adding a Windows command line solution that was not yet mentioned: