git 将没有共同祖先的多个存储库中的工作 merge 到新存储库中

标签 git version-control merge

我有一些存储库,我想将其 merge 到一个新的主存储库中,并且所有这些存储库都有独特的历史(其中绝对没有共同的提交)。我创建了新的主存储库,在其他每个存储库上创建了一个分支,并将每个分支推送到主存储库。现在在 master_repo 中,使用 git merge -s ours 和简单的 git merge (我从头开始尝试过)将每个分支 merge 到 master 分支中都不会产生冲突......但是我失败了这些 merge 尝试将其他分支的文件夹复制到主分支中。我认为部分问题在于没有共同的祖先提交。我可以按如下方式重现该问题:

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

merge 尝试:请注意,分支 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空 merge :

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 docs

读取树是 checkout 和 merge 以及许多其他功能的核心,任何您想要结合树和索引以及通常一些工作树更新的事情,都可以通过这种方式构建。

关于git 将没有共同祖先的多个存储库中的工作 merge 到新存储库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35737857/

相关文章:

git - Gradle bitbucket/git 发布插件

git - 如何排除要推送的文件?

git - Capistrano 在 deploy.rb 更改为新 repo 后寻找旧的 Github repo

git - 如果我将代码存储在一个分支中并更改到另一个分支会发生什么?

git - git reset 中的歧义参数

Git:生成在 "feature"分支上进行的所有提交的补丁,而不引用提交 ID

version-control - TFS 非 Windows 用户

java - 合并排序不能递归地工作

python - pandas:合并帮助两个数据框

r - 合并具有重复项的 data.frames