修复git双提交历史记录

ysi*_*son 18 git github git-filter-branch

前git filter-branch几天我不得不跑.我按照github上的说明进行操作,但出了点问题.我认为团队中的某个人没有在本地分支上运行rebase,而是合并了这些更改.从那时起,提交日志就会填充双重提交,例如:

commit b0c03ec925c0b97150594a99861d8f21fd3ab22d
Author: XXX
Date:   Wed Mar 19 17:01:52 2014 -0400

    Removed most clearfixs in templates

commit f30c21d21b5ea715a99b0844793cb4b5f5df97a1
Author: XXX
Date:   Wed Mar 19 17:01:52 2014 -0400

    Removed most clearfixs in templates

commit 2346be43d0e02d3987331f0a9eeb2f12cd698ede
Author: XXX
Date:   Wed Mar 19 16:40:26 2014 -0400

    new redirect logic

commit 1383070b31bde1aaa9eda7c2a9bcb598dd72247b
Merge: d1e2eb6 94e07fe
Author: XXX
Date:   Wed Mar 19 16:28:41 2014 -0400

    Merge branch 'develop' of github.com:xxx/xxx into develop

commit 79ce7824688cf2a71efd9ff82e3c7a71d53af229
Merge: 6079061 1ed3967
Author: XXX
Date:   Wed Mar 19 16:28:41 2014 -0400

    Merge branch 'develop' of github.com:xxx/xxx into develop

commit d1e2eb645a4fe2a1b3986082d0409b4075a0dbc9
Author: XXX
Date:   Wed Mar 19 16:28:36 2014 -0400

    Fixed broken responsiveness for companies listing page and code refactoring.

commit 6079061f6ef1f856f94d92bc0fdacf18854b8a89
Author: XXX
Date:   Wed Mar 19 16:28:36 2014 -0400

    Fixed broken responsiveness for companies listing page and code refactoring.
Run Code Online (Sandbox Code Playgroud)

奇怪的是,并非所有提交都是双倍的,例如上面的"新重定向逻辑".有什么办法可以解决这个问题吗?这是相对温和的,但现在我们的提交历史看起来像垃圾.这篇SO帖子建议将其保留原样,但为了后代,我宁愿有一个干净的提交历史.

小智 23

完成该任务的命令是:

git rebase -i HEAD~7
Run Code Online (Sandbox Code Playgroud)

这将打开你的编辑器,如下所示:

pick f392171 Removed most clearfixs in templates
pick ba9dd9a Removed most clearfixs in templates
pick df71a27 Unew redirect logic
pick 79ce782 Merge branch 'develop' of github.com:xxx/xxx into develop
pick 1383070 Merge branch 'develop' of github.com:xxx/xxx into develop
...
Run Code Online (Sandbox Code Playgroud)

现在你可以告诉git如何处理每个提交.让我们保留提交f392171,我们添加了我们的功能.我们将以下两个提交压缩到第一个 - 让我们一个干净.

将您的文件更改为:

pick f392171 Removed most clearfixs in templates
squash ba9dd9a Removed most clearfixs in templates
pick df71a27 Unew redirect logic
pick 79ce782 Merge branch 'develop' of github.com:xxx/xxx into develop
squash 1383070 Merge branch 'develop' of github.com:xxx/xxx into develop
Run Code Online (Sandbox Code Playgroud)

当您保存并退出编辑器时,Git会应用所有两个更改,然后返回编辑器以合并三个提交消息:

# This is a combination of  commits.
# The first commit's message is:
Removed most clearfixs in templates

# This is the 2nd commit message:

Removed most clearfixs in templates
Run Code Online (Sandbox Code Playgroud)

完成后,保存并退出编辑器.Git现在将提交压缩成一个.全部完成!

然后你必须这样做

git push origin your-branch -f
Run Code Online (Sandbox Code Playgroud)

强制您将本地提交更改到远程分支.

注意:您必须对每个重复的提交进行压缩.


myD*_*ode 5

@VAIRIX 的回答是完美的,但在某些复杂的情况下,重复提交不会彼此相邻,因此压缩无济于事。

因此,以下历史记录,(假设 a~ 是 a 的副本)

 # h
 # g
 # f
 # c~
 # b~
 # a~
 # e
 # d
 # c
 # b
 # a
Run Code Online (Sandbox Code Playgroud)

要遵循的命令:(如@VAIRIX 或下面的回答中所述,如果您想使用 master 进行变基)git rebase master -i(更好地遵循git rebase -i HEAD~n以避免变基 头痛)

现在!1)压缩重复提交如下:

 pick h
 pick g
 pick f
 pick c~
 s b~
 s a~
 pick e
 pick d
 pick c
 pick b
 pick a
Run Code Online (Sandbox Code Playgroud)

现在,这将压缩您在 c 中的提交

 # h
 # g
 # f
 # c~ (having changes of a~ and b~)
 # e
 # d
 # c
 # b
 # a
Run Code Online (Sandbox Code Playgroud)

就我而言, c~ 是 c 的反提交,所以我只需要再次执行该过程,但现在不是用 挤压,而是s用d

 pick h
 pick g
 pick f
 d c~ (having changes of a~ and b~)
 pick e
 pick d
 pick c
 pick b
 pick a
Run Code Online (Sandbox Code Playgroud)

现在,您的历史记录将删除所有重复的提交。现在,您可以与您使用的原始分支进行比较,该分支git diff对您的此分支进行了重复提交。如果你做得很完美,就不应该有任何差异。

这个过程可能看起来有点长,但你可以放心,你没有错过任何提交。

  • +1 也为您的用户名:D。是的,应该非常小心地选择挤压和丢弃,并使用提交数量开始挤压。HEAD~10 或类似的东西。 (2认同)