合并两个Git存储库并保留主记录 [英] Merge two Git repositories and keep the master history
问题描述
我需要将两个Git存储库合并到一个全新的第三个存储库中。
我按照以下说明操作,但结果不符合我的预期。
主人未被签名。 您可以自由使用我的示例存储库 github : 我如何获得到目前的情况: 所有帮助表示感谢!
#假设当前目录是我们希望创建新存储库的位置
#创建新的存储库
git init
#在我们进行合并之前,我们必须有一个初始提交,所以我们将做一个虚拟提交
dir > Read.md
git add。
git commit -m初始提交
#添加远程并获取旧的RepoA
git remote add -f RepoA https://github.com/DimitriDewaele/ RepoA
#将RepoA / master中的文件合并到新的/ master
中git merge RepoA / master --allow-unrelated-history
#做同样的事情对于RepoB
git remote add -f RepoB https://github.com/DimitriDewaele/RepoB
#将RepoB / master中的文件合并到新的/ master
中git merge RepoB / master --allow-unrelated-history
更新:ANSWER
正确答案是rebase,而不是merge 。
- https:/
代码:
pre>
#在repoB
git rebase之上重新运行工作分支(master)RepoB / master
#Rebase teh working branch(master with RepoB)on top op repA
git rebase RepoA / master
还有一个问题。第二个回购失去了父母的观点。我发布了一个后续问题:合并两个Git仓库并保留历史
< img src =https://i.stack.imgur.com/4sHIV.pngalt =在这里输入图片描述>
这很容易,而不是合并使用 rebase 。假设你希望首先拥有 repoA / master
(在获取和添加远程操作之后)
#开始于master
git rebase RepoB / master#将master放置在masterB上面
git rebase RepoA / master#在masterA上放置master(masterB)
请注意,rebase具有潜在的破坏性并且起初有点不直观,所以我强烈建议先阅读关于它们的信息 strong>(上面链接中的SO文档是一个很好的开始)
I need to merge two Git repositories into a brand new, third repository. I followed the following instructions and they work, but the result is not what I expected. Merge two Git repositories without breaking file history
Look at this:
The master is not alligned.
- Is it possible to merge everything onto the same master?
- I would like to have a clean history on the master.
You are free to use my example repository's on github:
This is my merged result.
This is the situation that I would like to have: (painted solution!!!)
How I got to the the current situation:
# Assume the current directory is where we want the new repository to be created
# Create the new repository
git init
# Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
dir > Read.md
git add .
git commit -m "initial commit"
# Add a remote for and fetch the old RepoA
git remote add -f RepoA https://github.com/DimitriDewaele/RepoA
# Merge the files from RepoA/master into new/master
git merge RepoA/master --allow-unrelated-histories
# Do the same thing for RepoB
git remote add -f RepoB https://github.com/DimitriDewaele/RepoB
# Merge the files from RepoB/master into new/master
git merge RepoB/master --allow-unrelated-histories
All help is appreciated!
UPDATE: ANSWER
The correct answer is to rebase, instead of merge.
Code:
# Rebase the working branch (master) on top of repoB
git rebase RepoB/master
# Rebase teh working branch (master with RepoB) on top op repoA
git rebase RepoA/master
One problem remains. The 2nd repo loses the parent view. I posted a follow-up question for this: Merge two Git repos and keep the history
It's easy, instead of merging use rebase. Assuming you want to have repoA/master
first just do (after fetching and adding remotes)
# starting on master
git rebase RepoB/master # place master on top of masterB
git rebase RepoA/master # place master (with masterB) on top of masterA
Note that rebases are potentially destructive and a bit unintuitive at first, so I strongly recommend reading about them first (SO documentation in the link above is a good place to start)
这篇关于合并两个Git存储库并保留主记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!