Git 如何压缩 commit
今天同事突然问我,由于在给老大的开源项目提 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 0350222da791e26d07a4e77060709700cb48608dAuthor: 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 commitDate: Mon Jun 11 11:01:35 2018 +08002 files changed, 1 insertion(+)create mode 100644 b.txtcreate mode 100644 c.txt
Successfully rebased and updated refs/heads/master.
自此,就完成了压缩多个「无意义」节点的需求了。操作简单,但是在我看来这不应该成为经常性的操作。因为在提交代码的时候,为了展示更加清晰的演进图,应该处理好自己的 commit
信息,主动去避免冗余。
Git 如何压缩 commit相关推荐
- Git笔记(一)——[commit, checkout]
其实一直觉得自己是会用Git的,毕竟咱也是用Github的人啊!可是三月份找工作时候的一次面试颠覆了我的看法: Q: 用过Git吗?平常怎么用的? A: 用过的,一般就是add,commit,push ...
- Git add回退 commit回退
Git add回退 & commit回退 Git 有三种状态,你的文件可能 处于其中之一: 已提交(committed).已修改(modified) 和 已暂存(staged). • 已修改表 ...
- 【Git】回退 commit 版本详解
文章目录 当我们提交的版本出bug后,如果不影响线上的功能,可能先回退到之前的版本,这时候就要用到git reset这个命令,及他各参数的用法:--hard.--mixed.--soft 假设我们还原 ...
- git 修改远端 commit 信息
git 修改远端 commit 信息 git rebase -i HEAD~x( x 代表最近几条commit ),执行之后将出现以下界面 上面的 pick 后面即远端的 commit 信息,最下面的 ...
- Git快速入门-git stash 暂存变更,git reset 撤销commit,git revert 回退远程版本库
Git快速入门系列文章 - Git快速入门-安装配置篇 - Git快速入门-常用命令之独奏篇 - Git快速入门-常用命令之交响乐篇 - Git快速入门-git stash 暂存变更,git rese ...
- git 撤销上次commit提交
git撤销上次commit提交(还在本地) 1.git reset --soft head~1 (撤销上一次commit, 不会撤销git add) 2.git reset head~ (撤销上一次c ...
- Git 提交模板 Commit Template
说明: 在公司工作或者管理学习代码,开发的代码时使用Git是一个十分方便的选择,然而,有些时候会遇到commit以后要规范化备注,以便以后出问题了回溯版本就可以看得到每次commit的详情. 每次手动 ...
- git删除指定commit
git删除指定commit 1.使用git log 命令,查看已提交的记录.例如红色圈出的commit是本次要删除的commit.(ps:使用git log查看日志,退出的时候直接按q就退出来了) 2 ...
- git 如何压缩多个commits 成一个commit
在git进行项目版本管理时,经常会遇到如下的场景, 开发者针对feature/bugfix/ hotfix/refactoring进行开发时,在本地repo中进行了很多次commits,然而当当前开发 ...
最新文章
- C#隐藏手机号中间四位为*
- 二进制安装部署 4 kubernetes集群---超详细教程
- ue4 无限地图_UE4大地图(流关卡、无缝地图)
- 基于CefSharp构建基于Chromium的应用程序
- 连接mysql报错有乱码_连接mysql服务器报错时,出现乱码
- 强大的漏洞扫描工具--nessus
- 安装 深度wine_深度系统更新(2020.11.25)
- 赛事招募 | 互联网创客马拉松开show啦~比武招APP
- 班尼机器人怎么语音_每日一句中话西说巧学英语:“我们今天就到这儿吧。”英语怎么说?...
- redis scan 效率太慢_阿里云的redis规范
- 狂神Reids学习笔记二
- GPS之家论坛最新地图下载精选 汇集论坛精华 不断更新中(2013.3.30)
- IOS企业应用出现无法验证,需要网络连接以在这台iPad上验证。接入互联网并重试
- 青春期发育阶段的测量
- 经验分享 | STM32CubeMX + STM32F1系列开发时遇到的四个问题及解决方案分享
- 【Day 3】机器阅读理解——常见机器阅读理解模型(下)
- SYN_RECV状态相关
- 宝塔低内存安装mysql_宝塔面板安装mysql 8.0提示最低内存和最低CPU限制的三种解决方法...
- 三相异步电机------坐标变换(二)
- 小型计算机房建设标准,依据国家计算机房建设标准.doc
热门文章
- java从键盘录入三位数,打印其个位十位百位
- 计算机怎么测试精度,通过实际测量比较来比较手持GPS定位精度
- Node.js(黑马)笔记01--day1--最简单的node执行脚本,readFile,创建服务器,核心模块,IP地址和端口号,编码格式g、get,post,路由,静态资源
- 又一巨头撤离中国,辉煌时拿下20%中国市场,没落后甩开中国制造
- 网关路由器的冗余设计与配置
- 计算机研究生学校分析报告,2015-2018年全国硕士研究生报考数据分析报告|硕士研究生|数据分析_新浪教育_新浪网...
- (一) DirectShow简单采集程序——使用CaptureGraphBuilder
- 非常不错的一个JS Bloger
- 服务器雪崩效应及解决方案
- mysql问题#1146 - Table 'xxx.xxxxx' doesn't exist