Git开发时多分支防止多次提交版本线,使用cherry-pick、合并commit实现多次修改关联iusses

使用场景

在实际操作中如果直接提交到生产分支,如果有问题修改或是bug修改的话,版本中就会有多次提交,iusses管理就会很烦乱。

解决方案

示意图:

分支示意图

  1. 依据master,创建分支“hainan”,这个作为生产版本分支,新版本分支命名为“v_173”,开发版本分支“v_173_dev”,所有修改在这个分支上操作
  2. 开发时创建issues关联到“v_173”,可以对版本功能开发进行跟踪,开发的代码提交到“v_173_dev”分支,这样的话就不用担心测试时的bug等的频繁改动了
  3. 在开发提交时,只要在提交的时候commit中指明关联的iusses的id,在通过测试后就可将“v_173_dev”的a/b/c, cherry-pick到“v_173”上,然后将a/b/c在“v_173”上合并commit,关联iusses后提交即可

相关命令

cherry-pick 单个commit

命令:

$ git cherry-pick 38361a68     # 这个 38361a68 commit
  1. 如果顺利,就会正常提交。结果:
Finished one cherry-pick.
# On branch old_cc
# Your branch is ahead of 'origin/old_cc' by 3 commits.
  1. 如果在cherry-pick 的过程中出现了冲突
Automatic cherry-pick failed.  After resolving the conflicts,
mark the corrected paths with 'git add <paths>' or 'git rm <paths>'
and commit the result with: 

        git commit -c 15a2b6c61927e5aed6718de89ad9dafba939a90b

cherry-pick 批量commit

Git从1.7.2版本开始支持批量cherry-pick,就是一次可以cherry-pick一个区间的commit。

git cherry-pick <start-commit-id>..<end-commit-id>
或
git cherry-pick <start-commit-id>^..<end-commit-id>

前者表示把到之间(左开右闭,不包含start-commit-id)的提交cherry-pick到当前分支;
后者表示把到之间(闭区间,包含start-commit-id)的提交cherry-pick到当前分支。
其中,到只需要commit-id的前6位即可,并且在时间上必须早于

合并commit: git rebase

将多次commit合并,只保留一次提交历史。

1.首先使用 git log 查看一下提交历史

[demo@ubuntu1204:zh_cn(bugfix/ycs-MOS-1503-notify-template-table-center)]$ git log  
commit 5e187c7dbe84af67ad19823a54f3cc3e3f6d6940  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Date:   Thu Jul 30 20:48:15 2015 +0800  

    add center style indent  

commit 6d577eb344080d7e3593733ac8dcb622de22b492  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Rebasing (4/4)  
Date:   Thu Jul 30 20:30:20 2015 +0800  

    add center style  

commit f9b9508a3ab634f8c8a2d28ab844a3a87d8e30ab  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Date:   Thu Jul 30 20:16:35 2015 +0800  

    add center style  

commit 111ab9cc26101f7c6972de3dccfef2836a95efe0  
Author: yangcs2009 <yangchangsheng@meituan.com>  
Date:   Thu Jul 30 18:57:46 2015 +0800  

    update templates 

这样在git中看到的是4次提交,有点冗余,需要做的是将4次commit合并为一次

2.git 压缩合并 git rebase -i HEAD~4

该命令执行后,会弹出一个编辑窗口,4次提交的commit倒序排列,最上面的是最早的提交,最下面的是最近一次提交。

执行命令:

git rebase -i HEAD~4

显示:

pick 5e187c7dbe8    add center style indent  
pick 6d577eb3440    add center style  
pick f9b9508a3ab    add center style  
pick 111ab9cc261    update templates  
# Rebase 150a643..2fad1ae onto 150a643  
#  
# 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  
#  
# 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

3.修改第2-4行的第一个单词pick为squash,当然看一下里面的注释就理解含义了。

pick 5e187c7dbe8    add center style indent  
squash 6d577eb3440  add center style  
squash f9b9508a3ab  add center style  
squash 111ab9cc261  update templates  
# Rebase 150a643..2fad1ae onto 150a643  
#  
# 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  
#  
# 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  

然后保存退出,git会压缩提交历史,如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。

4.修改新的commit描述,关联iusses id
修改以保存后,会将这几个的commit描述信息合并成在一起,修改后保存、关联iussesid即可

# This is a combination of 4 commits.  
# The first commit’s message is:  
add center style indent  

# The 2nd commit’s message is:  
add center style  

# The 3rd commit’s message is:  
add center style  

# The 4th commit’s message is:  
update templates  

# Please enter the commit message for your changes. Lines starting  
# with ‘#’ will be ignored, and an empty message aborts the commit. 

关联iusses id:

  新的合并commit后的描述
  close #DGN321
# Please enter the commit message for your changes. Lines starting  
# with ‘#’ will be ignored, and an empty message aborts the commit. 

5.提交远程库

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值