如何使用 git rebase -i 进行压缩

Pos*_*Guy 5 git rebase

我对变基有点陌生,而且之前肯定没有压缩过提交。

当签出到我的本地分支“whatever”时,我会执行 git add 和 git commit,然后进行 rebase。

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

git rebase -i development
Run Code Online (Sandbox Code Playgroud)

development 是我们的主线分支,我们在此基础上重新调整我们的提交。当我执行该命令时,我得到: 在此输入图像描述

我必须向下滚动才能看到我刚刚尝试在开发分支顶部重新建立基础的最新提交:在此输入图像描述

我不知道此时该怎么办。我显然已经提交了更改,然后进行了变基。有挤压命令吗?我要挤压什么?我不想破坏开发分支的整个历史。在rebase之前你会做squash吗?我有点迷失了。

Dav*_*ett 6

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

Git 文档:重写历史

一个例子:

$ 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.
Run Code Online (Sandbox Code Playgroud)

我想在最近一次提交之前重新调整 3 次提交:

$ git rebase -i HEAD~3
Run Code Online (Sandbox Code Playgroud)

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

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))
Run Code Online (Sandbox Code Playgroud)

我将其更改为:

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))
Run Code Online (Sandbox Code Playgroud)

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

# 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.
Run Code Online (Sandbox Code Playgroud)

我将其更改为:

# 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.
Run Code Online (Sandbox Code Playgroud)

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

$ 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.
Run Code Online (Sandbox Code Playgroud)