合并后压扁Git提交 [英] Squashing Git commits after merging
问题描述
我有一个功能分支,其中有一些提交(请参见下文).自分支以来,一些提交也已添加到master中.
I have a feature branch that has a few commits (see below). Since branching, a few commits have been added to master too.
所以我将master
合并到feature
分支中.
So I merged master
into the feature
branch.
git checkout feature
git merge master
这将导致以下feature
分支上的日志历史记录:
This results in the below log history on the feature
branch:
* E (HEAD) merging master into feature
|\
| * D feature commit 2
| |
| * C feature commit 1
* | B master commit 2
|/
* A master commit 1
实际上,特征路径上的提交数量很大(100+),我想将它们全部压缩.
In reality the number of commits on the feature path is large (100+) and I'd like to squash them all.
是否只有在保留master
行上的提交时才压榨特征分支上的提交?
Is there a way to just squash the commits on the feature branch only while preserving those on the master
line?
推荐答案
可以.因此,您想将问题中的提交图变成这个新图:
Yes you can. So you want to turn the commit graph in your question into this new graph:
* E (HEAD) merging master into feature
|\
| * D feature commit (squashes 1 and 2)
* | B master commit 2
|/
* A master commit 1
过程:
- 确保您的工作树是干净的.没有未提交的更改,没有暂存的更改,没有未跟踪的文件.
- 确保您位于要素分支上.
-
git reset --hard *Hash of D on feature branch*
-
git reset --soft *Hash of A on master*
-
git commit
,其中包含您想要的消息. -
git merge master
- Ensure your working tree is clean. No uncommitted changes, no staged changes, no untracked files.
- Ensure you are on the feature branch.
git reset --hard *Hash of D on feature branch*
git reset --soft *Hash of A on master*
git commit
with the message you desire.git merge master
这篇关于合并后压扁Git提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!