如何在 git repo 中链接依赖项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7813030/
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 12:05:59  来源:igfitidea点击:

How can I have linked dependencies in a git repo?

gitgithubdependenciesgit-submodulesgithooks

提问by Lea Verou

In my scripts, I often use libraries (mine or others') that have their own repos. I don't want to duplicate those in my repo and get stuck with updating them every time a new version comes out. However, when somebody clones the repo, it should still work locally and not have broken links.

在我的脚本中,我经常使用拥有自己存储库的库(我的或其他人的)。我不想在我的 repo 中复制那些,并在每次出现新版本时都坚持更新它们。然而,当有人克隆 repo 时,它应该仍然可以在本地工作并且没有断开的链接。

Any ideas about what I could do?

关于我能做什么的任何想法?

回答by Emily

You can do this with submodules in git. In your repository, do:

您可以使用 git 中的子模块执行此操作。在您的存储库中,执行以下操作:

git submodule add path_to_repo path_where_you_want_it

So, if the library's repository had a URL of git://github.com/example/some_lib.gitand you wanted it at lib/some_libin your project, you'd enter:

因此,如果库的存储库有一个 URLgit://github.com/example/some_lib.git并且您希望它lib/some_lib在您的项目中,您将输入:

git submodule add git://github.com/example/some_lib.git lib/some_lib

Note that this needs to be done from the top-level directory in your repository. So don't cdinto the directory where you're putting it first.

请注意,这需要从存储库的顶级目录中完成。所以不要cd进入你首先放置它的目录。

After you add a submodule, or whenever someone does a fresh checkout of your repository, you'll need to do:

添加子模块后,或者每当有人对您的存储库进行全新检出时,您需要执行以下操作:

git submodule init
git submodule update

And then all submodules you've added will be checked out at the same revision you have.

然后您添加的所有子模块都将在您拥有的同一修订版中检出。

When you want to update to a newer version of one of the libraries, cdinto the submodule and pull:

当您想更新到其中一个库的较新版本时,请cd进入子模块并拉取:

cd lib/some_lib
git pull

Then, when you do a git statusyou should see lib/someliblisted in the modified section. Add that file, commit, and you're up to date. When a collaborator pulls that commit into their repository, they'll see lib/somelibas modified until they run git submodule updateagain.

然后,当您执行一个操作时,git status您应该会看到lib/somelib已修改部分中列出的内容。添加该文件,提交,然后您就是最新的。当协作者将该提交拉入其存储库时,他们将看到lib/somelib已修改,直到git submodule update再次运行。