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的历史记录?
我有一个简单的解决办法吗?
我只需首先将
假设您要将
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 |
号
做得很好。