两个Git仓库共享通用代码 [英] Two Git repositories share common code

查看:686
本文介绍了两个Git仓库共享通用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个包含客户端部分和服务器部分的项目。我希望客户端和服务器代码位于不同的Git存储库中,以便清晰地分离代码,但是,它们共享部分文件,理想情况下,共享文件在客户端和服务器上始终保持相同。



我应该怎么做?是否有更好的方法为公共代码提供第三个存储库?

解决方案

我不会给出你应该怎么做这个。但我会解释如何完成将第三个存储库与通用代码集成到其他存储库:这是git子模块的工作。 git子模块允许你在指定的路径上引用git仓库的快照:

  git submodule add git:// example。 com / repository.git path 

然后创建提交。现在给定存储库的快照在路径中引用。



填充子模块


  1. 执行 git子模块init

  2. 执行 git子模块更新

每个配置的子模块都会更新以匹配它应该看起来像的状态。

将子模块更新为以后的提交




  1. 切换到子模块
  2. 执行 git pull / git fetch + git checkout 将子模块更新为所需的提交/标记
  3. 创建一个新提交来更新 .gitmodules中的提交 file

查看官方手册了解有关子模块的更多信息。


So I have a project which has the client parts and the server parts. I would like the client and the server code to be in different Git repositories for clear separation of the code, however, they share some of the files and ideally, the shared files will always be identical in both client and server.

How should I go about this? Is it better to have the third repository for the common code?

解决方案

I will not give advice on "How you should go about this". But I'll explain how to accomplish integrating a third repository with common code into the other repositories: This is a job for git submodules. git submodules allow you to reference a snapshot of a git repository at a specified path:

git submodule add git://example.com/repository.git path

Afterwards create a commit. Now a snapshot of the given repository is referenced at path.

Populating the submodule

  1. Execute git submodule init
  2. Execute git submodule update

Every configured submodule will be updated to match the state it is supposed to look like.

Updating a submodule to a later commit

  1. Change into the directory of the submodule
  2. Perform a git pull / git fetch + git checkout to update the submodule to the desired commit / tag
  3. Create a new commit to update the commit in the .gitmodules file

Take a look at the official manual for further information on submodules.

这篇关于两个Git仓库共享通用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆