gpt4 book ai didi

git - 将多个孤立分支 merge 为一个分支

转载 作者:太空狗 更新时间:2023-10-29 12:58:13 24 4
gpt4 key购买 nike

我有一个包含多个存储库的目录,它看起来像这样:

folder
|- repo1
| |- .git
| |- File1.txt
|- repo2
| |- .git
| |- File2.txt
|- repo3
| |- .git
| |- File3

我想将它们 merge 到一个 git 存储库中,我通过以下 these说明。

在这些指令之后,我有以下结构:

folder
|- .git
|- repo1
| |- File1.txt
|- repo2
| |- File2.txt
|- repo3
| |- File3

那太好了。但现在我有主要分支(来自 repo1)和历史上的两个孤儿分支:

* Merge repo3
|\
| * Add third file
* Merge repo2
|\
| * Add second file
* Add first file

我想得到的是这样的

* Merge repo 3
* Add third file
* Merge repo 2
* Add second file
* Add first file

甚至更好

* Add third file
* Add second file
* Add first file.

这样的事情可能吗?以及如何?


VonC 提供的答案的更多详细信息。

我从这个开始:

$ git log --graph
* commit 888bb0d7a81a40f8b42da7ad3f02103e898f5c1f
|\ Merge: cf85078 eab1269
| | Author: Bojan Delic <...>
| | Date: Sun Oct 6 13:12:36 2013 +0200
| |
| | Merge repo3
| |
| * commit eab1269be53929450c34c30416820c13a8058678
| Author: Bojan Delic <...>
| Date: Sun Oct 6 13:07:39 2013 +0200
|
| Add third file
|
* commit cf85078ca96d52f2277ad7e4c6f45b04a38cf7ee
|\ Merge: 92fa311 42333f8
| | Author: Bojan Delic <...>
| | Date: Sun Oct 6 13:12:33 2013 +0200
| |
| | Merge repo2
| |
| * commit 42333f8589e20d29e5e444d5e16ff1a5d63b8288
| Author: Bojan Delic <...>
| Date: Sun Oct 6 13:07:09 2013 +0200
|
| Add second file
|
* commit 92fa3113507548a62407431188c308685f72865d
Author: Bojan Delic <...>
Date: Sun Oct 6 13:06:39 2013 +0200

Add first file

然后我做:

$ git branch -v
* master 888bb0d Merge repo3
$ git checkout -b branch2 eab1269be53929450c34c30416820c13a8058678
$ git checkout -b branch3 42333f8589e20d29e5e444d5e16ff1a5d63b8288
$ git checkout branch2
Switched to branch 'branch2'
$ git rebase master
First, rewinding head to replay your work on top of it...
Fast-forwarded branch2 to master.
$ git checkout master
Switched to branch 'master'
$ git merge branch2
Already up-to-date.
$ git checkout branch3
Switched to branch 'branch3'
$ git rebase master
First, rewinding head to replay your work on top of it...
Fast-forwarded branch3 to master.
$ git checkout master
Switched to branch 'master'
$ git merge branch3
Already up-to-date.

之后:

$ git log --graph
* commit 888bb0d7a81a40f8b42da7ad3f02103e898f5c1f
|\ Merge: cf85078 eab1269
| | Author: Bojan Delic <...>
| | Date: Sun Oct 6 13:12:36 2013 +0200
| |
| | Merge repo3
| |
| * commit eab1269be53929450c34c30416820c13a8058678
| Author: Bojan Delic <...>
| Date: Sun Oct 6 13:07:39 2013 +0200
|
| Add third file
|
* commit cf85078ca96d52f2277ad7e4c6f45b04a38cf7ee
|\ Merge: 92fa311 42333f8
| | Author: Bojan Delic <...>
| | Date: Sun Oct 6 13:12:33 2013 +0200
| |
| | Merge repo2
| |
| * commit 42333f8589e20d29e5e444d5e16ff1a5d63b8288
| Author: Bojan Delic <...>
| Date: Sun Oct 6 13:07:09 2013 +0200
|
| Add second file
|
* commit 92fa3113507548a62407431188c308685f72865d
Author: Bojan Delic <...>
Date: Sun Oct 6 13:06:39 2013 +0200

Add first file

$ git branch -a
branch2
branch3
* master

最佳答案

您可以在当前 master(来自 repo1)的基础上对两个孤立分支进行 rebase

git checkout branch2
git rebase master
git checkout master
git merge branch2 # fast-forward master to branch2

git checkout branch3
git rebase master
git checkout master
git merge branch3 # fast-forward master to branch3

但是,在您的情况下(在对问题进行编辑之后),没有什么可做的:

那两个孤立分支已经 merge 到master中。
这就是为什么 rebase 只会将它们快速转发到 master... 因为它们已经 merge 到 master 中。
您可以安全地忽略 eab126942333f:如果它们未被标记或分支引用,它们将被垃圾收集。
例如,尝试克隆您的存储库并查看这些提交是否仍然存在(如果是,请在该克隆上尝试 git gc --aggressive --prune=now)


注意:git 2.9(2016 年 6 月)将允许您 merge 孤立分支(commit d04aa7e 仅使用 --allow-unrelated-histories 选项:

git merge --allow-unrelated-histories a b

关于git - 将多个孤立分支 merge 为一个分支,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19208359/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com