使用git rebase压缩提交(commits)
我使用 git
有一段时间了,但老实说,我很少关注凌乱的提交历史。最近在学习 git rebase
,想分享一下如何使用这个命令来压缩整理提交(Commits)
五步完成
简而言之,总共有五个步骤。
- 运行
git rebase -i head~x
→x
是需要从头开始压缩的数目,-i
表示交互模式 - 按
i
进入vim
的INSERT
的输入模式 - 将
pick
更改为fixup
或f
- 按
ESC
退出INSERT
模式,然后输入:wq
或者:x
保存并退出 - 运行
git push -f
更新新的commit(提交)
举例说明
我将举一个例子来详细说明一下~
Sisi是个程序员,他某天首先创建了一个git repo并使用 Readme.md 对其进行了初始化。他在这个文件里随意写了一句话。他运行 git add
, git commit -m "add a readme file"
, 和 git push
将此文件推送到 GitHub。提交历史如下
ff3acf2 (HEAD -> main, origin/main, origin/HEAD) add a readme file
245b1de Initial commit
很快,他发现这个文件有问题。因此他更改了该Readme文件并再次执行了三个 git 命令。然后提交历史变成如下
6646cc6 (HEAD -> main, origin/main, origin/HEAD) make some changes in readme
ff3acf2 add a readme file
245b1de Initial commit
出于可读性或任何其他原因,Sisi认为这两个最近的提交(commit)可以合并为一个Commit。他遵循了前面的五个步骤压缩来这两个提交,具体来说,
- 运行
git rebase -i head~2
:注意,你不能在这里输入head~3
因为目前只有两个提交 - 按
i
进入vim
的 INSERT 模式 - 将第二个
pick
更改为fixup
或简写为f
,如以下代码块所示
pick ff3acf2 add a test file
f 6646cc6 make some changes in readme
# Rebase 245b1de..6646cc6 onto 245b1de (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup [-C | -c] <commit> = like "squash" but keep only the previous
# commit's log message, unless -C is used, in which case
# keep only this commit's message; -c is same as -C but
# opens the editor
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
-- INSERT --
注意在此,您不能将第一个pick
更改为f
,否则会出现如下错误
错误:没有先前的提交就无法“修复”
- 点击
ESC
然后:wq
or:x
保存并退出 - 运行
git push -f
更新新的提交
然后就大功告成了,commit历史变得非常清晰,如下
ff3acf2 (HEAD -> main, origin/main, origin/HEAD) add a readme file
245b1de Initial commit
现在最新的提交 6646cc6
实际上被压缩到提交了 ff3acf2
,并且文件是也是最新的(第二次commit修改过的)
需要特别注意的
有一件小事需要你注意。到第 5 步时,如果你利用git status
检查 ,你会得到如下消息
On branch main
Your branch and 'origin/main' have diverged,
and have 1 and 2 different commits each, respectively.(use "git pull" to merge the remote branch into yours)
nothing to commit, working tree clean
此时,你应该运行 git push -f
而不是 git pull
。因为现在你的本地分支和原始远程分支之间已经有分歧了,运行git push -f
将推送一个压缩过的提交覆盖远程分支和本地分支(–force
标志允许您“强制推送”您对远程库所做的更改)。否则,如果在这里运行 git pull
,两个提交将被拉到本地并导致合并,如下图所示
* fa8709c (HEAD -> main) Merge branch 'main' of https://github.com/lafengxiaoyu/Git-rebase-test
|\
| * 6646cc6 (origin/main, origin/HEAD) make some changes in readme
| * ff3acf2 add a test file
* | 35caab5 add a test file
|/
* 245b1de Initial commit
我很确定这不是你想要的,没错吧?
使用git rebase压缩提交(commits)相关推荐
- [Git Rebase] Git Rebase 合并提交(命令操作 SourceTree 操作)
前言 在开发过程中, 我们有的时候会有代码提交失误的情况. 在前一章, 我们介绍了git push - - force命令. 本章我们介绍git rebase命令及 SourceTree内的相关操作. ...
- git 代码没了,git rebase 合并提交记录,git stash
xx 问题 git合并提交记录 创建提交记录 合并提交记录 rebase有冲突 合并后的本地路径提交到远程 其中的参数 pick 改变提交 5.txt 和 4.txt 的顺序 删除某一个提交 rewo ...
- git rebase 与提交纪录重写
前言 千万不要在主分支和远端分支有提交纪录的时候使用git rebase命令. 前段时间有这样一个需求,在不改变大体上git提交树的基础上,修改某个git提交节点的提交内容.查询到发现git reba ...
- git rebase 修改提交信息
reword pick:保留该commit(缩写:p) reword:保留该commit,但我需要修改该commit的注释(缩写:r) edit:保留该commit, 但我要停下来修改该提交(不仅仅修 ...
- git rebase 丢弃提交
drop pick:保留该commit(缩写:p) reword:保留该commit,但我需要修改该commit的注释(缩写:r) edit:保留该commit, 但我要停下来修改该提交(不仅仅修改注 ...
- 使用git rebase和merge合并特定commits到master分支
可能大家知道如何使用cherry-pick从一个分支合并特定的commits到另一个分支,但是这个方法不能保留原始的提交信息(比如提交时间线等),而如果要保留合并过来的commits的所有提交信息,那 ...
- git rebase合并多次commi
将多次commit合并,只保留一次提交历史. 1.首先使用git log查看一下提交历史 [demo@ubuntu1204:zh_cn(bugfix/ycs-MOS-1503-notify-templ ...
- 如何在推送后压缩git中的提交?
这给出了压缩多个提交的一个很好的解释: http://git-scm.com/book/en/Git-Branching-Rebasing 但它不适用于已被推送的提交. 如何在我的本地和远程回购中压缩 ...
- 【git 整理提交】git rebase -i 命令详解
git rebase -i 详解 官方文档 变基时可用的命令 变基时有六个命令可用: pick pick只是意味着包括提交.重新进行命令时,重新安排pick命令的顺序会更改提交的顺序.如果选择不包括提 ...
最新文章
- gitlab提交合并_notification - 如何把gitlab项目中的提交、PUSH、合并请求的通知发送到邮箱...
- 王峰记忆法 解密王峰记忆法之迷
- aix和linux脚本,AIX系统资源监控SHELL脚本
- LeetCode LCS 02. 完成一半题目(计数+排序)
- Python set list dict tuple 区别和相互转换 - Python零基础入门教程
- http请求头中Referer的作用及危害
- Kafka和的安装与配置
- linux裸机串口,裸机系列-UART串口
- Java-类型转换,String转Object和Object转String
- 分布式系统事务一致性解决方案(转)
- c++中*是什么意思_int在python中什么意思
- 功能接口 java8_Java 8功能接口
- 【BZOJ4196】[Noi2015]软件包管理器 树链剖分
- 2.微信小程序(colorUI)- 构建底部操作条
- 便捷、高效、智能—从运维视角看星环科技大数据基础平台TDH
- python学生管理系统毕业设计flask_python+flask实现简单的web端学生管理系统
- c语言三个矩阵乘法,c语言矩阵相乘
- WPS 2019解决不能启用宏问题
- OpenCV(二) —— 颜色通道提取 边界填充 数值计算 图像融合
- [NOIP2017普及组] 棋盘