Git rebase、merge、cherry-pick用法

项目开发过程中,一般会有一个主分支,用来进行版本发布,研发会新建一个属于自己的开发分支,一个功能开发完成后会合入主分支

新建分支

D:\workspace\android>git checkout -b develop_test origin/develop_main
Switched to a new branch 'develop_test'
Branch 'develop_test' set up to track remote branch 'develop_main' from 'origin'.

rebase

开过过程中个人开发分支代码需要和公共分支代码保持一致,定期就要合并公共分支代码到个人的分支,这里就需要用到rebase,这里我们先在个人分支提交两笔

D:\workspace\android>git branch
  develop_main
* develop_test
D:\workspace\android>git log
commit 5bb45d719589708208f96660a6be1a9b6cb696ce (HEAD -> develop_test)
Author:test 
Date:   Wed Aug 2 10:13:45 2023 +0800

    个人分支第二个提交

commit b86669328c7440907ca6ddc27b2d958e56a48721
Author:test 
Date:   Wed Aug 2 10:13:27 2023 +0800

    个人分支第一个提交

然后主分支添加两笔提交

D:\workspace\android>git branch
* develop_main
  develop_test

D:\workspace\android>git log
commit 1936cd7e5d4a78301e21d9b49a8a260b5c445afb (HEAD -> develop_main)
Author: test 
Date:   Wed Aug 2 10:17:06 2023 +0800

    主分支第二个提交

commit 2d5198471e67adf8217377d142ada0d1986ce5e4
Author: test 
Date:   Wed Aug 2 10:16:35 2023 +0800

    主分支第一个提交

这个时候切到个人分支,是没有主分支的两笔提交的,执行rebase看下:

D:\workspace\android>git rebase develop_main
First, rewinding head to replay your work on top of it...
Applying: 个人分支第一个提交
Applying: 个人分支第二个提交

D:\workspace\android>git log
commit 5717ed2c4ef46830d77043d5923f8011b42f02cb (HEAD -> develop_test)
Author: test 
Date:   Wed Aug 2 10:13:45 2023 +0800

    个人分支第二个提交

commit 2d7a084fbab7ee94d4c93bf6aaba4882a62bb7c5
Author: test 
Date:   Wed Aug 2 10:13:27 2023 +0800

    个人分支第一个提交

commit 1936cd7e5d4a78301e21d9b49a8a260b5c445afb (develop_main)
Author:test 
Date:   Wed Aug 2 10:17:06 2023 +0800

    主分支第二个提交

commit 2d5198471e67adf8217377d142ada0d1986ce5e4
Author: test 
Date:   Wed Aug 2 10:16:35 2023 +0800

    主分支第一个提交

主分支的两笔提交出现在了个人分支的提交之前,可以看出rebase就相当于先后执行了

  1. git format-patch个人分支的提交生成一个ptach
  2. git reset 撤销个人分支的提交
  3. git merge 添加主分支的提交
  4. git apply 合入个人分支的patch

merge

个人分支功能开发完成后,需要合入到主分支,先切到主分支看下提交记录

D:\workspace\android>git checkout develop_main
Switched to branch 'develop_main'
Your branch is ahead of 'origin/develop_main' by 2 commits.
  (use "git push" to publish your local commits)

D:\workspace\android>git log
commit 1936cd7e5d4a78301e21d9b49a8a260b5c445afb (HEAD -> develop_main)
Author: test 
Date:   Wed Aug 2 10:17:06 2023 +0800

    主分支第二个提交

commit 2d5198471e67adf8217377d142ada0d1986ce5e4
Author: test 
Date:   Wed Aug 2 10:16:35 2023 +0800

    主分支第一个提交

现在merge个人分支的提交

D:\workspace\android>git merge develop_test
Updating 1936cd7..5717ed2
Fast-forward
 ...util/Util.kt   | 7 +++++++
 1 file changed, 7 insertions(+)

D:\workspace\android>git log
commit 5717ed2c4ef46830d77043d5923f8011b42f02cb (HEAD -> develop_main, develop_test)
Author: test 
Date:   Wed Aug 2 10:13:45 2023 +0800

    个人分支第二个提交

commit 2d7a084fbab7ee94d4c93bf6aaba4882a62bb7c5
Author: test 
Date:   Wed Aug 2 10:13:27 2023 +0800

    个人分支第一个提交

commit 1936cd7e5d4a78301e21d9b49a8a260b5c445afb
Author: test 
Date:   Wed Aug 2 10:17:06 2023 +0800

    主分支第二个提交

commit 2d5198471e67adf8217377d142ada0d1986ce5e4
Author: test 
Date:   Wed Aug 2 10:16:35 2023 +0800

    主分支第一个提交

可以看到个人分支的两笔提交已经转到了主分支

cherry-pick

merge会把分支的所有改动都合并过来,如果只想转移部分提交点,就需要使用herry-pick,先在个人分支提交一笔

D:\workspace\android>git log
commit dc17c89fe3481e2500814ef4dbbf0b8bcebf27a3 (HEAD -> develop_test)
Author: test 
Date:   Wed Aug 2 10:33:59 2023 +0800

    个人分支第三个提交

然后切到主分支把合并这个提交点

D:\workspace\android>git cherry-pick dc17c89fe3481e2500814ef4dbbf0b8bcebf27a3
[develop_main 87c1277] 个人分支第三个提交
 Date: Wed Aug 2 10:33:59 2023 +0800
 1 file changed, 3 insertions(+)

D:\workspace\android>git log
commit 87c1277a57544c53c603b04110e3dde100da8f57 (HEAD -> develop_main)
Author:test 
Date:   Wed Aug 2 10:33:59 2023 +0800

    个人分支第三个提交

你可能感兴趣的:(git,elasticsearch,大数据,rebase,merge)