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

历史记录

首先我们查看一下master分支的提交历史:

$ git log
commit 415a0be986a48113829b3c60ee2387c6dbdc81d8
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + -> commit ed09a6cbe0797275ceead3f8c8d829d01f0e604b
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + 2015.01. commit 1821f6a1c1ed074fe886228cf33b3b3cb71819c4
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + 2015.01. commit cc779982fc61e82ec494d6a5654417fa7194d748
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + 2015.1.

我们看到上面有四次commit,如何将这个四次合并到一次呢?

压缩

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

git rebase -i HEAD~

该命令执行后,会弹出vim的编辑窗口,4次提交的信息会按照提交时间顺序排列,最上面的是最早一次提交,最下面的是最近一次提交。

我们需要修改第2-4行的第一个单词pick为squash,意思就是将下面的三次提交压缩到第一行中去,效果就是我们在pick所在的提交就已经做了4次动作,但是看起来就是一次而已。
注意:并不会删除下面的三次提交内容,只会将四次提交压缩为一次。

然后我们保存退出,git会一个一个压缩提交历史。

如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:

git add .
git rebase --continue

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

git rebase --abort

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

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

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

$ git rebase -i HEAD~
[detached HEAD ] helloworld
Author: xuxu <xuxu_1988@.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 files changed, insertions(+), deletions(-)
Successfully rebased and updated refs/heads/master.

现在我们来查看一下历史:

$ git log
commit 90976848524251b0a62376a9e45ea5c8aae25d87
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + helloworld commit f57da9460196b950036fe4c2c8f7e4c9131ee04e
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + 2015.01. commit 64104e057f48d6dd0e432af8b55cbccd0a09ee63
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + 2015.01. commit 8499d4fa23c4395660d21e0b7dca873a7a3ef2b8
Author: xuxu <xuxu_1988@.com>
Date: Mon Jan :: + 2015.01.

提交历史很清楚的显示出了我们的压缩成功了。

同步到远程仓库

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.. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git) Counting objects: , done.
Writing objects: % (/), bytes | bytes/s, done.
Total (delta ), reused (delta )
To https://github.com/DoctorQ/AndroidTestScrpits.git
+ 415a0be... master -> master (forced update)

OK,不出意外的话,打开gitlab应该可以看到结果了。

利用rebase来压缩多次提交的更多相关文章

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

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

  2. PHP简单利用token防止表单重复提交

    <?php /* * PHP简单利用token防止表单重复提交 * 此处理方法纯粹是为了给初学者参考 */ session_start(); function set_token() { $_S ...

  3. PHP简单利用token防止表单重复提交(转)

    <?php/* * PHP简单利用token防止表单重复提交 */function set_token() { $_SESSION['token'] = md5(microtime(true)) ...

  4. 利用session防止表单重复提交

    转自:http://www.cnblogs.com/xdp-gacl/p/3859416.html 利用Session防止表单重复提交 对于[场景二]和[场景三]导致表单重复提交的问题,既然客户端无法 ...

  5. 利用十字链表压缩稀疏矩阵(c++)-- 数据结构

    题目: 7-1 稀疏矩阵 (30 分)   如果一个矩阵中,0元素占据了矩阵的大部分,那么这个矩阵称为“稀疏矩阵”.对于稀疏矩阵,传统的二维数组存储方式,会使用大量的内存来存储0,从而浪费大量内存.为 ...

  6. 利用javascript:void(0)制作假的提交按钮替代button

    在写html页面,我们很自然的在表单提交的地方采用button来作为提交按钮,但是,用<button type=”button”>按钮</button>作为提交代码会有个问题, ...

  7. C#利用WinRAR实现压缩和解压缩

    using System; using Microsoft.Win32; using System.Diagnostics; using System.IO; namespace MSCL { /// ...

  8. 利用ICSharpCode进行压缩和解压缩

    说说我利用ICSharpCode进行压缩和解压缩的一些自己的一下实践过程 1:组件下载地址 参考文章:C#使用ICSharpCode.SharpZipLib.dll压缩文件夹和文件 2: 文件类 // ...

  9. 重新理解了重定向,利用重定向可以防止用户重复提交表单(兼谈springmvc重定向操作)

    自己用springmvc框架有一段时间了,但是都还一直分不清楚什么时候应该用转发,什么时候应该用重定向.可能用转发的情形太多了,以致于自己都忘记了还有重定向. 当用户提交post请求之后,刷新页面就会 ...

随机推荐

  1. WPF 微信 MVVM

    公司的同事离职了,接下来的日子可能会忙碌,能完善DEMO的时间也会少了,因此,把做的简易DEMO整体先记录一下,等后续不断的完善. 参考两位大神的日志:WEB版微信协议部分功能分析.[完全开源]微信客 ...

  2. python学习笔记(python介绍)

    为什么要学python? python和shell的比较,和PHP.和JAVA比较 运维开发只是用到python的很小一部分 python在一些知名公司的应用: 谷歌:python的创始人原来在谷歌工 ...

  3. 防线修建 bzoj 2300

    防线修建(1s 512MB)defense [问题描述] 近来A国和B国的矛盾激化,为了预防不测,A国准备修建一条长长的防线,当然修建防线的话,肯定要把需要保护的城市修在防线内部了.可是A国上层现在还 ...

  4. springmvc SSM 多数据源 shiro redis 后台框架 整合

    A集成代码生成器 [正反双向(单表.主表.明细表.树形表,开发利器)+快速构建表单 下载地址    ; freemaker模版技术 ,0个代码不用写,生成完整的一个模块,带页面.建表sql脚本,处理类 ...

  5. mysql删除重复记录语句的方法

    例如: id name value 1 a pp 2 a pp 3 b iii 4 b pp 5 b pp 6 c pp 7 c pp 8 c iii id是主键 要求得到这样的结果 id name ...

  6. CentOS:设置系统级代理(转)

    原文地址:http://www.cnblogs.com/cocowool/archive/2012/07/05/2578487.html YUM代理设置 编辑/etc/yum.conf,在最后加入 # ...

  7. 二叉树的创建和遍历(C版和java版)

    以这颗树为例:#表示空节点前序遍历(根->左->右)为:ABD##E##C#F## 中序遍历(左->根->右)为:#D#B#E#A#C#F# 后序遍历(左->右-> ...

  8. 机器指令翻译成 JavaScript —— No.2 跳转处理

    上一篇,我们发现大多数 6502 指令都可以直接 1:1 翻译成 JS 代码,但除了「跳转指令」. 跳转指令,分无条件跳转.条件跳转.从另一个角度,也可分: 静态跳转:目标地址已知 动态跳转:目标地址 ...

  9. 【初码干货】【Azure系列】1、再次感受Azure,体验Windows Server 2016并部署BlogEngine.NET

    上个月末,在某人的建议下,重新注册了一个1元试用账户(包含1个月期限的1500元订阅),并充值了1000元转为了正式账户,相当于1000元得到了2500的订阅,于是又一次开启了Azure之旅. 在这不 ...

  10. Web 项目杂记(一)

    1.Tomcat 多实例部署 在Tomcat下多实例部署后,发现如下问题,采用etc/init.d/tomcat start方式无法启动,而需要采用startup.sh.查找原因发现,是因为多实例部署 ...