Gitlab 简明教程

GitLab - Squashing Commits

Description

合并请求时,压扁是一种将所有提交合并到一个提交中的方式。

Squashing is a way of combining all commits into one when you are obtaining a merge request.

Steps for Squashing Commits

Step 1 − 转到您的项目目录并使用 git checkout 命令签出带有 squash-chapter 名称的新分支 −

Step 1 − Go to your project directory and check out a new branch with the name squash-chapter by using the git checkout command −

squashing commit 1

标志 -b 表示新分支名称。

The flag -b indicates new branch name.

Step 2 − 现在,使用两个提交创建一个新文件,将该文件添加到工作目录并将其更改存储到存储库以及提交消息中,如下所示 −

Step 2 − Now, create a new file with two commits, add that file to working directory and store the changes to the repository along with the commit messages as shown below −

squashing commit
squashing commit 2

Step 3 − 现在,使用以下命令将上述两个提交压扁为一个提交 −

Step 3 − Now, squash the above two commits into one commit by using the below command −

$ git rebase -i HEAD~2

此处,使用 git rebase 命令来集成一个分支到另一个分支的更改,HEAD~2 为最后两个压扁提交,如果您想要压扁四个提交,那么您需要写成 HEAD~4。还有另一个重点在于,您至少需要两个提交来完成压扁操作。

Here, git rebase command is used to integrate changes from one branch to another and HEAD~2 specifies last two squashed commits and if you want to squash four commits, then you need to write as HEAD~4. One more important point is, you need atleast two commits to complete the squash operation.

Step 4 − 在输入上述命令后,它将打开下面的编辑器,您需要在第二行将 pick 单词更改为 squash 单词(您需要压扁此提交)。

Step 4 − After entering the above command, it will open the below editor in which you have to change the pick word to squash word in the second line (you need to squash this commit).

squashing commit 3

现在按 Esc 键,然后输入冒号 (:) 并键入 wq 来保存和退出屏幕。

Now press the Esc key, then colon(:) and type wq to save and exit from the screen.

Step 5 − 现在将分支推送到远程存储库,如下所示 −

Step 5 − Now push the branch to remote repository as shown below −

squashing commit 4