强制 Git 子模块始终保持最新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10443627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:37:12  来源:igfitidea点击:

Force Git submodules to always stay current

gitgit-submodules

提问by Ben

I love git submodules. Also, I hate git submodules. What I love about them is how it enables to you to cleanly compartmentalize dependencies etc. I get the point of having them point to a specific commit on a repo, I do. But in my case, I'm building a library that will be used in another project, so I want to keep it in that seperate repo.

我喜欢 git 子模块。另外,我讨厌 git 子模块。我喜欢它们的地方在于它如何使您能够干净地划分依赖项等。我明白让它们指向 repo 上的特定提交,我是这样做的。但就我而言,我正在构建一个将在另一个项目中使用的库,所以我想将它保存在那个单独的仓库中。

However, the annoyance comes when I'm working on a daily basis on this library and I constantly have to switch back to the app using my library to commit the pointer update.

然而,当我每天在这个库上工作时,烦恼就会出现,我经常不得不使用我的库切换回应用程序来提交指针更新。

So, is it possible to have a git submodule just always be on the head of the repo it's pointing at while I'm constantly updating and adding to this library?

那么,当我不断更新和添加到这个库时,是否有可能让 git 子模块始终位于它指向的存储库的头部?

采纳答案by Greg Hewgill

No, and this is by design. If there were a way to point a submodule to the "current head" of some other repository, then it would be impossible to retrieve a historical version (such as a tagged version) from the main repository. It wouldn't know which version of the submodule to check out.

不,这是设计使然。如果有一种方法可以将子模块指向某个其他存储库的“当前头”,那么就不可能从主存储库中检索历史版本(例如标记版本)。它不知道要检查哪个版本的子模块。

Having said that, you may be interested in the git subtreescript. This offers a different way of working with submodules that may be more compatible with your workflow. I was just reminded of this by the recent post on HN.

话虽如此,您可能对git subtree脚本感兴趣。这提供了一种不同的子模块工作方式,可能与您的工作流程更兼容。最近关于 HN 的帖子提醒了我这一点。

回答by VonC

As I mention in "git submodule tracking latest", you can since git 1.8.2 (March 2013) make a submodule track the HEAD of branch:

正如我在“ git submodule tracking latest”中提到的,从 git 1.8.2(2013 年 3 月)开始,您可以让子模块跟踪分支的 HEAD:

git submodule add -b <branch> <repository> [<path>]

A submodule SHA1 is still recorded in the parent repo as a gitlink(special entry in the index)

子模块 SHA1 仍作为gitlink记录在父仓库中索引中的特殊条目

But a git submodule update --remotewill update that entry to the SHA1 matching the HEAD of a branch of the submodule remote repo.

但是 agit submodule update --remote会将该条目更新为与子模块远程存储库的分支的 HEAD 匹配的 SHA1。

If you have an existing submodule, you can make it follow a branchwith:

如果你有一个现有的子模块,你可以让它跟随一个分支

cd /path/to/your/parent/repo
git config -f .gitmodules submodule.<path>.branch <branch>

cd path/to/your/submodule
git checkout -b branch --track origin/branch
  # if the master branch already exist:
  git branch -u origin/master master

cd /path/to/your/parent/repo
git add path/to/your/submodule
git commit -m "Make submodule tracking a branch"

回答by Sailesh

Why don't you make changes inside the submodule directory, which itself is a git repo? This way, your app will always have updated library.

为什么不在子模块目录中进行更改,它本身就是一个 git repo?这样,您的应用程序将始终拥有更新的库。

Caveats:

注意事项:

  1. You still need to commit the submodule change inside your app repo to put the change in version control (for the app).

  2. If there are more apps than one that are using this library, than this is not going to work, since only one app will be up-to-date at any given time.

  1. 您仍然需要在应用程序存储库中提交子模块更改以将更改放入版本控制(对于应用程序)。

  2. 如果使用此库的应用程序多于一个,那么这将不起作用,因为在任何给定时间只有一个应用程序是最新的。