[Git & GitHub] 如何合并多个 commit

目录

一、前言

二、多个 commit 的现象

三、将多个 commit 进行合并的方法

四、文末总结


一、前言

众所周知,Git是一款分布式版本控制工具。由于其分布式特性,理论上我们可以在没有网络连接的情况下进行版本管理。然而,实际上这并非完全可行,因为我们仍然需要在线查阅Git的各种命令和文档。

为什么我要写这篇文章呢?原因在于许多同学经常向我咨询如何减少Pull Request中的提交记录、如何合并提交以及如何应对过多的提交记录。每次都重复相同的解释,这显然不符合程序员的风格。

二、多个 commit 的现象

那么,就先让我们来看这么一个情况,我们执行以下命令获得四个 Commit:

mkdir test
cd test

git init

echo "0" >> a
git add a
git commit -m "Commit-0"

echo "1" >> a
git add a
git commit -m "Commit-1"

echo "2" >> a
git add a
git commit -m "Commit-2"

echo "3" >> a
git add a
git commit -m "Commit-3"

我们可以看到 Git 的里面的记录是这样的:

* b1b8189 - (HEAD -> master) Commit-3
* 5756e15 - Commit-2
* e7ba81d - Commit-1
* 5d39ff2 - Commit-0

从上面的记录可以看到,在 git 中的记录,commit是有多条的,那么如何进行合并呢,接下来进行讲解。

三、将多个 commit 进行合并的方法

那么问题来了,如何把 e7ba81d(Commit-1)5756e15(Commit-2)b1b8189(Commit-3) 合并到一起,并且只保留 e7ba81d(Commit-1) 的 git message Commit-1 呢?

这个时候我们就要祭出我们这篇文章的主角—— git rebase -i 了!
这里我不想直接搬出写文档的那套,把所有的选项都介绍完,我们就把这次要用到的讲一下。

-i 实际上就是 --interactive 的简写

在使用 git rebase -i 时,我们要在后面再添加一个参数,这个参数应该是 最新的一个想保留的 commit。这句话读起来有点坳口,所以这个情况下通常需要举个例子。就我们前面提到的那个例子中,这个「最新的一个想保留的 commit」就是 5d39ff2(Commit-0),于是我们的命令看起来就长这样:

git rebase -i 5d39ff2

当然,我们也可以通过 HEAD~3 来指定该 Commit:

git rebase -i HEAD~3
  • git rebase:这是 Git 中用于重新应用提交历史的命令。

  • -i:这个选项表示以交互模式运行 rebase,允许你编辑提交历史。

  • HEAD~3:这是一个引用,表示当前分支的前三个提交(从最新的提交开始向前数)。你可以根据需要更改数字,以指定要重新应用的提交数量。

按下回车后,我们会进入到这么一个界面:

pick e7ba81d Commit-1
pick 5756e15 Commit-2
pick b1b8189 Commit-3
 
# Rebase 5d39ff2..b1b8189 onto 5d39ff2 (3 command(s))
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out

前面三行是我们需要操作的三个 Commit,每行最前面的是对该 Commit 操作的 Command。关于每个 Command 具体做什么,下面的注释写得非常清楚。为了完成我们的需求,我们可以关注到这两个命令:

s, squash = use commit, but meld into previous commit
f, fixup = like "squash", but discard this commit's log message

为了让大家看得更明白,翻译解释为:

  • squash:使用该 Commit,但会被合并到前一个 Commit 当中
  • fixup:就像 squash 那样,但会抛弃这个 Commit 的 Commit message

看样子两个命令都可以完成我们的需求,那么让我们先试一下 squash!由于我们是想把三个 Commit 都合并在一起,并且使 Commit Message 写成 Commit-1,所以我们需要把 5756e15(Commit-2) 和 b1b8189(Commit-3) 前面的 pick 都改为 squash,于是它看起来像这样:

pick e7ba81d Commit-1
squash 5756e15 Commit-2
squash b1b8189 Commit-3

当然,也可以缩写为:

pick e7ba81d Commit-1
s 5756e15 Commit-2
s b1b8189 Commit-3

完成后,使用 :wq 保存并退出。这个时候,我们进入到了下一个界面:

# This is a combination of 3 commits.
# The first commit's message is:
Commit-1

# This is the 2nd commit message:

Commit-2

# This is the 3rd commit message:

Commit-3

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Jan 5 23:27:22 2016 +0800
#
# rebase in progress; onto 5d39ff2
# You are currently editing a commit while rebasing branch 'master' on '5d39ff2'.
#
# Changes to be committed:
#   modified:   a

通过下面的注释,我们可以知道,这里其实就是一个编写 Commit Message 的界面,带 # 的行会被忽略掉,其余的行就会作为我们的新 Commit Message。为了完成我们的需求,我们修改成这样:

Commit-1

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Tue Jan 5 23:27:22 2016 +0800
#
# rebase in progress; onto 5d39ff2
# You are currently editing a commit while rebasing branch 'master' on '5d39ff2'.
#
# Changes to be committed:
#   modified:   a

使用 :wq 后,再看一下我们的 log:

* 2d7b687 - (HEAD -> master) Commit-1
* 5d39ff2 - Commit-0

可以看到我们的 log 已经合并成功了!

四、文末总结

本文旨在探讨多个 commit 的现象及如何将它们进行合并,以优化项目的提交历史。在前言中,我们提到了这个问题的重要性,以及为什么需要解决多个 commit 带来的混乱。接着,我们详细讨论了多个 commit 的各种情形和原因。

在第三部分,我们提供了多种方法来合并多个 commit,包括使用交互式变基 (git rebase -i)、合并提交 (git merge --squash) 和重写提交 (git commit --amend) 等。这些方法各有优点,可以根据项目需求和个人偏好来选择。

通过本文的学习,您将能够更好地管理项目的提交历史,使其更清晰、有序,便于团队协作和维护。合并多个 commit 并保持干净的提交历史是一个高效的开发实践,有助于提高代码质量和项目可维护性。希望您能从本文中获得有用的知识,将其应用到您的日常工作中。

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bluetata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值