0

I want to know how to squash some commits that are upstream of a merge. Below is an attempt at doing this, which resulted in the newly squashed branch becoming independent, while leaving the original commits un-merged.

$ git checkout dev-track_rpm
Switched to branch 'dev-track_rpm'
$ git lg
**removed a bunch of log for brevity**
| * 6802588 - (2 months ago)
* | 267b937 - (1 month ago)
|/
*   45f7846 - (2 months ago) Merge branch 'dev-track_rpm'
|\
| * 7e07b43 - (2 months ago) (HEAD, dev-track_rpm)
| * 5cce85b - (2 months ago)
| * e36f2fb - (2 months ago)
| * 081175b - (2 months ago)
* | 4896e1f - (2 months ago)
|/
* 4767c9d - (2 months ago)


$ git rebase -i -p 4767c9d **also tried without -p initially**
*choose to squash 5cce85b and e36f2fb**
[detached HEAD c77cf3e]
 20 files changed, 4139 insertions(+), 0 deletions(-)
**removed file list for brevity**
Successfully rebased and updated refs/heads/dev-track_rpm.
$ git lg
* 260cdce - (2 months ago) (HEAD, dev-track_rpm)
* c77cf3e - (2 months ago)
**removed same bunch of log for brevity**
| | * 6802588 - (2 months ago)
| * | 267b937 - (1 month ago)
| |/
| *   45f7846 - (2 months ago) Merge branch 'dev-track_rpm'
| |\
| | * 7e07b43 - (2 months ago)
| | * 5cce85b - (2 months ago)
| | * e36f2fb - (2 months ago)
| | * 081175b - (2 months ago)
| |/
|/|
| * 4896e1f - (2 months ago)
|/
* 4767c9d - (2 months ago)
$

I wanted to get this:

$ git lg
**removed a bunch of log for brevity**
| * 6802588 - (2 months ago)
* | 267b937 - (1 month ago)
|/
*   45f7846 - (2 months ago) Merge branch 'dev-track_rpm'
|\
| * <some new sha> - (2 months ago) same changes as 7e07b43 (HEAD, dev-track_rpm)
| * <some new sha> - (2 months ago) squashed 
* | 4896e1f - (2 months ago)
|/
* 4767c9d - (2 months ago)
4

1 回答 1

0

当您进行变基时,您会更改树的哈希顺序,因此无论您做什么,它都会显示为不同的树,唯一的解决方案是强制推送新树以替换旧树,尽管通常不鼓励,特别是当这是一个有多个提交者的共享项目时。

编辑:这是您想要做的简单解释:

假设这棵当前树(假设这些字母是提交哈希)

A -> B -> C -> D -> E

你想挤压BCD

所以你运行这个命令

git rebase -i B~

注意B~也一样A,你想要最后一次提交的父级要编辑,我通常使用~格式。

你会得到你的编辑器与这些行

pick B the b message
pick C the c messasge
pick D the d message
pick E the e message

你会把镐换成南瓜

pick B the b message
squash C the c messasge
squash D the d message
pick E the e message

rebase 将开始选择提交,然后它会再次停止并显示您正在压缩的两个提交的日志,并要求您选择一个或编辑两者并编写新消息,只需保存,树将继续重新建立基础。

现在你会得到一棵看起来像这样的树

A -> BCD' -> E'

请注意,即使您只编辑了B,CDcommits ,E也发生了变化,因为它下面的树发生了变化,如果您绘制整个树,它看起来像这样

A -> BCD' -> E' <- HEAD and BRANCH will be here
 \-> B -> C -> D -> E ( this part will be detached )

然后,您将强制将 HEAD 或 BRANCH 推到原点-f

git push origin BRANCH -f
于 2013-11-13T12:57:10.053 回答