Git 如何压缩 commit

2023-11-10

今天同事突然问我,由于在给老大的开源项目提 pr 的时候,自己比较长时间没有 rebase 的老大的项目 master 分支了,而自己提交的 commit 又很多,有些 commit 又是实验性质的,乱七八糟的(其实就是没有用熟 git…),还有不少和老大的代码冲突了。提交代码的时候,老大要求精简一下 commit 否则其他人在看项目的演进的时候会一头雾水的。

我认为老大说的是对的,当然也存在一个问题就是同事没有用熟 git,因为在我们在版本管理的时候,其实每个 commit 都是有一定意义的,commit 不应该成为实验性质的一个动作。

回到老大的问题,「精简一下 commit」,那么好几个 commit 如何进行压缩呢?当然是有办法的。

下图是为了演示所提交的三个 commit

* commit c4bf6724a039b524edeca2489086fbed0c4b5a0d
| Author: Oscar <runzhliu@163.com>
| Date:   Mon Jun 11 11:02:33 2018 +0800
|
|     [add] third commit
|
* commit a7e9aa4654e7f7d99ba530211e056a55314a3fff
| Author: Oscar <runzhliu@163.com>
| Date:   Mon Jun 11 11:01:35 2018 +0800
|
|     [add] second commit
|
* commit 0350222da791e26d07a4e77060709700cb48608d
  Author: Oscar <runzhliu@163.com>
  Date:   Mon Jun 11 11:01:17 2018 +0800

      [add] first commit

可以利用 git rebase 来实现本文所说的压缩 commit-i 是代表 let the user edit the list of commits to rebase,这是在 git rebase -h 抄过来的,所以为什么是 -i 呢?就是因为我们需要编辑之前提交过的 commit

➜  git-compress git:(master) git rebase -i <指定的 commit>

当我们指定到第一个 commit 的时候,我们可以编辑 a7e9aa4 还有 c4bf672 两个节点。具体命令的含义,在操作的清单上已经列举没明显了,有兴趣的可以逐个命令进行测试。这里我选择 squash

pick a7e9aa4 [add] second commit
pick c4bf672 [add] third commit

# Rebase 0350222..c4bf672 onto 0350222 (2 commands)
#
# 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
# d, drop = remove commit
#
# 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

以下是编辑最后的情况,把第三个节点压缩到第二个节点,并且保留第二个节点。

pick a7e9aa4 [add] second commit
squash c4bf672 [add] third commit

# Rebase 0350222..c4bf672 onto 0350222 (2 commands)
#
# 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
# d, drop = remove commit
#
# 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 信息。

# This is a combination of 2 commits.
# This is the 1st commit message:

[add] second commit

# This is the commit message #2:

[add] third commit

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# Date:      Mon Jun 11 11:01:35 2018 +0800
#
# interactive rebase in progress; onto 0350222
# Last commands done (2 commands done):
#    pick a7e9aa4 [add] second commit
#    squash c4bf672 [add] third commit
# No commands remaining.
# You are currently editing a commit while rebasing branch 'master' on '0350222'.
#
# Changes to be committed:
#       new file:   b.txt
#       new file:   c.txt
#

最后压缩成功的提示。

➜  git-compress git:(master) git rebase -i 0350222da791e26d07a4e77060709700cb48608d
[detached HEAD 51c5f09] [add] second commit
 Date: Mon Jun 11 11:01:35 2018 +0800
 2 files changed, 1 insertion(+)
 create mode 100644 b.txt
 create mode 100644 c.txt
Successfully rebased and updated refs/heads/master.

自此,就完成了压缩多个「无意义」节点的需求了。操作简单,但是在我看来这不应该成为经常性的操作。因为在提交代码的时候,为了展示更加清晰的演进图,应该处理好自己的 commit 信息,主动去避免冗余。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Git 如何压缩 commit 的相关文章

  • Git 子模块:[电子邮件受保护]:权限被拒绝(公钥)。致命:无法从远程存储库读取

    我有一个问题git submodule update init remote 我收到错误 权限被拒绝和克隆失败 但我将 SSH 密钥添加到了我的 github 存储库中 我可以拉 推 git 克隆 我拥有所有需要的访问权限 我使用操作系统
  • 警告:您的 git 版本是 1.9.3。存在严重的安全漏洞

    我在部署到 Heroku 期间收到有关 git 1 9 3 严重安全漏洞的警告 我尝试通过 homebrew 更新 git 但发现 git 最初并不是通过 homebrew 安装的 然后我通过自制程序安装了它 brew update bre
  • 如何查看上次提交和现在之间发生了什么变化(进行一些更改后)

    与此类似question https stackoverflow com questions 1552340 how to list the file names only that changed between two commits但
  • 在 git 子模块中签出分支

    如何从子模块内更改分支 当我跑步时git branch从子模块内 我看到以下输出 gt git branch HEAD detached from 229a7b2 master 我如何将自己置于一个新的分支上 喜欢development 只
  • git 可以与 Xcode 集成吗?

    有没有办法将 git 存储库与 Xcode 内置的 SCM 功能一起使用 Xcode 4 原生支持 git WWDC 2010 上的开发者工具国情咨文演讲 在这里了解更多 Xcode 4 中的新增功能 http developer appl
  • DVCS命令的统一

    当处理多个 开源 项目时 多个版本控制系统开始出现问题 虽然它们共享共同的操作 但我经常在输入时犯错误hg add反而git add 我记得前段时间看到过一个项目 通过提供基本命令以统一的方式访问不同的源代码控制软件提交 ci add等在外
  • 忽略 git 中的本地配置文件

    Rails 应用程序中有一些本地文件 属于我们存储库的一部分 我希望 git 忽略它们 基本上 我希望 git 忽略我对 config environments 目录和 config application rb 文件中的任何内容所做的所有
  • git diff 在尖括号中显示 unicode 符号

    我有一个带有 unicode 符号 俄语文本 的文件 当我修复一些拼写错误时 我使用git diff color words 看看我所做的改变 如果是 unicode 西里尔文 符号 尖括号会造成一些混乱 如下所示 cat p1 cat p
  • 如何将更改移出主分支

    基本问题 但这一直发生在我身上 进行更改working branch 切换到master git merge working branch git push cap deploy 到舞台 泡一杯新茶 然后我回来思考其他事情并开始做出一些改变
  • 为什么我无法创建/签出该分支?

    我正在尝试创建本地 Git 分支 但它不起作用 以下是我正在使用的命令 tablet edit11 git checkout b edit 11 Switched to a new branch edit 11 tablet edit11
  • 有没有办法让 git flow 显示它在幕后执行的命令?

    有什么方法可以让 git flow 提前告诉我当我执行 flow 命令时它将执行的确切 git 命令吗 或者告诉我它是东吗 我只能看到输出和摘要吗 你可以使用Git的GIT TRACE 环境变量 http git scm com docs
  • 如何正确使用“mvn release:prepare”?

    我尝试了这个命令 用dryrun在我的 Maven 项目上进行测试 mvn release clean release prepare DdryRun true DgenerateBackupPoms false Dtag solocal
  • 格里特:! [远程拒绝] HEAD -> refs/publish/master (没有新的更改)

    我做了一些更改 提交了它们并将分支推送到 Gerrit git push gerrit 现在我的更改没有出现在 Gerrit 中 我认为这是因为我手动推送更改而不是使用git 审查 https github com openstack in
  • 致命:.git/info/refs 无效:这是一个 git 存储库吗?

    我有一个托管在 Assembla 上的 Git 存储库 我正在尝试执行以下操作 git push u origin master 我一遍又一遍地收到以下错误 fatal https url repo name git info refs n
  • 如何合并两个连续的 git 存储库

    我有一个相当独特的情况 我有一个名为 Project1 的存储库 我在其中工作了一些时间 几个月 一年后 我创建了存储库 Project1 Again 从 Project1 停止的地方开始 现在 我希望修订历史记录是连续的 因此我希望它们合
  • 默认情况下 git merge -Xignore-space-change

    我该如何设置该选项ignore space change对于所有合并使用git config 我也许可以使用别名merge 但因为我希望该设置应用于git stash pop git stash apply git pull and git
  • 使用终端时 Git 推送在总计后卡住了?

    我尝试将一些文件推送到Github 总大小只有22 2M 我不知道为什么它在总行之后卡住了 我读过推送到 Github 时 Git 推送挂起 https stackoverflow com questions 16906161 git pu
  • 如何克隆特定的 Git 标签

    From git clone 1 手册页 http git scm com docs git clone branch还可以在结果存储库中的该提交处获取标签并分离 HEAD 我试过 git clone branch
  • 如何使用 AWS Lambda 安装 Git?

    我在代码提交存储库中有代码 我正在编写一个 lambda 函数来为代码提交存储库的每个签入 事件 构建代码 我无法安装 git 因此无法克隆存储库 我该怎么办呢 正如其他人提到的 在 lambda 上安装 git 要么非常困难 要么完全不可
  • 当当前分支上有未提交的更改时签出另一个分支

    大多数时候 当我尝试签出另一个现有分支时 如果我在当前分支上有一些未提交的更改 Git 会不允许我这样做 所以我必须首先提交或隐藏这些更改 然而 有时 Git 确实允许我在不提交或存储这些更改的情况下签出另一个分支 并且它会将这些更改携带到

随机推荐