簡體   English   中英

git將沒有共同祖先的多個倉庫中的工作合並到新倉庫中

[英]git merge work from multiple repos with no common ancestor into new repo

我有一些倉庫,我想合並到一個新的主倉庫中,所有這些倉庫都有獨特的歷史記錄(在它們之間絕對沒有共同的提交)。 我已經創建了新的主存儲庫,在其他每個存儲庫上都創建了一個分支,並將每個分支推送到主存儲庫。 現在在master_repo中,使用git merge -s ours以及簡單的git merge(將我從頭開始都嘗試過),將每個分支合並到master分支中不會產生任何沖突...但是這些合並嘗試都失敗了文件夾從其他分支轉移到master分支。 我認為部分問題是沒有共同的祖先承諾。 我可以重現以下問題:

在test_merge中設置的目錄結構:master_repo(帶有文件夾01)other_repoA(帶有文件夾03)other_repoB(帶有文件夾01和09)

然后在Cygwin終端中(“ ...” =裁剪的終端消息):

$ cd master_repo/
$ git init
Initialized empty Git repository in .../test_merge/master_repo/.git/

在master_repo的master分支上添加文件並進行通信

$ echo "text in file01" > 01/file01
$ git add *
warning: LF will be replaced by CRLF in 01/file01.
The file will have its original line endings in your working directory.
$ git commit -m "master_repo: first commit"
[master (root-commit) ...blah, blah...
1 file changed, 1 insertion(+)...blah, blah

添加文件,與other_repoA的主分支通信,添加新分支,並將新分支推入master_repo

$ cd ../other_repoA
$ git init
Initialized empty Git repository...blah,blah
$ echo "text in file03" > 03/file03
$ git add *
...
$ git commit -m "other_repoA: first commit"
...
$ git checkout -b branchA
...
$ git status
On branch branchA
nothing to commit, working directory clean
$ git push ../master_repo branchA
To ../master_repo
 * [new branch]      branchA -> branchA

添加文件,在other_repoB的主分支上通信,添加新分支,並將新分支推入master_repo

$ cd ../other_repoB
$ git init
...
$ echo "text in file01 from other_repoB" > 01/file01
$ echo "text in file09 from other_repoB" > 09/file09
$ git add *
$ git commit -m "other_repoB: first commit"
...
$ git checkout -b branchB
...
$ git status
On branch branchB
nothing to commit, working directory clean
$ git push ../master_repo branchB
To ../master_repo
 * [new branch]      branchB -> branchB

看看master_repo

$ cd ../master_repo
$ git status
On branch master
nothing to commit, working directory clean
$ git branch
  branchA
  branchB
* master
$ ls
01
$ git checkout branchA
Switched to branch 'branchA'
$ ls
03
$ git checkout branchB
Switched to branch 'branchB'
$ ls
01  09

合並嘗試:請注意,分支A和B中的內容似乎被忽略了

$ git checkout master
Switched to branch 'master'
$ git merge -s ours branchA
$ ls
01
$ git merge -s ours branchB
Merge made by the 'ours' strategy.
$ ls
01
$ cat 01/file01
text in file01
$ git checkout branchB
Switched to branch 'branchB'
$ ls
01  09
$ cat 01/file01
text in file01 from other_repoB

我希望看到的最終結果是將文件夾03和09添加到master_repo,並將“ from other_repoB”附加到01 / file01。 我正在尋找甚至可以使用git(而不是手動復制任何東西)的東西嗎?

您需要添加一些read-tree工作來進行-s ours null合並:

git merge -s ours --no-commit branchA    # null `-s ours` for `branchA` parent
git read-tree -u --prefix= branchA       # add the branchA tree at the project root
git commit

git read-tree文檔

讀取樹是簽出和合並的核心,還有許多其他事情,您想要做的一切都將樹和索引結合在一起,通常還需要對一些工作樹進行更新,您可以通過這種方式來構建。

我在這里的文檔中找到了答案:

https://git-scm.com/book/en/v2/Git-Tools-Advanced-Merging#Other-Types-of-Merges

解釋了有關-Xours選項的信息...

該分支上的所有其他非沖突更改都已成功合並。

以及-s ours所作所為,這有點不同...

如果您想做這樣的事情,但是甚至連Git都沒有嘗試合並另一側的更改,則有一個更嚴厲的選擇,這就是“我們的”合並策略。 這與“我們的”遞歸合並選項不同。

這基本上將進行偽合並。 它將記錄兩個分支都作為父級的新合並提交,但它甚至不會查看您要合並的分支。它將簡單地記錄合並的結果作為當前分支中的確切代碼。

我正在尋找的是-Xours選項。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM