2025-03-16 09:00:01

组合多个git存储库

假设我有一个这样的设置

phd/code/
phd/figures/
phd/thesis/

由于历史原因,这些都有自己的git存储库。但是我想把它们合并成一个,这样可以简化一些。例如,现在我可能要做两组更改,并且必须做一些类似的事情

cd phd/code
git commit 
cd ../figures
git commit

(现在)只要能表演就好了

cd phd
git commit

似乎有几种方法可以使用子模块或从我的子存储库中提取,但这比我想要的要复杂一些。至少让我满意的

cd phd
git init
git add [[everything that's already in my other repositories]]

但这似乎不是一句俏皮话。git中有什么可以帮助我的吗?


当前回答

git-stitch-repo将在命令行中给出的git存储库上处理git-fast-export——all——date-order的输出,并创建一个适合于git-fast-import的流,该流将创建一个新的存储库,其中包含一个新的提交树中的所有提交,该提交树尊重所有源存储库的历史。

其他回答

你可以尝试子树合并策略。它可以让你将回购B合并到回购A。相对于git-filter-branch的优点是它不需要你重写回购A的历史记录(破坏SHA1和)。

亚里士多德Pagaltzis回答的git-stitch-repo只适用于具有简单线性历史的存储库。

MiniQuark的答案适用于所有存储库,但它不处理标记和分支。

我创建了一个与MiniQuark描述的工作方式相同的程序,但是它使用一个合并提交(有N个父级),并且还重新创建了指向这些合并提交的所有标记和分支。

有关如何使用它的示例,请参阅git-merge-repos存储库。

我创造了一个工具来完成这项任务。使用的方法是类似的(内部做一些东西,如——filter-branch),但更友好。是GPL 2.0

http://github.com/geppo12/GitCombineRepo

借助IntelliJ IDEA Community Edition中的git集成,我手动将3个git存储库合并为一个。

Create a new repo, add a new commit to the master branch with an empty README.md file. Add three remotes for the new repo, using the name of the 3 repositories and the remote URL of them respectively. Run Git Fetch. Create a new local branch named temp based on the master branch, so we can start over without pollute the master branch. Checkout the temp branch. Select to only show commits of one remote branch(one repository). Select all the commits and right click to Cherry-Pick them. Create directory structure for this repository, then move the files into it and commit. Repeat the step 4 to 6 for the other 2 remote branch(repository). When everything is OK, merge all the changes in the temp branch into master branch.

然后添加主分支的原始远程URL并推送到它。

Actually, git-stitch-repo now supports branches and tags, including annotated tags (I found there was a bug which I reported, and it got fixed). What i found useful is with tags. Since tags are attached to commits, and some of the solutions (like Eric Lee's approach) fails to deal with tags. You try to create a branch off an imported tag, and it will undo any git merges/moves and sends you back like the consolidated repository being near identical to the repository that the tag came from. Also, there are issues if you use the same tag across multiple repositories that you 'merged/consolidated'. For example, if you have repo's A ad B, both having tag rel_1.0. You merge repo A and repo B into repo AB. Since rel_1.0 tags are on two different commits (one for A and one for B), which tag will be visible in AB? Either the tag from the imported repo A or from imported repo B, but not both.

git-stitch-repo helps to address that problem by creating rel_1.0-A and rel_1.0-B tags. You may not be able to checkout rel_1.0 tag and expect both, but at least you can see both, and theoretically, you can merge them into a common local branch then create a rel_1.0 tag on that merged branch (assuming you just merge and not change source code). It's better to work with branches, as you can merge like branches from each repo into local branches. (dev-a and dev-b can be merged into a local dev branch which can then be pushed to origin).