设为首页 收藏本站
查看: 2400|回复: 0

[经验分享] git合并历史提交

[复制链接]

尚未签到

发表于 2018-1-12 23:48:00 | 显示全部楼层 |阅读模式
背景
  以前一直觉得只要pull和push就够了,但合作中总会遇到各种非理想的情况。这时候才发现git其他命令的作用。
  现在的情况是,repo是一个远程team维护的,我们需要增加新feature,那么就是一个feature分支了。由于开发中各种修改,本feature分支多次commit。最后,交给远程team review的时候,人家看着乱七八糟的修改历史很蛋疼:很难看懂各种增量修改。其实,对人家来说,我们的改动应该就是增加或者删除。给他们看开发过程的增量反而太乱。于是,人家要求我们将feature分支的提交合并,这样看起来清爽。

一些简单的命令准备
  合并分支的命令是rebase,除此之外,其他的一些命令也应该知晓。
  查看commit历史
  

git log  

  查看当前状态
  

git status  

  添加所有文件
  

git add .  

  提交修改
  

git commit -m "本次提交添加了xxxx"  

vim的简单指令:
  参阅vim的简单使用

准备一个测试repo
  

git init test-rebase  
cd test-rebase
  

  提交一个文件多次:
  

vim test.txt  
//输入第一次提交。
  

  
git add test.txt
  
git commit -m "1"
  

  
vim test.txt
  
//输入第2次提交。
  

  
git add test.txt
  
git commit -m "2"
  

  
vim test.txt
  
//输入第3次提交。
  

  
git add test.txt
  
git commit -m "3"
  

  查看log:
  

git log  

  
//////
  
commit 0353373749d72e53a34c7bdda86d77d7bb3ca6fe
  
Author: ryan <v-rmiao@expedia.com>
  
Date:   Wed Jul 19 13:23:18 2017 +0800
  

  3
  

  
commit acf6d24adc2097fda82d29064e8edfef6355d01d
  
Author: ryan <v-rmiao@expedia.com>
  
Date:   Wed Jul 19 13:20:37 2017 +0800
  

  2
  

  
commit 2169bc5e20386951b19aff32143e74f2da683df2
  
Author: ryan <v-rmiao@expedia.com>
  
Date:   Wed Jul 19 13:19:42 2017 +0800
  

  1
  

  可以看到有三次提交了。现在我们想要把第2次和第3次提交的内容合并成一次提交。

开始rebase

1. 复制合并前的一次提交的hash
  这里就是第一次提交的hash。即2169bc5e2

2. git rebase -i xxx
  

git rebase -i 2169bc5e2  

  进入历史提交的编辑页面,此时编辑方式为vim。
  

pick acf6d24 2  
pick 0353373 3
  

  
# Rebase 2169bc5..0353373 onto 2169bc5 (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 &quot;squash&quot;, 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
  

  可以看到第2次和第3次的提交消息,并且是从old->new来排序的。我们需要把第3次提交合并到第2次上。使用squash.

squash
  修改第三次提交为squash,意思是和前一次(第二次)提交合并。
  
键盘按键j移动到第二行,然后按a开始编辑,删除pick,插入squash
  
如下:
  

pick acf6d24 2  
squash  0353373 3
  

  
# Rebase 2169bc5..0353373 onto 2169bc5 (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 &quot;squash&quot;, 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
  

  然后,按esc退出编辑,再按:,输入wq保存。
  
这时候会进入第二个vim页面,这里让我们再次修改commit message的。就是合并后的message。
  

# This is a combination of 2 commits.  
这是合并后的message,以下是之前合并的历史
  
# This is the 1st commit message:
  

  
2
  

  
# This is the commit message #2:
  

  
3
  

  
# Please enter the commit message for your changes. Lines starting
  
# with '#' will be ignored, and an empty message aborts the commit.
  
#
  
# Date:      Wed Jul 19 13:20:37 2017 +0800
  
#
  
# interactive rebase in progress; onto 2169bc5
  
# Last commands done (2 commands done):
  
#    pick acf6d24 2
  
#    squash 0353373 3
  
# No commands remaining.
  
# You are currently editing a commit while rebasing branch 'master' on '2169bc5'.
  
#
  
# Changes to be committed:
  

  

  还是和刚才一样,按o插入下一行,输入这次合并的message。然后按esc,按:, 输入wq保存并退出。

完事,再次查看log
  

git log  

  内容如下:
  

commit 8f54e6b5643ff26ac967a9e6e6cded68a6c50906  
Author: ryan <v-rmiao@expedia.com>
  
Date:   Wed Jul 19 13:20:37 2017 +0800
  

  这是合并后的message,以下是之前合并的历史
  

  2
  

  3
  

  
commit 2169bc5e20386951b19aff32143e74f2da683df2
  
Author: ryan <v-rmiao@expedia.com>
  
Date:   Wed Jul 19 13:19:42 2017 +0800
  

  1
  



参考
  https://git-scm.com/book/zh/v2/Git-%E5%B7%A5%E5%85%B7-%E9%87%8D%E5%86%99%E5%8E%86%E5%8F%B2

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-434493-1-1.html 上篇帖子: git操作方便,简单使用的客户端sourcetree 安装配置所遇问题总结 下篇帖子: 一个可以提高开发效率的Git命令
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表