破碎的Git树

时间:2018-12-20 13:20:09

标签: git

我最近继承了一个项目,发现git repo树坏了。enter image description here

第一个蓝色提交包含应用程序的所有代码以及提交中的某些更改。

我有两个问题。

  1. 如何创建这种状态?
  2. 是否可以修复此树。也许将蓝色子分支的头推到红色主分支上?

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

可以使用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是最后一个红色提交。


这是完整过程的示例:

让我们创建一个仓库和一些红色的提交:

$ 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(c2,最后一个红色)为其新父对象,为d91d385(c3,第一个蓝色)创建嫁接提交:

$ 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)已经很好(与原始的c3,ed2b106相同,只是父级不同),但是5b655a6(c4)被重写,因此其父级是4656e5b(固定为c3),而不是ed2b106(原始c3)。现在我们可以放心替换掉,因为我们的历史记录中不再使用ed2b106

$ git replace -d ed2b106d7bd0ffef317a723f2921808bc8ad9f45
Deleted replace ref 'ed2b106d7bd0ffef317a723f2921808bc8ad9f45'