如何使用 git rebase -i 压缩

How to squash with git rebase -i

我对变基有点陌生,之前绝对没有压缩过提交。

虽然检出到我的本地分支 'whatever',然后我执行 git 添加和 git 提交,然后变基。

我想这是变基的正确方法,或者至少是一种方法:

git rebase -i development

development 是我们的主线分支,我们将我们的提交重新定位在其之上。当我执行该命令时,我得到:

我必须向下滚动才能看到我刚刚尝试在开发分支上重新设置基准的最新提交:

我现在不知道该怎么做。我显然已经提交了我的更改,然后做了一个变基。有压缩命令吗?我要压缩什么??我不想压缩开发分支的整个历史。 变基之前,你会做壁球吗?我有点迷路了。

您应该能够将单词 "pick" 更改为 "squash",以便将标记为 "squash" 的提交压缩到前面的提交中。

Git Documentation: Rewriting History

一个例子:

$ git log --oneline
acb05b3 Some final commit.
4968dbd Fixing an error in commit df89a81.
df89a81 Something committed too early.
c365eab Another commit...
625889f A commit...
70f29fd The beginning.

我想 rebase 到最近一次之前的 3 次提交:

$ git rebase -i HEAD~3

这会在文本编辑器中给出以下文本:

pick df89a81 Something committed too early.
pick 4968dbd Fixing an error in commit df89a81.
pick acb05b3 Some final commit.

# Rebase c365eab..acb05b3 onto c365eab (3 command(s))

我改成这个:

pick df89a81 Something committed too early.
squash 4968dbd Fixing an error in commit df89a81.
pick acb05b3 Some final commit.

# Rebase c365eab..acb05b3 onto c365eab (3 command(s))

退出后,我得到另一个包含以下内容的编辑器:

# This is a combination of 2 commits.
# The first commit's message is:

Something committed too early.

# This is the 2nd commit message:

Fixing an error in commit df89a81.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

我改成这个:

# This is a combination of 2 commits.
# The first commit's message is:

This is the squashed commit. It and everything after it get new commit hashes.

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.

现在,如果我查看我的新历史记录:

$ git log --oneline
8792fef Some final commit.
df775c4 This is the squashed commit. It and everything after it get new commit hashes.
c365eab Another commit...
625889f A commit...
70f29fd The beginning.