之前我们用git merge –squash来将分支中多次提交合并到master后,只保留一次提交历史。但是有些提交到github远程仓库中的commit信息如何合并呢?

使用下面的命令,最后一个数字4代表压缩最后四次提交。

git rebase -i HEAD~4

该命令执行后,会弹出vim的编辑窗口,4次提交的信息会倒序排列,最上面的是第四次提交,最下面的是最近一次提交。

我们需要修改第2-4行的第一个单词pick为squash,这个意义为将最后三次的提交压缩到倒数第四次的提交,效果就是我们在pick所在的提交就已经做了4次动作,但是看起来就是一次而已:

然后我们保存退出,git会一个一个压缩提交历史,如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:

git add .
git rebase --continue

如果你想放弃这次压缩的话,执行以下命令:

git rebase --abort

如果所有冲突都已经解决了,会出现如下的编辑窗口:

这个时候我们需要修改一下合并后的commit的描述信息,我们将其描述为helloworld吧:

保存退出后会看到我们完整的信息:

$ git rebase -i HEAD~4
[detached HEAD 9097684] helloworld
Author: xuxu <xuxu_1988@126.com>
Committer: unknown <hui.qian@HuiQianPC.spreadtrum.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly: git config --global user.name "Your Name"
git config --global user.email you@example.com After doing this, you may fix the identity used for this commit with: git commit --amend --reset-author 9 files changed, 25 insertions(+), 11 deletions(-)
Successfully rebased and updated refs/heads/master.

同步到远程仓库

那么这个时候我们的github上的信息又是如何呢?

github上的提交历史还是之前的,如何更新我们压缩后的历史记录呢?

我们采用git push 试试:

To https://github.com/DoctorQ/AndroidTestScrpits.git
! [rejected] master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/DoctorQ/AndroidTestScrpit
s.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

被拒绝了,因为我们合并的历史记录已经在远程仓库之前了,你无法覆盖它。那怎么办?如果你确定你的合并过程都是正确无误的,那么就可以强制push:

$ git push -f
warning: push.default is unset; its implicit value is changing in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the current behavior after the default changes, use: git config --global push.default matching To squelch this message and adopt the new behavior now, use: git config --global push.default simple When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name. In Git 2.0, Git will default to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch. See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git) Counting objects: 1, done.
Writing objects: 100% (1/1), 232 bytes | 0 bytes/s, done.
Total 1 (delta 0), reused 0 (delta 0)
To https://github.com/DoctorQ/AndroidTestScrpits.git
+ 415a0be...9097684 master -> master (forced update)
 

git中利用rebase来压缩多次提交 ----- 原文:https://blog.csdn.net/itfootball/article/details/44154121的更多相关文章

  1. js中index()的四种经典用法(转https://blog.csdn.net/superit401/article/details/51726826)

    <!doctype html><html lang="en"> <head> <meta charset="UTF-8" ...

  2. 数组中&a与&a[0]的区别 转载自http://blog.csdn.net/FX677588/article/details/74857473

    在探讨这个问题之前,我们首先来看一道笔试题,如下: [摘自牛客网]下列代码的结果是:(正确答案是 C) main() { int a[5]={1,2,3,4,5}; int *ptr=(int *)( ...

  3. 虚拟机中的CentOS7如何上网?---https://blog.csdn.net/nothing2017/article/details/61420767

    虚拟机中的CentOS7如何上网?https://blog.csdn.net/nothing2017/article/details/61420767

  4. 向txt文件中写入内容(覆盖重写与在末尾续写+FileOutputStream与FileWriter)(转发:https://blog.csdn.net/bestcxx/article/details/51381460)

    !!!! 读取txt文件中的内容 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; /** ...

  5. OpenGL学习脚印: uniform blocks在着色器中的使用 转自https://blog.csdn.net/wangdingqiaoit/article/details/52717963

    写在前面 目前,我们在着色器中要传递多个uniform变量时,总是使用多个uniform,然后在主程序中设置这些变量的值:同时如果要在多个shader之间共享变量,例如投影矩阵projection和视 ...

  6. 比较C++中的4种类型转换方式(转自http://blog.csdn.net/hrbeuwhw/article/details/7884797)

    C++的四种cast操作符的区别 Q:什么是C风格转换?什么是static_cast, dynamic_cast 以及 reinterpret_cast?区别是什么?为什么要注意? A:转换的含义是通 ...

  7. 如何简单地理解Python中的if __name__ == '__main__'(https://blog.csdn.net/yjk13703623757/article/details/77918633)

    1. 摘要 通俗的理解__name__ == '__main__':假如你叫小明.py,在朋友眼中,你是小明(__name__ == '小明'):在你自己眼中,你是你自己(__name__ == '_ ...

  8. golang中defer的详解 转自https://blog.csdn.net/skh2015java/article/details/77081250

    Go里的defer很有用,尤其在很多执行模块化操作时,初始化时给各个需要执行的模块传入参数,但是这些参数有些事在模块执行过程中才赋值的. 这时候有了defer就不会把代码写的很凌乱. Go的defer ...

  9. Android 利用cursor来进行排序(转至http://blog.csdn.net/yangzongquan/article/details/6547860)

    主要思路是:override move系列的方法,让cursor以自己想要的顺序来移动,从而达到对cursor排序的目的.比如数组A0里有 4(0),3(1),1(2),2(3),括号内为位置,排序后 ...

随机推荐

  1. android glide图片加载框架

    项目地址: https://github.com/bumptech/glide Glide作为安卓开发常用的图片加载库,有许多实用而且强大的功能,那么,今天就来总结一番,这次把比较常见的都写出来,但并 ...

  2. SQL语句检索数据排序及过滤

    阅读目录 一:排序检索数据 二:过滤数据 三:高级数据过滤 四:用通配符进行过滤 回到顶部 一:排序检索数据 1.1 排序数据 比如查询数据库中表数据的时候,我们使用如下语句: select * fr ...

  3. python之os

    os 系统级别的操作 os.getcwd() 获取当前工作目录,即当前python脚本工作的目录路径 os.chdir("dirname") 改变当前脚本工作目录:相当于shell ...

  4. 远程连接腾讯云服务器MySQL数据库

    1.添加腾讯云安全组规则的MySQL 3306端口 将所有端口打开,至少打开3306,不在赘述. 2.打开更改MySQL配置文件 打开配置文件 vi /etc/mysql/mysql.conf.d/m ...

  5. 有关swiper动态改变数据遇到的坑(不能自动滚动,自动跟新数据,切换不正常)

    以前都觉得swiper的使用很简单,那是因为使用swiper时都是写的数据,按照官网上介绍直接初始化swiper,随便丢一个地方初始化就ok了,但是在很多需求中,我们都需要动态的改变数据,这样可能就会 ...

  6. #!/usr/bin/python3的作用 解决vscode ImportError: No module named xxxx

    在 Python 脚本的第一行经常见到这样的注释: #!/usr/bin/env python3 或者 #!/usr/bin/python3 含义 在脚本中, 第一行以 #! 开头的代码, 在计算机行 ...

  7. ☆ [NOI2014] 魔法森林 「LCT动态维护最小生成树」

    题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:带权无向图,每条边有权值\(a[i],b[i]\).要求一条从\(1\)到\(N\)的路径,使得这条路径上的\(Ma ...

  8. MT【314】正切比值

    (05复旦)已知三角形$\Delta ABC$满足$\tan A:\tan B:\tan C=1:2:3$,求$\dfrac{AC}{AB}$____ 解答:设$x=tan A$,利用恒等式$\tan ...

  9. Logstash替换字符串,解析json数据,修改数据类型,获取日志时间

    在某些情况下,有些日志文本文件类json,但它的是单引号,具体格式如下,我们需要根据下列日志数据,获取正确的字段和字段类型 {'usdCnyRate': '6.728', 'futureIndex': ...

  10. Zabbix通过JMX方式监控java中间件

    Zabbix2.0添加了支持用于监控JMX应用程序的服务进程,称为“Zabbix-Java-gateway”:它是用java写的一个程序. 工作原理: zabbix_server想知道一台主机上的特定 ...