关于github:我如何合并2个git存储库?

How can I merge 2 git repositories?

我有两个Git存储库,例如Project1和Project2,Project2位于Project1的子文件夹中:

1
2
/project1/.git (with contents of project2/ ignored)
/project1/project2/.git

我想知道如何将项目2合并到具有相同文件夹结构的项目1中,但要保留项目2的历史记录?

我有一个简单的解决办法吗?


我只需首先将project2中的所有内容移动到一个子目录中,然后简单地将修改后的project2存储库合并到project1中。

假设您要将project2中的PROJECT2_BRANCH分支的内容和历史作为子模块合并到project1中的PROJECT1_BRANCH分支中:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
git clone /project1/project2 ~/tmp_project2
cd ~/tmp_project2
git checkout PROJECT2_BRANCH
mkdir project2
# The following line requires bash, it will not work in a POSIX shell.
# It basically"git mv"s into project2 everything except .git and project2
git ls-tree --name-only -z HEAD|{ while read -d '' f; do git mv"$f" project2; done; }
git commit -m"Moved everything into a new subdirectory project2"

git clone /project1 ~/project1_merged_with_project2
cd ~/project1_merged_with_project2
git checkout PROJECT1_BRANCH
git remote add proj2 ~/tmp_project2
git pull proj2 PROJECT2_BRANCH

git remote remove proj2
rm -rf ~/tmp_project2


最后,我将Project2移动到一个临时文件夹中,然后:

1
git subtree add -P project2 ~/tmp_project2 PROJECT2_BRANCH

做得很好。