修复 git 双重提交历史
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22817360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fixing git double-commit history
提问by ysimonson
I had to do run git filter-branch
the other day. I followed the instructions on github, but something went wrong. I thinksomeone on the team didn't run rebase on a local branch, and instead merged the changes. Ever since, the commit log is filled with double-commits, e.g.:
前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.
Weirdly enough, not all the commits are doubled-up, such as "new redirect logic" above. Is there anything I can do to fix this? It's relatively benign, but now our commit history looks like crap. This SO postsuggested just leaving it as-is, but I'd rather have a clean commit history for the sake of posterity.
奇怪的是,并不是所有的提交都是加倍的,比如上面的“新重定向逻辑”。我能做些什么来解决这个问题吗?这是相对良性的,但现在我们的提交历史看起来像废话。这篇SO 帖子建议保持原样,但为了后代,我宁愿拥有一个干净的提交历史。
回答by VAIRIX
The command to accomplish that is:
完成它的命令是:
git rebase -i HEAD~7
This will open up your editor with something like this:
这将打开您的编辑器,如下所示:
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
...
Now you can tell git what to do with each commit. Let's keep the commit f392171, the one where we added our feature. We'll squash the following two commits into the first one - leaving us with one clean.
现在你可以告诉 git 对每次提交做什么。让我们保留提交 f392171,这是我们添加功能的地方。我们会将以下两个提交压缩到第一个提交中 - 留下一个干净的。
Change your file to this:
将您的文件更改为:
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
When you save and exit the editor, Git applies all two changes and then puts you back into the editor to merge the three commit messages:
当您保存并退出编辑器时,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
When done, save and quit your editor. Git will now squash the commits into one. All done!
完成后,保存并退出编辑器。Git 现在会将提交压缩为一个。全部完成!
Then you have to do
然后你必须做
git push origin your-branch -f
to force your locally commits changes into remote branch.
强制您在本地提交更改到远程分支。
Note: You have to do a squash to every duplicated commit.
注意:您必须对每个重复的提交进行压缩。
回答by myDoggyWritesCode
Answer by @VAIRIX is perfect but there are complex cases where duplicate commits doesn't appear adjacent to each other, so squashing won't help.
@VAIRIX 的回答是完美的,但在某些复杂的情况下,重复提交不会彼此相邻,因此压缩无济于事。
So taking below history, (assume a~ is duplicate of a)
所以取下面的历史,(假设 a~ 是 a 的副本)
# h
# g
# f
# c~
# b~
# a~
# e
# d
# c
# b
# a
Command to follow: (as told in answer by @VAIRIX or below if you want to rebase with master)
git rebase master -i
(Better follow git rebase -i HEAD~n
to avoid rebasing headache)
要遵循的命令:(如@VAIRIX 或下面的回答所述,如果您想使用 master 进行变基)git rebase master -i
(更好地遵循git rebase -i HEAD~n
以避免变基
头痛)
Now! 1) squash the repeated commits as below:
现在!1)压缩重复提交如下:
pick h
pick g
pick f
pick c~
s b~
s a~
pick e
pick d
pick c
pick b
pick a
Now, this will squash your commits in c
现在,这将压缩您在 c 中的提交
# h
# g
# f
# c~ (having changes of a~ and b~)
# e
# d
# c
# b
# a
In my case, c~ was anti-commit of c, so I just had to do the process again, but now instead of squash with s
, I'm dropping the commit with d
就我而言,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
Now, you history will remove all the duplicate commits. Now, you can compare with the origin branch you had using git diff
which had duplicate commits against your this branch. There shouldn't be any diff if you did it perfectly.
现在,您的历史记录将删除所有重复的提交。现在,您可以与您使用的原始分支进行比较,该分支git diff
对您的此分支进行了重复提交。如果你做得很完美,就不应该有任何差异。
This process might seem a little longer but you're assured that you didn't miss any commits.
这个过程可能看起来有点长,但你可以放心,你没有错过任何提交。