2017-02-10 127 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