git 进阶 (四)变基提交

编程入门 行业动态 更新时间:2024-10-18 14:17:16

git  <a href=https://www.elefans.com/category/jswz/34/1769503.html style=进阶 (四)变基提交"/>

git 进阶 (四)变基提交

1.1 变基提交 

1.1.1 git rebase 命令是用来改变一串提交是以什么为基础的。

比如,有两个分支正在开发中,最初,topic分支是从master分支的提交B处开始的,在此期间,master分支已经进展到提交E。

可以改写提交让它们基于提交E而不是B,这样就相当于在master分支最新的基础上分出的分支进行开发。

git checkout topicgit rebase master 

 

1.1.2 git rebase --onto  可以将一条分支上的开发线整个移植到完全不同的分支上。

比如已经在feature上开发了一个新功能,在提交P和Q中,是基于maint分支的。要把feature分支上的提交P和Q从maint分支整体迁移到master分支:

git rebase --onto master maint^ feature

变基操作一次只能迁移一个提交,从各自原始提交位置迁移到新的提交基础。因此,每个移动的提交都可能有冲突需要解决。

 

举例:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git branch 
* AAAmastermaster_dragontoney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n3
commit d54bfd23d30d1ff6aea5fc6197f13a579cf33521
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:41 2019 +0800bcommit a7899fd5a67302d5ceb02914f26955f6e3587ba2
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:27 2019 +0800acommit 11316b05d9eb14d0be27cb5efb8e0ef3120fce95
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Tue Jul 18 14:06:06 2017 +0800[Dragon-SDK-Shell]optimise sdk diag shelltoney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git checkout master_dragon
Switched to branch 'master_dragon'
Your branch is up-to-date with 'origin/master_dragon'.toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n5
commit 42947bf9888c01669ed3541ddb7d585502f38bc8
Author: zhigang <zhigangfeng@nettech-global.com>
Date:   Fri Jul 28 11:03:24 2017 +0800[DBS]This is the local pagecommit 3162d0391dbabff2ba7effe252f6c8b1aabed541
Author: yangjun <yangxiaojun@nettech-global.com>
Date:   Mon Jul 24 15:44:07 2017 +0800[Dragon-Function-Implement]:Single fw support for 52MP(Rtk839X)commit 846c0af7a77559d989dbc60be88facecc1ddfc3c
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Thu Jul 27 14:05:21 2017 +0800Dragon-fix: FW upgrade will cause crashcommit d8a0e2cd3c9cd3e32588cdccc62ebc002cf79462
Author: yangjun <yangxiaojun@nettech-global.com>
Date:   Thu Jul 20 18:33:37 2017 +0800[Dragon-Fuction-Implement]:Support Single fw for 28P/10MPcommit 11316b05d9eb14d0be27cb5efb8e0ef3120fce95
Author: chenhaodai <chenhaodai@nettech-global.com>
Date:   Tue Jul 18 14:06:06 2017 +0800[Dragon-SDK-Shell]optimise sdk diag shelltoney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git checkout AAA 
Switched to branch 'AAA'toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git rebase master_dragon
First, rewinding head to replay your work on top of it...
Applying: a
Applying: btoney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n3
commit 94f9dc2dc625735f3e9cbcfa3794152bc87c7986
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:41 2019 +0800bcommit 295671c0b394db080fb8d8ef1fe5ada3b2771806
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:27 2019 +0800acommit 42947bf9888c01669ed3541ddb7d585502f38bc8
Author: zhigang <zhigangfeng@nettech-global.com>
Date:   Fri Jul 28 11:03:24 2017 +0800[DBS]This is the local page

 

1.1.3 git rebase -i 

使用git rebase -i 可以对提交进行重新排序,编辑,删除,把多个提交合并成一个,把一个提交分离成多个。

举例:

  • 调整提交的顺序:
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n3
commit 436d7cf4a0d762bfc5fbb42daedcda867cf4cde9
Author: chk <chk@163.com>
Date:   Mon Aug 19 21:23:37 2019 +0800ccommit 94f9dc2dc625735f3e9cbcfa3794152bc87c7986
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:41 2019 +0800bcommit 295671c0b394db080fb8d8ef1fe5ada3b2771806
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:27 2019 +0800

对b和c的提交顺序进行修改:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git rebase -i HEAD~3GNU nano 2.2.6   File: /home/toney/work/project/dragon/core/code/customer/cus_dlinkg2/.git/rebase-merge/git-rebase-todo            pick 295671c a
pick 436d7cf c
pick 94f9dc2 b# Rebase 42947bf..436d7cf onto 42947bf
#
# 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

查看效果:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n3
commit fc3a4259add2cd796744ba74e7de1feecd3956b1
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:41 2019 +0800bcommit 637f3f836d7bb7c7be9e087f70c30edae2f672b1
Author: chk <chk@163.com>
Date:   Mon Aug 19 21:23:37 2019 +0800ccommit 295671c0b394db080fb8d8ef1fe5ada3b2771806
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:27 2019 +0800a

 

  • 合并提交:
toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git rebase -i HEAD~3GNU nano 2.2.6   File: /home/toney/work/project/dragon/core/code/customer/cus_dlinkg2/.git/rebase-merge/git-rebase-todo            pick 295671c a
squash 637f3f8 c
pick fc3a425 b# Rebase 42947bf..fc3a425 onto 42947bf
#
# 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

查看效果:

toney@sw2:~/work/project/dragon/core/code/customer/cus_dlinkg2$ git log -n3
commit e10192dd278c0eada10636df5264ec42a39b37e9
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:41 2019 +0800bcommit aec5216b3f7096e5b3425c850ca324ac4444da78
Author: chk <chk@163.com>
Date:   Sun Aug 18 21:21:27 2019 +0800accommit 42947bf9888c01669ed3541ddb7d585502f38bc8
Author: zhigang <zhigangfeng@nettech-global.com>
Date:   Fri Jul 28 11:03:24 2017 +0800[DBS]This is the local page

 

更多推荐

git 进阶 (四)变基提交

本文发布于:2023-06-20 02:48:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/794944.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:进阶   git

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!