Git分支-基础与基本操作

分支本质上其实就是一个指向某次提交的可变指针。Git的默认分支名字为master 。而我们是怎么知道当前处于哪个分支当中呢?答案就是在于HEAD这个十分特殊的指针,它专门用于指向于本地分支中的当前分支。

Note: 测试数据准备
创建新文件“branch.txt”,并提交三个版本

1
this Is test branch.

查看分支

1
2
3
$ git branch
* master
test

命令会列出所有分支,当前分支前面会标一个*号。

创建分支

1
$ git branch test

查看分支

1
2
3
$ git branch
* master
test

切换分支

1
2
$ git checkout test
Switched to branch 'test'
  • 查看分支
    1
    2
    3
    $ git branch
    master

    * test

Notes:
我们可能通过 git checkout 命令加上-b参数表示创建并切换分支上:
git checkout -b [name]

  • 测试分支
  1. 此时对‘branch.txt’进行修改,并在‘test’分支上提交。

    1
    2
    this Is test branch.
    add On 'test' branch.
  2. 切换到‘master’分支

    1
    2
    $ git checkout master
    Switched to branch 'master'

可以看到刚才增加的一行不见了。因为那个提交是在test分支上,而master分支没有变化。

合并分支(Fast forward)

git merge 命令用于合并指定分支到当前分支。

1
2
3
4
5
$ git merge test
Updating 42139cd..c587e76
Fast-forward
branch.txt | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

再查看文件内容,和test分支的最新提交是完全一样的。

删除分支

1
2
$ git branch -d test
Deleted branch test (was c587e76).

查看分支

1
2
$ git branch
* master

分支合并冲突

  1. 创建新分支

    1
    2
    $ git checkout -b confict
    Switched to a new branch 'confict'
  2. 修改文件,并提交

    1
    2
    3
    this Is test branch.
    add On 'test' branch.
    add On 'confict' branch.

  3. 切换到‘master’分支

    1
    2
    $ git checkout master
    Switched to branch 'master'
  4. 改文件,并提交

    1
    2
    3
    this Is test branch.
    add On 'test' branch.
    add On 'master' branch.

  5. 合并分支

    1
    2
    3
    4
    $ git merge confict
    Auto-merging branch.txt
    CONFLICT (content): Merge conflict in branch.txt
    Automatic merge failed; fix conflicts and then commit the result.
  6. 查看git状态

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    $ git status
    On branch master
    You have unmerged paths.
    (fix conflicts and run "git commit")

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

    both modified: branch.txt

    no changes added to commit (use "git add" and/or "git commit -a")
  7. 打开文件并修改

    • 查看

      1
      2
      3
      4
      5
      6
      7
      this Is test branch.
      add On 'test' branch.
      <<<<<<< HEAD

      add On 'master' branch.
      =======

      add On 'confict' branch.
      >>>>>>> confict

    • 修改

      1
      2
      3
      4
      this Is test branch.
      add On 'test' branch.
      add On 'master' branch.
      add On 'confict' branch.

  8. 再提交

    1
    2
    3
    $ git add branch.txt
    $ git commit -m "concflic fixed"
    [master c44d910] concflic fixed

  9. 查看分支的合并

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    $ git log --graph
    * commit c44d9107ec810b03af593dd164bd6d3a9b6333e7
    |\ Merge: 4d0342e 17047cd
    | | Author: yongfei.hu <yongfeiuall@163.com>
    | | Date: Wed Nov 11 15:39:51 2015 +0800
    | |

    | | concflic fixed
    | |

    | * commit 17047cd86e0615b6bdcfe4df8f27f40776deb276
    | | Author: yongfei.hu <yongfeiuall@163.com>
    | | Date: Wed Nov 11 15:29:06 2015 +0800
    | |

    | | add 'confict' branch
    | |

    * | commit 4d0342e3a989d5ad21104a75055267d41363beee
    |/ Author: yongfei.hu <yongfeiuall@163.com>
    | Date: Wed Nov 11 15:31:05 2015 +0800
    |

    | add 'master' branch
    |

合并分支(普通合并)

快速合并就看不出来曾经做过合并,删除分支后会丢掉分支信息。普通合并(Git就会在merge时生成一个新的commit),合并后的历史有分支记录,能看出来曾经做过合并。
git mrege –no-ff [branch name],–no-ff参数表示禁用快速合并。

  1. 创建新分支

    1
    2
    $ git checkout -b noff
    Switched to a new branch 'noff'
  2. 修改文件,并提交

    1
    2
    3
    4
    5
    this Is test branch.
    add On 'test' branch.
    add On 'master' branch.
    add On 'confict' branch.
    add On 'noff' branch.

  3. 切换到‘master’分支

    1
    2
    $ git checkout master
    Switched to branch 'master'
  4. 合并分支

    1
    2
    3
    4
    $ git merge --no-ff -m "merge with no-ff" noff
    Merge made by the 'recursive' strategy.
    branch.txt | 3 ++-
    1 file changed, 2 insertions(+), 1 deletion(-)

    因为本次合并要创建一个新的commit,所以加上-m参数。

  5. 查看分支的合并
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    $ git log --graph
    * commit 83962f9f60d52784a7b0d9241165e1f81d646bc4
    |\ Merge: 3cf9a8b 84d3355
    | | Author: yongfei.hu <yongfeiuall@163.com>
    | | Date: Thu Nov 12 08:11:42 2015 +0800
    | |

    | | merge with no-ff
    | |

    | * commit 84d3355e070bb4e57ee5055099a227d928f07039
    |/ Author: yongfei.hu <yongfeiuall@163.com>
    | Date: Thu Nov 12 08:09:46 2015 +0800
    |

    | add on 'noff' branch
    |

    * commit 3cf9a8be65b1c7d6ab53dac8dd0628016e7c52d2
    | Author: yongfei.hu <yongfeiuall@163.com>
    | Date: Wed Nov 11 15:49:47 2015 +0800
    |

    | a
唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!