git rebase使用详解

git Terry 251浏览 0评论

git合并分支两种方式:merge、rebase
这里主要介绍rebase操作

图解:
merge
merge
rebase
rebase

实例搞起:

#初始化测试仓库
[email protected]:~$ mkdir gitrebasetest
[email protected]:~$ cd gitrebasetest/
[email protected]:~/gitrebasetest$ git init
Initialized empty Git repository in /home/terry/gitrebasetest/.git/
#master分支添加文件
[email protected]:~/gitrebasetest$ vim readme
[email protected]:~/gitrebasetest$ git add .
[email protected]:~/gitrebasetest$ git commit  -m 'readme init'
[master (root-commit) 8d4d518] readme init
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 readme
#切换到dev分支并编辑readme文件
[email protected]:~/gitrebasetest$ git checkout  -b dev
Switched to a new branch 'dev'
[email protected]:~/gitrebasetest$ vim readme 
[email protected]:~/gitrebasetest$ git add .
[email protected]:~/gitrebasetest$ git commit -m 'dev add something'
[dev 6df4740] dev add something
 1 file changed, 1 insertion(+)
#切换到master分支编辑readme文件
[email protected]:~/gitrebasetest$ git checkout  master 
Switched to branch 'master'
[email protected]:~/gitrebasetest$ vim readme 
[email protected]:~/gitrebasetest$ git add .
[email protected]:~/gitrebasetest$ git commit  -m 'master add something'
[master c1fe7e4] master add something
 1 file changed, 1 insertion(+)
[email protected]:~/gitrebasetest$ vim Hello.java
[email protected]:~/gitrebasetest$ git add .
[email protected]:~/gitrebasetest$ git commit  -m 'master add Hello.java'
[master be18aa3] master add Hello.java
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 Hello.java
#切换到dev分支编辑
[email protected]:~/gitrebasetest$ git checkout  dev
Switched to branch 'dev'
[email protected]:~/gitrebasetest$ vim HelloDev.java
[email protected]:~/gitrebasetest$ git add .
[email protected]:~/gitrebasetest$ git commit  -m 'dev add HelloDev.java'
[dev 5f75a33] dev add HelloDev.java
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 HelloDev.java

开始rebase合并

[email protected]:~/gitrebasetest$ git rebase master 
First, rewinding head to replay your work on top of it...
Applying: dev add something
Using index info to reconstruct a base tree...
M	readme
Falling back to patching base and 3-way merge...
Auto-merging readme
CONFLICT (content): Merge conflict in readme
error: Failed to merge in the changes.
Patch failed at 0001 dev add something
Use 'git am --show-current-patch' to see the failed patch

Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit: run "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".
#查看状态
[email protected]:~/gitrebasetest$ git status
rebase in progress; onto be18aa3
You are currently rebasing branch 'dev' on 'be18aa3'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Unmerged paths:
  (use "git reset HEAD <file>..." to unstage)
  (use "git add <file>..." to mark resolution)

  both modified:   readme

no changes added to commit (use "git add" and/or "git commit -a")
#编辑readme
[email protected]:~/gitrebasetest$ vim readme
#继续rebase
[email protected]:~/gitrebasetest$ git add readme 
[email protected]:~/gitrebasetest$ git rebase --continue 
Applying: dev add something
Applying: dev add HelloDev.java
#查看分支情况
[email protected]:~/gitrebasetest$ git log
commit 2f4cd0d888864b7a8b838703a5dadbd1e3394abd (HEAD -> dev)
Author: terry <[email protected]>
Date:   Sun May 12 10:26:18 2019 +0800

    dev add HelloDev.java

commit f09b5a8cb1fd30a30aa9ff1e0233be71d4f41811
Author: terry <[email protected]>
Date:   Sun May 12 10:22:58 2019 +0800

    dev add something

commit be18aa3a371d15b6a5e24350edb619c8fecfdaf0 (master)
Author: terry <[email protected]>
Date:   Sun May 12 10:25:05 2019 +0800

    master add Hello.java

commit c1fe7e4bdc30777bdb707bb0fe480367f1540f37
Author: terry <[email protected]>
Date:   Sun May 12 10:24:06 2019 +0800

    master add something

commit 8d4d51804d56c8021299e5b539b83deaed8f5699
Author: terry <[email protected]>
Date:   Sun May 12 10:21:31 2019 +0800

    readme init
#切换到master分支merge dev
[email protected]:~/gitrebasetest$ git checkout  master 
Switched to branch 'master'
[email protected]:~/gitrebasetest$ git merge dev
Updating be18aa3..2f4cd0d
Fast-forward
 HelloDev.java | 0
 readme        | 1 +
 2 files changed, 1 insertion(+)
 create mode 100644 HelloDev.java

到此rebase操作结束

总结:

git rebase过程相比较git merge合并整合得到的结果没有任何区别,但是通过git rebase衍合能产生一个更为整洁的提交历史。
如果观察一个衍合过的分支的历史提交记录,看起来会更清楚:仿佛所有修改都是在一根线上先后完成的,尽管实际上它们原来是同时并行发生的。

一般我们使用衍合的目的,是想要得到一个能在远程分支上干净应用的补丁,比如某个项目你不是维护者,但是想帮点忙,最好使用衍合处理。
先在自己的一个分支进行开发,当准备向主项目提交补丁的时候,根据最新的orgin/master进行一次衍合操作然后再提交,这样维护者就不需要任何整合工作。

实际为:把解决分支补丁同最新主干代码之间的冲突的责任,划转给由提交补丁的人来解决。
作为维护项目的人只需要根据你提供的仓库地址做一次快进合并,或者直接采纳你提交的补丁。

衍合的风险,请务必遵循如下准则:
一旦分支中的提交对象发布到公共仓库,就千万不要对该分支进行衍合操作。

转载请注明:Terry's blog » git rebase使用详解

与本文相关的文章

  • 暂无相关文章!
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址