• git rebase


    我们新建三个分支分别是master、dev、test,之后在dev分支的test.txt文件新建2个提交,在test分支的test.txt文件新建2个提交。 
    切换到test分支,然后执行git rebase dev 我们要将dev分支的提交应用到test分支: 
    这里写图片描述
    然后在test分支执行git rebase dev

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     2 $ git rebase dev
     3 First, rewinding head to replay your work on top of it...
     4 Applying: [test branch] add -test1- in text.txt //将test的第一次提交应用到dev的最后一个节点
     5 error: Failed to merge in the changes.
     6 Using index info to reconstruct a base tree...
     7 M       test.txt
     8 Falling back to patching base and 3-way merge...
     9 Auto-merging test.txt
    10 CONFLICT (content): Merge conflict in test.txt
    11 Patch failed at 0001 [test branch] add -test1- in text.txt
    12 The copy of the patch that failed is found in: .git/rebase-apply/patch
    13 
    14 When you have resolved this problem, run "git rebase --continue".
    15 If you prefer to skip this patch, run "git rebase --skip" instead.
    16 To check out the original branch and stop rebasing, run "git rebase --abort".
    17 
    18 
    19 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
    20 $ cat test.txt
    21 master1
    22 <<<<<<< HEAD
    23 dev1
    24 dev2
    25 =======
    26 test1
    27 >>>>>>> [test branch] add -test1- in text.txt
    28 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
    29 $ git status
    30 rebase in progress; onto f28df0d
    31 You are currently rebasing branch 'test' on 'f28df0d'.//对dev 的'f28df0d'应用补丁。
    32   (fix conflicts and then run "git rebase --continue")
    33   (use "git rebase --skip" to skip this patch)//使用dev的补丁,废弃test的修改。
    34   (use "git rebase --abort" to check out the original branch)//终止rebase过程
    35 
    36 Unmerged paths:
    37   (use "git reset HEAD <file>..." to unstage)
    38   (use "git add <file>..." to mark resolution)
    39 
    40         both modified:   test.txt
    41 
    42 no changes added to commit (use "git add" and/or "git commit -a")

    git rebase –abort的意思是终止当前rebase的操作,回到原始状态。

    1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
    2 $ git rebase --abort
    3 
    4 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    5 $ cat test.txt
    6 master1
    7 test1
    8 test2

    刚才演示了abort的效果,我们重新rebase回到rebase状态:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     2 $ git rebase dev
     3 First, rewinding head to replay your work on top of it...
     4 Applying: [test branch] add  a line  in test.txt
     5 error: Failed to merge in the changes.
     6 Using index info to reconstruct a base tree...
     7 M       test.txt
     8 Falling back to patching base and 3-way merge...
     9 Auto-merging test.txt
    10 CONFLICT (content): Merge conflict in test.txt
    11 Patch failed at 0001 [test branch] add  a line  in test.txt
    12 The copy of the patch that failed is found in: .git/rebase-apply/patch
    13 
    14 When you have resolved this problem, run "git rebase --continue".
    15 If you prefer to skip this patch, run "git rebase --skip" instead.
    16 To check out the original branch and stop rebasing, run "git rebase --abort".
    17 
    18 
    19 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
    20 $ cat test.txt
    21 master1
    22 <<<<<<< HEAD
    23 dev1
    24 dev2
    25 =======
    26 test1
    27 >>>>>>> [test branch] add  a line  in test.txt

    test.txt文件里边的冲突部分是dev分支的“dev1 dev2”2行和test分支的“test1”, 即以dev的2次提交为基准,那么如果执行git rebase –skip所做的操作是: 
    丢弃test的第一次提交的修改,保留dev的修改:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
     2 $ git rebase --skip
     3 Applying: [test branch] add -test2- in text.txt //应用test的第二次提交。
     4 error: Failed to merge in the changes.
     5 Using index info to reconstruct a base tree...
     6 M       test.txt
     7 Falling back to patching base and 3-way merge...
     8 Auto-merging test.txt
     9 CONFLICT (content): Merge conflict in test.txt
    10 Patch failed at 0002 [test branch] add -test2- in text.txt
    11 The copy of the patch that failed is found in: .git/rebase-apply/patch
    12 
    13 When you have resolved this problem, run "git rebase --continue".
    14 If you prefer to skip this patch, run "git rebase --skip" instead.
    15 To check out the original branch and stop rebasing, run "git rebase --abort".
    16 
    17 
    18 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 2/2)
    19 $ cat test.txt
    20 master1
    21 <<<<<<< HEAD
    22 dev1
    23 dev2
    24 =======
    25 test1
    26 test2 //test2出现。
    27 >>>>>>> [test branch] add -test2- in text.txt

    此时如果我们再次执行git rebase –skip 就会将test的第二次提交丢弃,保留dev的修改:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 2/2)
     2 $ git rebase --skip
     3 
     4 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     5 $ cat test.txt
     6 master1
     7 dev1
     8 dev2
     9 
    10 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    11 $

    可以看到保留的2次的dev出现在文件里边,即“dev1 “和”dev2”这2次提交。 
    同时我们看一下git log:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     2 $ git log
     3 commit f28df0d2c6fcb11d34e7551d5bb9b337aa46b259 (HEAD -> test, dev)
     4 Author: ceaser <ceaserwang@outlook.com>
     5 Date:   Sat Aug 5 20:38:36 2017 +0800
     6 
     7     [dev  branch] add -dev2- in test.txt
     8 
     9 commit 9ad27e0c99795978f259af4d2e17b99b77448f91
    10 Author: ceaser <ceaserwang@outlook.com>
    11 Date:   Sat Aug 5 20:38:22 2017 +0800
    12 
    13     [dev  branch] add -dev1- in test.txt
    14 
    15 commit 72d9d551a336c1db8976a8d1f595f4d9dbd5569b (master)
    16 Author: ceaser <ceaserwang@outlook.com>
    17 Date:   Sat Aug 5 19:55:40 2017 +0800
    18 
    19     master branch add file test.txt with master1 content
    20 
    21 commit 62046ccba0fea0b8e922697312632972edc8f0f8
    22 Author: ceaser <ceaserwang@outlook.com>
    23 Date:   Sat Aug 5 19:48:49 2017 +0800
    24 
    25     master branch add file test.txt with line master1 commtent

    里边有dev的2次提交的commit id验证了我们所说的。 
    此时我们切换到dev分支,演示出现冲突的一种情况,上边我们演示的是丢弃test的提交,那么我们接下来演示不丢弃test的提交,切换到dev的时候我们 git merge test,进行的是fast forward:

    1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    2 $  git checkout dev
    3 Switched to branch 'dev'
    4 
    5 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    6 $ git merge test
    7 Already up-to-date.

    我们增加dev3和dev4:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
     2 $ cat test.txt
     3 master1
     4 dev1
     5 dev2
     6 
     7 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
     8 $ echo 'dev3' >> test.txt
     9 
    10 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    11 $ git add test.txt
    12 warning: LF will be replaced by CRLF in test.txt.
    13 The file will have its original line endings in your working directory.
    14 
    15 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    16 $ git commit -m '[dev branch] add -dev3- in test.txt'
    17 [dev f018dc6] [dev branch] add -dev3- in test.txt
    18  1 file changed, 1 insertion(+)
    19 
    20 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    21 $ echo 'dev4' >> test.txt
    22 
    23 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    24 $ git add test.txt
    25 warning: LF will be replaced by CRLF in test.txt.
    26 The file will have its original line endings in your working directory.
    27 
    28 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    29 $ git commit -m '[dev branch] add -dev4- in test.txt'
    30 [dev 3d74c2e] [dev branch] add -dev4- in test.txt
    31  1 file changed, 1 insertion(+)
    32 
    33 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    34 $ cat test.txt
    35 master1
    36 dev1
    37 dev2
    38 dev3
    39 dev4
    40 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
    41 $ git log -3
    42 commit 3d74c2ee12b7556cdbd576eb9f6fd9795898ddf6 (HEAD -> dev)
    43 Author: ceaser <ceaserwang@outlook.com>
    44 Date:   Sat Aug 5 21:08:39 2017 +0800
    45 
    46     [dev branch] add -dev4- in test.txt
    47 
    48 commit f018dc6f17d1a9b7c835e43064b695dd4b74e8df
    49 Author: ceaser <ceaserwang@outlook.com>
    50 Date:   Sat Aug 5 21:08:23 2017 +0800
    51 
    52     [dev branch] add -dev3- in test.txt
    53 
    54 commit f28df0d2c6fcb11d34e7551d5bb9b337aa46b259 (test)
    55 Author: ceaser <ceaserwang@outlook.com>
    56 Date:   Sat Aug 5 20:38:36 2017 +0800
    57 
    58     [dev  branch] add -dev2- in test.txt

    切换到test分支增加test3 和test4:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     2 $ cat test.txt
     3 master1
     4 dev1
     5 dev2
     6 
     7 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     8 $ echo 'test3' >> test.txt
     9 
    10 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    11 $ git add test.txt
    12 warning: LF will be replaced by CRLF in test.txt.
    13 The file will have its original line endings in your working directory.
    14 
    15 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    16 $ git commit -m '[test branch] add -test3- in test.txt'
    17 [test 17794bd] [test branch] add -test3- in test.txt
    18  1 file changed, 1 insertion(+)
    19 
    20 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    21 $ echo 'test4' >> test.txt
    22 
    23 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    24 $ git add test.txt
    25 warning: LF will be replaced by CRLF in test.txt.
    26 The file will have its original line endings in your working directory.
    27 
    28 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    29 $ git commit -m '[test branch] add -test4- in test.txt'
    30 [test d5a085e] [test branch] add -test4- in test.txt
    31  1 file changed, 1 insertion(+)
    32 
    33 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    34 $ git log -3
    35 commit d5a085e27b6f25f354c3292078d0069a3605eeaa (HEAD -> test)
    36 Author: ceaser <ceaserwang@outlook.com>
    37 Date:   Sat Aug 5 21:12:56 2017 +0800
    38 
    39     [test branch] add -test4- in test.txt
    40 
    41 commit 17794bda63b337577ad0da88826c28ef2b17ba67
    42 Author: ceaser <ceaserwang@outlook.com>
    43 Date:   Sat Aug 5 21:12:43 2017 +0800
    44 
    45     [test branch] add -test3- in test.txt
    46 
    47 commit f28df0d2c6fcb11d34e7551d5bb9b337aa46b259
    48 Author: ceaser <ceaserwang@outlook.com>
    49 Date:   Sat Aug 5 20:38:36 2017 +0800
    50 
    51     [dev  branch] add -dev2- in test.txt
    52 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
    53 $ cat test.txt
    54 master1
    55 dev1
    56 dev2
    57 test3
    58 test4

    test分支下执行git rebase dev:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     2 $ git rebase dev
     3 First, rewinding head to replay your work on top of it...
     4 Applying: [test branch] add -test3- in test.txt
     5 error: Failed to merge in the changes.
     6 Using index info to reconstruct a base tree...
     7 M       test.txt
     8 Falling back to patching base and 3-way merge...
     9 Auto-merging test.txt
    10 CONFLICT (content): Merge conflict in test.txt
    11 Patch failed at 0001 [test branch] add -test3- in test.txt
    12 The copy of the patch that failed is found in: .git/rebase-apply/patch
    13 
    14 When you have resolved this problem, run "git rebase --continue".
    15 If you prefer to skip this patch, run "git rebase --skip" instead.
    16 To check out the original branch and stop rebasing, run "git rebase --abort".
    17 
    18 
    19 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
    20 $ cat test.txt
    21 master1
    22 dev1
    23 dev2
    24 <<<<<<< HEAD
    25 dev3
    26 dev4
    27 =======
    28 test3
    29 >>>>>>> [test branch] add -test3- in test.txt

    此时出现的冲突我们先保留test3: 
    vi test.txt 删除456行: 
    这里写图片描述

    1 $ cat test.txt
    2 master1
    3 dev1
    4 dev2
    5 test3

    执行git rebase –continue

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
     2 $ git add test.txt
     3 
     4 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test|REBASE 1/2)
     5 $ git rebase --continue
     6 Applying: [test branch] add -test3- in test.txt
     7 Applying: [test branch] add -test4- in test.txt
     8 
     9 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test) //回到test
    10 $

    执行之后出现: 
    Applying: [test branch] add -test3- in test.txt 
    Applying: [test branch] add -test4- in test.txt 
    我们保留了test3的提交,test4就是比test3多了一次提交,直接就成功了。

    查看文件只保留了test的提交:

    1 $  cat test.txt
    2 master1
    3 dev1
    4 dev2
    5 test3
    6 test4

    test的日志: 
    这里写图片描述

    如果我们在dev分支执行merge test会执行fast forward:

     1 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (test)
     2 $ git checkout dev
     3 Switched to branch 'dev'
     4 
     5 Administrator@CeaserWang MINGW64 /e/Study/git_rebase (dev)
     6 $ git merge test
     7 Updating 3d74c2e..9dcfcec
     8 Fast-forward
     9  test.txt | 4 ++--
    10  1 file changed, 2 insertions(+), 2 deletions(-)

    我们用gitk看一下提交历史: 
    他的整个提交历史是一条直线 
    这里写图片描述 
    总的来说rebase的操作是将分叉的情况形成一条直线,但是会修改git的提交历史。

    将test的提交应用到dev: 
    1、git checkout test 
    2、git rebase dev 以dev为基准将test的提交进行回放,挨个的应用到dev上去,然后test的那些提交就会废弃。 
    等价于git merge dev

    git merge 和git rebase区别: 
    merge不会修改提交历史,rebase会修改提交历史。 
    rebase只应用于本地没有提交的代码,如果应用到已经提交到远程的分支不要应用,不然会非常的麻烦,get merge可以应用于远程分支。

    转自:https://blog.csdn.net/wzq6578702/article/details/76736008

  • 相关阅读:
    tesseract动态库依赖关系
    面向对象分析与设计笔记(一)
    用例图笔记
    矩阵乘法求解
    二维数组 Visual Studio怎么监视
    cmake windows caffe cuda版本的切换
    Python入门
    Python基本数据类型
    【LabVIEW】二进制文件的存储与读取方法
    【LabVIEW】文件对话框点击取消后报错、实现自定义文件名
  • 原文地址:https://www.cnblogs.com/UniqueColor/p/9228432.html
Copyright © 2020-2023  润新知