Git:如何压缩中间有合并提交的提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30136558/
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
Git: How to squash commits which have merge-commit in between?
提问by Miral
I am working on a feature branch.
我正在开发一个功能分支。
- Made several commits. Squashed commits.
- Pushed changes to remote branch. Got conflicts.
- Merged changes from master, resolved conflicts on feature branch. (git fetch origin master > git merge FETCH_HEAD > resolved conflicts manually > git commit > git push)
- I made one more commit.
- 做了几次提交。压扁提交。
- 将更改推送到远程分支。有冲突。
- 合并来自 master 的更改,解决了功能分支上的冲突。(git fetch origin master > git merge FETCH_HEAD > 手动解决冲突 > git commit > git push)
- 我又做了一次提交。
So, current commit history looks like this. From current to old:
所以,当前的提交历史看起来像这样。从现在到旧:
- commit 3
- commit M yyy (Merged)
- commit 2
- 提交 3
- 提交 M yyy (合并)
- 提交 2
How do I squash above 3 commits into 1 before I merge my feature branch to master?
在将我的功能分支合并到 master 之前,如何将 3 个以上的提交压缩为 1 个?
采纳答案by Kristján
You can rebase -i
starting with commit 2
's parent (that is, the commit on master
that you branched from. You'll likely have to re-resolve conflicts when you get to the merge commit.
您可以rebase -i
从commit 2
的父级开始(即master
您从其分支的提交。当您进行合并提交时,您可能必须重新解决冲突。
So if your history looks like
所以如果你的历史看起来像
* D commit 3 (HEAD)
* M merge
/|
| * C commit 2
* | B commit on master
|/
* A (master)
Start with git rebase -i A
. You'll see a list of commits including both master
and your_branch
, but not the merge commit. pick
the first one (B
or C
, depending on timing) and squash
the rest.
开始git rebase -i A
。您将看到一个提交列表,包括master
和your_branch
,但不包括合并提交。pick
第一个(B
或C
,取决于时间)和squash
其余的。
回答by user6096790
In my case, I started working with a branch that had several commits, then a merge with the main/source branch, then more commits and I wanted to squash all commits, but kept running into an error because of the merge commit:
就我而言,我开始使用一个有多个提交的分支,然后与主/源分支合并,然后更多的提交,我想压缩所有提交,但由于合并提交而一直遇到错误:
error: commit is a merge but no -m option was given.
错误:提交是一个合并,但没有给出 -m 选项。
->C1->C2->M(merge with source branch)->C3->C4
->C1->C2->M(与源分支合并)->C3->C4
There's probably a better way (and I look forward to learning), but what I ended up doing after much reading and trial and error was creating a copy branch for reference, then reverting the current branch to C1,
可能有更好的方法(我期待学习),但经过大量阅读和反复试验后,我最终做的是创建一个复制分支以供参考,然后将当前分支恢复到 C1,
reset --hard (C1 hash)
重置 --hard (C1 哈希)
then cherry-picking C2, C3, C4, then squashing, then rebasing ... resulting in:
然后樱桃采摘C2,C3,C4,然后挤压,然后变基......导致:
M->C
M->C
(just one commit that has been rebased with source!)
(只有一个已使用源重新定位的提交!)
I hope this helps someone else with the same problem.
我希望这可以帮助其他有同样问题的人。