提交对子模块的更改,而无需提交父存储库
假设我有一个父存储库 myproject
和一个名为 submodule
的单独存储库,其目录结构如下:
root$ find . -not -path *.git* . ./myproject ./myproject/submodule
现在我将 submodule
添加为myproject
的子模块。
root$ cd myproject myproject$ git submodule add git://url-to-submodule:submodule.git submodule Adding existing repo at 'submodule' to the index
现在,假设我对子模块
进行了一些更改。
myproject$ cd submodule submodule$ touch herpin.txt submodule$ add herpin.txt submodule$ git commit -am "i'm herpin and i'm derpin"
此时,我返回父存储库,并检查 git 状态:
submodule$ cd .. myproject$ git status # On branch master # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: submodule (new commits) # no changes added to commit (use "git add" and/or "git commit -a")
好吧,该死——现在每次我在子模块中提交某些内容时,我也必须提交父存储库。
如果你有一个更复杂的子模块树,它很快就会变得烦人。比方说 - 4 层深。如果我在最里面的子模块进行更改,我必须提交它的父模块、祖父母、曾祖父母和曾曾父母。那是一种令人毛骨悚然的痛苦——。
一定有更好的办法! (不,不嵌套这么多级别不是一个选项。:/ 这不是我的调用...)是否有一种方法可以让 git-commit 通知父存储库提交?
Let's say I have a parent repo myproject
, and a separate repo called submodule
, with the directory structure that's the following:
root$ find . -not -path *.git* . ./myproject ./myproject/submodule
Now I add submodule
as a submodule to myproject
.
root$ cd myproject myproject$ git submodule add git://url-to-submodule:submodule.git submodule Adding existing repo at 'submodule' to the index
Now, let's say i change something to submodule
.
myproject$ cd submodule submodule$ touch herpin.txt submodule$ add herpin.txt submodule$ git commit -am "i'm herpin and i'm derpin"
At this point, I go back to the parent repository, and check the git status:
submodule$ cd .. myproject$ git status # On branch master # Changes not staged for commit: # (use "git add ..." to update what will be committed) # (use "git checkout -- ..." to discard changes in working directory) # # modified: submodule (new commits) # no changes added to commit (use "git add" and/or "git commit -a")
Well, damn it -- now each time I commit something in submodule, I also have to commit the parent.
It gets annoying pretty quickly if you have a more complex submodule tree. Let's say -- 4-levels deep. If I make a change at the inner-most submodule, I have to commit its parent, its grandparent, its great-grandparent, and its great-great-parent. That's a freaking pain in the ---.
There must be a better way! (And no, not nesting so many levels isn't an option. :/ That's not my call to make ...) Isn't there a way where git-commit can notify the parent repositories of the commit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
提交到子模块应该被视为提交到另一个外部库/存储库。
不要在根模块上使用 git status,而在子模块上使用它。
不,您不需要在容器中提交更改,您只需从子模块内提交它们。
然后,您可以进行多项更改、回滚、将子模块保留在稳定分支上等,然后仅将父容器更新为子模块的稳定版本。
更新子模块引用不应该经常进行,而应该只在兼容版本之间切换,否则您将连续出现大量“碰撞”提交,而这些提交只是您发现的更新子模块。
简而言之,在子模块中完成工作,提交,在子模块提交中完成更多工作。完成后,通过提交容器将子模块引用更新为新版本,或者将子模块回滚到最后一个稳定版本,将提交留给将来的开发工作。
Committing to a submodule should be treated like committing to another external library / repository.
Don't use git status on the root, use it on the submodule.
No you don't need to commit the changes in the container, you only need to commit them from within the sub module.
You can then make several changes, roll back, keep the submodule on a stable branch etc, and then only update the parent container to a stable version of the submodule.
Updating a submodule reference shouldn't be done constantly, and should only really swing between compatible versions, otherwise you will have a ton of 'bump' commits in a row that's just updating the sub module as you found out.
So in short, do your work in a submodule, commit, do more work in a submodule commit. Then once done, update the submodule reference to either the new version by committing the container, or rolling back the submodule to the last stable version, leaving the commits for future dev work.
当您对子模块进行更改时,您可以将未提交的更改推送到远程存储库,而无需触摸父项目,如下所示:
cd 子模块
。git stash
git pull;
git checkout
git stash pop
git add
,git commit
git Push <远程名称>; <分支名称>
。现在您已在本地子模块中提交了更改并将它们推送到远程存储库。在您的主项目中,
.gitmodules
应更新并指向您在子模块目录中所做的最后一次提交。现在,您可以选择提交.gitmodules
文件以使用包含更新子模块的提交,也可以放弃并继续使用旧的且未更新的子模块。但是,如果您放弃并运行 git submodule update --remote ,它将提取旧的未更新分支,因此您可能需要在主项目中提交此 .gitmodules 文件以及。这是 Pro Git 书中关于它的一章: https://git -scm.com/book/en/v2/Git-Tools-Submodules
When you make changes to the submodule, you can push your uncommited changes to the remote repository without touching your parent projects as following:
cd submodule
.git stash
git pull <remotename> <branchname>
git checkout <branchname>
git stash pop
git add
,git commit
git push <remotename> <branchname>
.Now you have commited changes in your submodule locally and pushed them to your remote repository. In your main project
.gitmodules
should be updated and point to the last commit you made in your submodule directory. Now it's your choice to either commit your.gitmodules
file to use a commit with updated submodule, or you can discard and continue using old and not updated submodule. But if you discard and rungit submodule update --remote
, it will pull that old not updated branch, so you probably want to commit this.gitmodules
file in your main project as well.Here's a chapter about it from the Pro Git book: https://git-scm.com/book/en/v2/Git-Tools-Submodules