2017-02-10 133 views
3

我需要将两个Git存储库合并成一个全新的第三个存储库。 我按照以下说明操作,但结果不符合我的预期。 Merge two Git repositories without breaking file history合并两个Git存储库并保留主记录

看看这个:

Git Merge operation

主不神韵:。

  • 是否有可能将所有内容合并到同一个主机上?
  • 我想对主人有一个清晰的历史。

您可以自由使用我的例子库的在GitHub上

这是我合并结果

这是我想有这种情况:(涂液!)

Painted solution!!

如何我到了目前的情况:

# 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 

所有帮助表示赞赏!

UPDATE:ANSWER

正确的答案是衍合,而不是合并。

代码:

# 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 

一个问题仍然存在。第二个回购失去了父母的观点。我张贴这是一个后续问题:Merge two Git repos and keep the history

enter image description here

+1

你能更详细地解释你期待什么以及这有什么不同吗? –

+0

你拥有一个主人的所有东西,我不确定你的意思是“对齐” - 尝试从你的本地移除RepoA和RepoB遥控器,然后看看图形的样子。 – Adrian

+0

我已添加“涂漆解决方案” –

回答

1

这很容易,而不是合并使用rebase。假设你想有repoA/master第一只是做(获取和添加遥控器后)

# 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 

注意底垫有潜在的破坏性和开头有点不直观,所以我强烈建议阅读他们先(SO在文档上面的链接是一个很好的开始)

+0

感谢您的正确评价。一个小问题:RepoA的rebase使我放弃合并历史。看到上面的图片(我在上面添加了当前状态)。可以保留这个吗? –

+0

@DimitriDewaele尝试'rebase'的'--preserve-merges'选项 – Dunno

+0

正如@ DimitriDewaele的后续问题所指出的那样,'rebase'确实不是您想要用于此目的的:http://stackoverflow.com /一个/54249分之42457384 – dahlbyk