如何使用另一个存储库中的 git 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1811730/
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
How do I work with a git repository within another repository?
提问by Brent O'Connor
I have a Git media repository where I'm keeping all of my JavaScript and CSS master files and scripts that I'll use on various projects.
我有一个 Git 媒体存储库,我将在其中保存我将在各种项目中使用的所有 JavaScript 和 CSS 主文件和脚本。
If I create a new project that's in its own Git repository, how do I use JavaScript files from my media repository in my new project in a way that makes it so I don't have to update both copies of the script when I make changes?
如果我在自己的 Git 存储库中创建一个新项目,我如何在我的新项目中使用媒体存储库中的 JavaScript 文件,以便在进行更改时不必更新脚本的两个副本?
回答by gahooa
The key is git submodules.
关键是git submodules。
Start reading the Submodules chapter of the Git Community Bookor of the Users Manual
Say you have repository PROJECT1, PROJECT2, and MEDIA...
假设您有存储库 PROJECT1、PROJECT2 和 MEDIA...
cd /path/to/PROJECT1
git submodule add ssh://path.to.repo/MEDIA
git commit -m "Added Media submodule"
Repeat on the other repo...
在另一个 repo 上重复...
Now, the cool thing is, that any time you commit changes to MEDIA, you can do this:
现在,很酷的事情是,任何时候您向 MEDIA 提交更改时,您都可以执行以下操作:
cd /path/to/PROJECT2/MEDIA
git pull
cd ..
git add MEDIA
git commit -m "Upgraded media to version XYZ"
This just recorded the fact that the MEDIA submodule WITHIN PROJECT2 is now at version XYZ.
这只是记录了一个事实,即 MEDIA 子模块 WITHIN PROJECT2 现在处于 XYZ 版本。
It gives you 100% control over what version of MEDIA each project uses. git submodulesare great, but you need to experiment and learn about them.
它使您可以 100% 控制每个项目使用的 MEDIA 版本。 git 子模块很棒,但是您需要尝试并了解它们。
With great power comes the great chance to get bitten in the rump.
强大的力量带来了被咬到臀部的大好机会。
回答by Ruslan Kabalin
Consider using subtreeinstead of submodules, it will make your repo users life much easier. You may find more detailed guide in Pro Git book.
考虑使用子树而不是子模块,它会让你的 repo 用户的生活更轻松。你可以在Pro Git book 中找到更详细的指南。
回答by gyim
If I understand your problem well you want the following things:
如果我很好地理解您的问题,您需要以下内容:
- Have your media files stored in one single git repository, which is used by many projects
- If you modify a media file in any of the projects in your local machine, it should immediately appear in every other project (so you don't want to commit+push+pull all the time)
- 将您的媒体文件存储在一个单一的 git 存储库中,该存储库被许多项目使用
- 如果您在本地机器的任何项目中修改媒体文件,它应该立即出现在所有其他项目中(因此您不想一直提交+推+拉)
Unfortunately there is no ultimate solution for what you want, but there are some things by which you can make your life easier.
不幸的是,没有您想要的最终解决方案,但是有一些事情可以让您的生活更轻松。
First you should decide one important thing: do you want to store for every version in your project repository a reference to the version of the media files? So for example if you have a project called example.com, do you need know which style.css it used 2 weeks ago, or the latest is always (or mostly) the best?
首先,您应该决定一件重要的事情:您是否要为项目存储库中的每个版本存储对媒体文件版本的引用?例如,如果您有一个名为 example.com 的项目,您是否需要知道它在 2 周前使用了哪个 style.css,或者最新的始终(或大部分)是最好的?
If you don't need to know that, the solution is easy:
如果您不需要知道这一点,解决方案很简单:
- create a repository for the media files and one for each project
- create a symbolic link in your projects which point to the locally cloned media repository. You can either create a relative symbolic link (e.g. ../media) and assume that everybody will checkout the project so that the media directory is in the same place, or write the name of the symbolic link into .gitignore, and everybody can decide where he/she puts the media files.
- 为媒体文件创建一个存储库,为每个项目创建一个存储库
- 在您的项目中创建一个指向本地克隆媒体存储库的符号链接。您可以创建一个相对符号链接(例如 ../media)并假设每个人都将签出该项目以便媒体目录在同一位置,或者将符号链接的名称写入 .gitignore,然后每个人都可以决定他/她放置媒体文件的地方。
In most of the cases, however, you want to know this versioning information. In this case you have two choices:
但是,在大多数情况下,您想知道此版本信息。在这种情况下,您有两个选择:
Store every project in one big repository. The advantage of this solution is that you will have only 1 copy of the media repository. The big disadvantage is that it is much harder to switch between project versions (if you checkout to a different version you will always modify ALL projects)
Use submodules (as explained in answer 1). This way you will store the media files in one repository, and the projects will contain only a reference to a specific media repo version. But this way you will normally have many local copies of the media repository, and you cannot easily modify a media file in all projects.
将每个项目存储在一个大存储库中。此解决方案的优点是您将只有 1 个媒体存储库副本。最大的缺点是在项目版本之间切换要困难得多(如果您签出到不同的版本,您将始终修改所有项目)
使用子模块(如答案 1 中所述)。通过这种方式,您将媒体文件存储在一个存储库中,并且项目将仅包含对特定媒体存储库版本的引用。但是这样你通常会有很多媒体存储库的本地副本,并且你不能轻松地修改所有项目中的媒体文件。
If I were you I would probably choose the first or third solution (symbolic links or submodules). If you choose to use submodules you can still do a lot of things to make your life easier:
如果我是你,我可能会选择第一个或第三个解决方案(符号链接或子模块)。如果你选择使用子模块,你仍然可以做很多事情来让你的生活更轻松:
Before committing you can rename the submodule directory and put a symlink to a common media directory. When you're ready to commit, you can remove the symlink and remove the submodule back, and then commit.
You can add one of your copy of the media repository as a remote repository to all of your projects.
在提交之前,您可以重命名子模块目录并将符号链接放入公共媒体目录。当您准备好提交时,您可以删除符号链接并删除子模块,然后提交。
您可以将您的媒体存储库副本之一作为远程存储库添加到您的所有项目中。
You can add local directories as a remote this way:
您可以通过这种方式将本地目录添加为远程目录:
cd /my/project2/media
git remote add project1 /my/project1/media
If you modify a file in /my/project1/media, you can commit it and pull it from /my/project2/media without pushing it to a remote server:
如果您修改 /my/project1/media 中的文件,则可以提交它并从 /my/project2/media 中拉取它,而无需将其推送到远程服务器:
cd /my/project1/media
git commit -a -m "message"
cd /my/project2/media
git pull project1 master
You are free to remove these commits later (with git reset) because you haven't shared them with other users.
您可以稍后(使用 git reset)自由删除这些提交,因为您尚未与其他用户共享它们。
回答by ickydime
I had issues with subtrees and submodules that the other answers suggest... mainly because I am using SourceTree and it seems fairly buggy.
我遇到了其他答案所暗示的子树和子模块的问题……主要是因为我使用的是 SourceTree 并且它看起来相当有问题。
Instead, I ended up using SymLinks and that seems to work well so I am posting it here as a possible alternative.
相反,我最终使用了 SymLinks,这似乎运行良好,因此我将其发布在这里作为可能的替代方案。
There is a complete guide here: http://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/
这里有一个完整的指南:http: //www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/
But basically you just need to mklink the two paths in an elevated command prompt. Make sure you use the /J hard link prefix. Something along these lines: mklink /J C:\projects\MainProject\plugins C:\projects\SomePlugin
但基本上你只需要在提升的命令提示符中 mklink 两条路径。确保使用 /J 硬链接前缀。沿着这些路线的东西: mklink /JC:\projects\MainProject\plugins C:\projects\SomePlugin
You can also use relative folder paths and put it in a bat to be executed by each person when they first check out your project.
您还可以使用相对文件夹路径并将其放在一个 bat 中,以便每个人在第一次检查您的项目时执行。
Example: mklink /J .\Assets\TaqtileTools ..\TaqtileHoloTools
示例: mklink /J .\Assets\TaqtileTools ..\TaqtileHoloTools
Once the folder has been linked you may need to ignore the folder within your main repository that is referencing it. Otherwise you are good to go.
链接文件夹后,您可能需要忽略主存储库中引用它的文件夹。否则你很高兴去。
NoteI've deleted my duplicate answer from another post as that post was marked as a duplicate question to this one.
请注意,我已从另一篇文章中删除了我的重复答案,因为该文章被标记为与此问题重复的问题。