第一个蓝色提交包含应用程序的所有代码以及提交中的一些更改。
我有两个问题。
- 这样的状态是如何产生的?
- 是否可以修复这棵树。也许将蓝色子分支的头部推到红色主分支上?
如有任何想法,我们将不胜感激。
最佳答案
可以使用 git filter-branch
将不相关的历史连接在一起。以下是联机帮助页的摘录:
To set a commit (which typically is at the tip of another history) to be the
parent of the current initial commit, in order to paste the other history behind
the current history:
git filter-branch --parent-filter 'sed "s/^\$/-p <graft-id>/"' HEAD
(if the parent string is empty - which happens when we are dealing with the
initial commit - add graftcommit as a parent). Note that this assumes history
with a single root (that is, no merge without common ancestors happened). If
this is not the case, use:
git filter-branch --parent-filter \
'test $GIT_COMMIT = <commit-id> && echo "-p <graft-id>" || cat' HEAD
or even simpler:
git replace --graft $commit-id $graft-id
git filter-branch $graft-id..HEAD
在您的例子中,$commit-id
是第一个蓝色提交,$graft-id
是最后一个红色提交。
下面是完整过程的示例:
让我们创建一个 repo 和一些红色提交:
$ git init
$ echo a >a
$ git add a
$ git commit -m c1
[master (root-commit) 6a6b98f] c1
1 file changed, 1 insertion(+)
create mode 100644 a
$ echo b >>a
$ git add a
$ git commit -m c2
[master d91d385] c2
1 file changed, 1 insertion(+)
现在创建一个不相关的分支 o1
并添加一些蓝色提交:
$ git checkout --orphan=o1
Switched to a new branch 'o1'
$ echo c >>a
$ git add a
$ git commit -m c3
[o1 (root-commit) ed2b106] c3
1 file changed, 3 insertions(+)
create mode 100644 a
$ echo d >>a
$ git add a
$ git commit -m c4
[o1 5b655a6] c4
1 file changed, 1 insertion(+)
检查我们是否得到了我们想要的:
$ git log --format=short --graph --all
* commit 5b655a615f8a729c123d89180ca1928451b465b2 (HEAD -> o1)
|
| c4
|
* commit ed2b106d7bd0ffef317a723f2921808bc8ad9f45
c3
* commit d91d385c7811ba07f4092133c435b55323562686 (master)
|
| c2
|
* commit 6a6b98fca7150839f607d9d55c6b9f10861375f8
c1
为 ed2b106
(c3,第一个蓝色)创建一个嫁接提交,并将 d91d385
(c2,最后一个红色)作为其新父级:
$ git replace --graft ed2b106d7bd0ffef317a723f2921808bc8ad9f45 d91d385c7811ba07f4092133c435b55323562686
$ git log --format=short --graph --all
* commit 5b655a615f8a729c123d89180ca1928451b465b2 (HEAD -> o1)
|
| c4
|
* commit ed2b106d7bd0ffef317a723f2921808bc8ad9f45 (replaced)
|
| c3
|
| * commit 4656e5bca003770b1a35aff10e3ffb51f7fb1ad9
|/
| c3
|
* commit d91d385c7811ba07f4092133c435b55323562686 (master)
|
| c2
|
* commit 6a6b98fca7150839f607d9d55c6b9f10861375f8
c1
我们得到了 4656e5b
(固定的 c3)作为固定父代的替换,但是替换只是本地的,所以我们现在需要重写所有的蓝色提交:
$ git filter-branch d91d385c7811ba07f4092133c435b55323562686..HEAD
Rewrite 5b655a615f8a729c123d89180ca1928451b465b2 (2/2) (0 seconds passed, remaining 0 predicted)
Ref 'refs/heads/o1' was rewritten
$ git log --format=short --graph
* commit 2cf6b0d3a0ee2deff99bbe327d065f90c82c1c2b (HEAD -> o1)
|
| c4
|
* commit 4656e5bca003770b1a35aff10e3ffb51f7fb1ad9
|
| c3
|
* commit d91d385c7811ba07f4092133c435b55323562686 (master)
|
| c2
|
* commit 6a6b98fca7150839f607d9d55c6b9f10861375f8
c1
4656e5b
(固定的 c3)已经很好(与 ed2b106
相同,原始的 c3,只是父级不同),但是 5b655a6
(c4) 被重写,使其父级为 4656e5b
(固定的 c3)而不是 ed2b106
(原始 c3)。我们现在可以安全地删除替换项,因为我们的历史记录中不再使用 ed2b106
:
$ git replace -d ed2b106d7bd0ffef317a723f2921808bc8ad9f45
Deleted replace ref 'ed2b106d7bd0ffef317a723f2921808bc8ad9f45'
关于git - splinter 的 Git 树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53869576/