简体   繁体   English

除了子模块中的新提交之外,我如何`git add -u`?

[英]How do I `git add -u` except new commits in submodules?

My current git status is:我目前的git status是:

modified:   Gemfile
modified:   Gemfile.lock
modified:   ... many more files
modified:   submodule_1 (new commits)
modified:   submodule_2 (new commits)
modified:   ... many more updated submodules ...

How do I git add -u except new commits in submodules?除了子模块中的新提交,我如何git add -u

What I want to achieve is an equivalent of git add -u , then git reset HEAD submodule_1 submodule_2 ... submodule_n , or an equivalent of git add -u Gemfile Gemfile.lock ...all other files that are not submodules...我想要实现的是git add -u的等价物,然后git reset HEAD submodule_1 submodule_2 ... submodule_n ,或git add -u Gemfile Gemfile.lock ...all other files that are not submodules...

Once git add -u is done, you would need to reset all submodule paths.完成 git add -u 后,您需要重置所有子模块路径。
As documented in " List submodules in a git repository "如“在 git 存储库中列出子模块”中所述

git config --file .gitmodules --get-regexp path | awk '{ print $2 }' | xargs git reset -- 

This would list all submodule paths and do a git reset on each one.这将列出所有子模块路径并对每个子模块进行git reset

A similar idea, from this gist :类似的想法,来自这个要点

for i in `git config -f .gitmodules --get-regexp path | cut -d" " -f2` ; do git reset -- $i ; done

Final solution, a bashrc/zshrc funnction that does git add -u and resets submodules:最终解决方案,一个 bashrc/zshrc 函数,它执行git add -u并重置子模块:

gitaddus() {
  git add -u
  git config -f .gitmodules --get-regexp path | awk '{ print $2 }' | xargs git reset -- >/dev/null
  git status
}

(There's still a room for improvements, eg check which submodules are already added to the index so we don't git reset these.) (仍有改进的空间,例如检查哪些子模块已经添加到索引中,这样我们就不会git reset这些。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM