一、分支名

  • 分支名不能以斜线结尾
  • 分支名不能以减号开头
  • 以斜杠分割的组件不能以点开头(feature/.new)
  • 分支名的任何地方都不能包含连个连续的点
  • 分支名不能包含空格或空白符
  • 分支名不能包含波浪线~、插入符^、冒号:、问号?、星号*、左括号[,以及ASCII码控制字符

二、创建分支

  1. [root@localhost public_html]# git branch pu-1138
  2. 为了在软件2.3发布版本上修复一个BUG,可以指定rel-2.3的分支为开始提交
  3. [root@localhost public_html]# git branch pu-1138 rel-2.3
  4. [root@localhost public_html]# git branch
  5. * master
  6. pu-1138
  7. [root@localhost public_html]# git show-branch
  8. * [master] mv bar to foo
  9. ! [pu-1138] mv bar to foo
  10. --
  11. *+ [master] mv bar to foo
  12. [root@localhost public_html]# git show-branch pu-1138
  13. [pu-1138] mv bar to foo

例子:

  1. [root@localhost public_html]# git branch testing
  2. [root@localhost public_html]# git branch
  3. * master
  4. testing
  5. [root@localhost public_html]# ls
  6. foo.html index.html
  7. [root@localhost public_html]# echo 'testing' > test.txt
  8. [root@localhost public_html]# ls
  9. foo.html index.html test.txt
  10. [root@localhost public_html]# git add .
  11. [root@localhost public_html]# git commit -m "add test.txt"
  12. [master b0b257c] add test.txt
  13. 1 files changed, 1 insertions(+), 0 deletions(-)
  14. create mode 100644 test.txt
  15. [root@localhost public_html]# ls
  16. foo.html index.html test.txt
  17. [root@localhost public_html]# git checkout testing
  18. Switched to branch 'testing'
  19. [root@localhost public_html]# ls
  20. foo.html index.html
  21. [root@localhost public_html]# git checkout master
  22. Switched to branch 'master'
  23. [root@localhost public_html]# ls
  24. foo.html index.html test.txt
  25. [root@localhost public_html]# git checkout -b newtest
  26. Switched to a new branch 'newtest'
  27. [root@localhost public_html]# ls
  28. foo.html index.html test.txt
  29. [root@localhost public_html]# git rm test.txt
  30. rm 'test.txt'
  31. [root@localhost public_html]# ls
  32. foo.html index.html
  33. [root@localhost public_html]# git commit -am "removed test.txt"
  34. [newtest 480b789] removed test.txt
  35. 1 files changed, 0 insertions(+), 1 deletions(-)
  36. delete mode 100644 test.txt
  37. [root@localhost public_html]# git checkout master
  38. Switched to branch 'master'
  39. [root@localhost public_html]# ls
  40. foo.html index.html test.txt

三、检出分支

  1. [root@localhost public_html]# git branch
  2. * master
  3. pu-1138
  4. [root@localhost public_html]# git checkout pu-1138
  5. Switched to branch 'pu-1138'
  6. [root@localhost public_html]# ls
  7. foo.html index.html
  8. [root@localhost public_html]# git branch
  9. master
  10. * pu-1138

四、删除分支

  1. [root@localhost public_html]# git branch -d pu-1138
  2. Deleted branch pu-1138 (was aa431d9).
  3. [root@localhost public_html]# git branch
  4. * master

五、合并分支

(1)合并一个分支

  1. [root@localhost public_html]# git branch
  2. * master
  3. newtest
  4. testing
  5. [root@localhost public_html]# git checkout testing
  6. Switched to branch 'testing'
  7. [root@localhost public_html]# ls
  8. foo.html index.html
  9. [root@localhost public_html]# git merge newtest
  10. Updating aa431d9..b488b19
  11. Fast-forward
  12. yes.html | 1 +
  13. 1 files changed, 1 insertions(+), 0 deletions(-)
  14. create mode 100644 yes.html
  15. [root@localhost public_html]# ls
  16. foo.html index.html yes.html

(2)合并两个分支

  1. [root@localhost ~]# mkdir fenzhi_total
  2. [root@localhost ~]# cd fenzhi_total/
  3. [root@localhost fenzhi_total]# ls
  4. [root@localhost fenzhi_total]# git init
  5. Initialized empty Git repository in /root/fenzhi_total/.git/
  6. [root@localhost fenzhi_total]# cat > file
  7. Line 1 stuff
  8. Line 2 stuff
  9. Line 3 stuff
  10. [root@localhost fenzhi_total]# ls
  11. file
  12. [root@localhost fenzhi_total]# git add file
  13. [root@localhost fenzhi_total]# git commit -m "Initial 3 line file"
  14. [master (root-commit) 2f793c2] Initial 3 line file
  15. 1 files changed, 3 insertions(+), 0 deletions(-)
  16. create mode 100644 file
  17. [root@localhost fenzhi_total]# cat > other_file
  18. Here is stuff on another file!
  19. [root@localhost fenzhi_total]# git add other_file
  20. [root@localhost fenzhi_total]# git commit -m "Another file"
  21. [master 9f175ec] Another file
  22. 1 files changed, 1 insertions(+), 0 deletions(-)
  23. create mode 100644 other_file
  24. [root@localhost fenzhi_total]# git checkout -b alternate master^
  25. Switched to a new branch 'alternate'
  26. [root@localhost fenzhi_total]# git show-branch
  27. * [alternate] Initial 3 line file
  28. ! [master] Another file
  29. --
  30. + [master] Another file
  31. *+ [alternate] Initial 3 line file
  32. [root@localhost fenzhi_total]# cat >> file
  33. Line 4 alternate stuff
  34. [root@localhost fenzhi_total]# git commit -am "Add alternate's line 4"
  35. [alternate c004281] Add alternate's line 4
  36. 1 files changed, 1 insertions(+), 0 deletions(-)
  37. [root@localhost fenzhi_total]# git checkout master
  38. Switched to branch 'master'
  39. [root@localhost fenzhi_total]# git status
  40. # On branch master
  41. nothing to commit (working directory clean)
  42. [root@localhost fenzhi_total]# ls
  43. file other_file
  44. [root@localhost fenzhi_total]# git merge alternate
  45. Merge made by recursive.
  46. file | 1 +
  47. 1 files changed, 1 insertions(+), 0 deletions(-)
  48. [root@localhost fenzhi_total]# git log --graph --pretty=oneline --abbrev-commit
  49. * bdb04ee Merge branch 'alternate'
  50. |\
  51. | * c004281 Add alternate's line 4
  52. * | 9f175ec Another file
  53. |/
  54. * 2f793c2 Initial 3 line file
  55. [root@localhost fenzhi_total]# cat file
  56. Line 1 stuff
  57. Line 2 stuff
  58. Line 3 stuff
  59. Line 4 alternate stuff

(3)有冲突的分支

  1. [root@localhost fenzhi_total]# git branch
  2. alternate
  3. * master
  4. [root@localhost fenzhi_total]#
  5. [root@localhost fenzhi_total]#
  6. [root@localhost fenzhi_total]# git checkout master
  7. Already on 'master'
  8. [root@localhost fenzhi_total]# cat >> file
  9. Line 5 stuff
  10. Line 6 stuff
  11. [root@localhost fenzhi_total]# git commit -am "Add line 5 and 6"
  12. [master 6e1cf57] Add line 5 and 6
  13. 1 files changed, 2 insertions(+), 0 deletions(-)
  14. [root@localhost fenzhi_total]# git checkout alternate
  15. Switched to branch 'alternate'
  16. [root@localhost fenzhi_total]# git show-branch
  17. * [alternate] Add alternate's line 4
  18. ! [master] Add line 5 and 6
  19. --
  20. + [master] Add line 5 and 6
  21. *+ [alternate] Add alternate's line 4
  22. [root@localhost fenzhi_total]# cat >> file
  23. Line 5 alternate stuff
  24. Line 6 alternate stuff
  25. [root@localhost fenzhi_total]# cat file
  26. Line 1 stuff
  27. Line 2 stuff
  28. Line 3 stuff
  29. Line 4 alternate stuff
  30. Line 5 alternate stuff
  31. Line 6 alternate stuff
  32. [root@localhost fenzhi_total]# git diff
  33. diff --git a/file b/file
  34. index a29c52b..802acf8 100644
  35. --- a/file
  36. +++ b/file
  37. @@ -2,3 +2,5 @@ Line 1 stuff
  38. Line 2 stuff
  39. Line 3 stuff
  40. Line 4 alternate stuff
  41. +Line 5 alternate stuff
  42. +Line 6 alternate stuff
  43. [root@localhost fenzhi_total]# git commit -am "Add alternate line 5 and 6"
  44. [alternate 4596a0d] Add alternate line 5 and 6
  45. 1 files changed, 2 insertions(+), 0 deletions(-)
  46. [root@localhost fenzhi_total]# git show-branch
  47. * [alternate] Add alternate line 5 and 6
  48. ! [master] Add line 5 and 6
  49. --
  50. * [alternate] Add alternate line 5 and 6
  51. + [master] Add line 5 and 6
  52. *+ [alternate^] Add alternate's line 4
  53. [root@localhost fenzhi_total]# git checkout master
  54. Switched to branch 'master'
  55. [root@localhost fenzhi_total]# git merge alternate
  56. Auto-merging file
  57. CONFLICT (content): Merge conflict in file
  58. Automatic merge failed; fix conflicts and then commit the result.
  59. 此时出现合并冲突
  60. [root@localhost fenzhi_total]# git diff
  61. diff --cc file
  62. index 4d77dd1,802acf8..0000000
  63. --- a/file
  64. +++ b/file
  65. @@@ -2,5 -2,5 +2,10 @@@ Line 1 stuf
  66. Line 2 stuff
  67. Line 3 stuff
  68. Line 4 alternate stuff
  69. ++<<<<<<< HEAD
  70. +Line 5 stuff
  71. +Line 6 stuff
  72. ++=======
  73. + Line 5 alternate stuff
  74. + Line 6 alternate stuff
  75. ++>>>>>>> alternate
  76. [root@localhost fenzhi_total]# cat file
  77. Line 1 stuff
  78. Line 2 stuff
  79. Line 3 stuff
  80. Line 4 alternate stuff
  81. <<<<<<< HEAD
  82. Line 5 stuff
  83. Line 6 stuff
  84. =======
  85. Line 5 alternate stuff
  86. Line 6 alternate stuff
  87. >>>>>>> alternate
  88. 此时可以选择只取一边的版本或两边混合(需要手动修改)
  89. [root@localhost fenzhi_total]# vim file
  90. [root@localhost fenzhi_total]# cat file
  91. Line 1 stuff
  92. Line 2 stuff
  93. Line 3 stuff
  94. Line 4 alternate stuff
  95. Line 5 alternate stuff
  96. Line 6 stuff
  97. [root@localhost fenzhi_total]# git add file
  98. [root@localhost fenzhi_total]# git commit
  99. [master 5a8e0fa] Merge branch 'alternate'
  100. [root@localhost fenzhi_total]# git show-branch
  101. ! [alternate] Add alternate line 5 and 6
  102. * [master] Merge branch 'alternate'
  103. --
  104. - [master] Merge branch 'alternate'
  105. +* [alternate] Add alternate line 5 and 6
  106. [root@localhost fenzhi_total]# git log
  107. commit 5a8e0fa2318cc9d9aa4171d8aaff0105c4a06331
  108. Merge: 6e1cf57 4596a0d
  109. Author: tong <tongxiaoda@anzhi.com>
  110. Date: Tue Feb 27 16:24:48 2018 +0800
  111. Merge branch 'alternate'
  112. Conflicts:
  113. file
  114. change file
  115. commit 4596a0d219ceb1ac33214a8b2f891d7788ea80e5
  116. Author: tong <tongxiaoda@anzhi.com>
  117. Date: Tue Feb 27 16:18:11 2018 +0800
  118. Add alternate line 5 and 6
  119. commit 6e1cf5717cd1012f527262d3108c8fad22dc4dde
  120. Author: tong <tongxiaoda@anzhi.com>
  121. Date: Tue Feb 27 16:16:26 2018 +0800
  122. Add line 5 and 6
  123. commit bdb04eec38411ea7d490054f4d236af783debb3c
  124. Merge: 9f175ec c004281
  125. Author: tong <tongxiaoda@anzhi.com>
  126. Date: Tue Feb 27 16:13:20 2018 +0800
  127. Merge branch 'alternate'
  128. commit c0042812ba692e1a16c0a5e0d9c9dc5e9b1f7a7e
  129. Author: tong <tongxiaoda@anzhi.com>
  130. Date: Tue Feb 27 16:12:39 2018 +0800
  131. Add alternate's line 4
  132. commit 9f175ec6e0e9b85de029969bb1da897a84abc4f5
  133. Author: tong <tongxiaoda@anzhi.com>
  134. Date: Tue Feb 27 16:11:03 2018 +0800
  135. Another file
  136. commit 2f793c27b2bc1ab5b046eec2790e88e751727566
  137. Author: tong <tongxiaoda@anzhi.com>
  138. Date: Tue Feb 27 16:10:12 2018 +0800
  139. Initial 3 line file

(4)处理合并冲突

  1. [root@localhost fz_t]# git init
  2. Initialized empty Git repository in /root/fz_t/.git/
  3. [root@localhost fz_t]# echo hello > hello
  4. [root@localhost fz_t]# git add hello
  5. [root@localhost fz_t]# git commit -m "Initial hello file"
  6. [master (root-commit) 296dfa0] Initial hello file
  7. 1 files changed, 1 insertions(+), 0 deletions(-)
  8. create mode 100644 hello
  9. [root@localhost fz_t]# git checkout -b alt
  10. Switched to a new branch 'alt'
  11. [root@localhost fz_t]# echo world >> hello
  12. [root@localhost fz_t]# echo 'Yay!' >> hello
  13. [root@localhost fz_t]# git commit -am "One world"
  14. [alt ea3c204] One world
  15. 1 files changed, 2 insertions(+), 0 deletions(-)
  16. [root@localhost fz_t]# git checkout master
  17. Switched to branch 'master'
  18. [root@localhost fz_t]# echo worlds >> hello
  19. [root@localhost fz_t]# echo 'Yay!' >> hello
  20. [root@localhost fz_t]# git commit -am "All worlds"
  21. [master 8f316ee] All worlds
  22. 1 files changed, 2 insertions(+), 0 deletions(-)
  23. [root@localhost fz_t]# git merge alt
  24. Auto-merging hello
  25. CONFLICT (content): Merge conflict in hello
  26. Automatic merge failed; fix conflicts and then commit the result.
  27. 定位冲突文件
  28. [root@localhost fz_t]# git status
  29. # On branch master
  30. # Unmerged paths:
  31. # (use "git add/rm <file>..." as appropriate to mark resolution)
  32. #
  33. # both modified: hello
  34. #
  35. no changes added to commit (use "git add" and/or "git commit -a")
  36. [root@localhost fz_t]# git ls-files -u
  37. 100644 ce013625030ba8dba906f756967f9e9ca394464a 1 hello
  38. 100644 e63164d9518b1e6caf28f455ac86c8246f78ab70 2 hello
  39. 100644 562080a4c6518e1bf67a9f58a32a67bff72d4f00 3 hello
  40. 检查冲突
  41. [root@localhost fz_t]# git diff HEAD
  42. diff --git a/hello b/hello
  43. index e63164d..1f2f61c 100644
  44. --- a/hello
  45. +++ b/hello
  46. @@ -1,3 +1,7 @@
  47. hello
  48. +<<<<<<< HEAD
  49. worlds
  50. +=======
  51. +world
  52. +>>>>>>> alt
  53. Yay!
  54. [root@localhost fz_t]# git diff MERGE_HEAD
  55. diff --git a/hello b/hello
  56. index 562080a..1f2f61c 100644
  57. --- a/hello
  58. +++ b/hello
  59. @@ -1,3 +1,7 @@
  60. hello
  61. +<<<<<<< HEAD
  62. +worlds
  63. +=======
  64. world
  65. +>>>>>>> alt
  66. Yay!
  67. 或使用以下命令检查
  68. [root@localhost fz_t]# git log --merge --left-right -p hello
  69. commit <8f316ee211109affd61eb89c001ac139f7274406
  70. Author: tong <tongxiaoda@anzhi.com>
  71. Date: Tue Feb 27 16:28:35 2018 +0800
  72. All worlds
  73. diff --git a/hello b/hello
  74. index ce01362..e63164d 100644
  75. --- a/hello
  76. +++ b/hello
  77. @@ -1 +1,3 @@
  78. hello
  79. +worlds
  80. +Yay!
  81. commit >ea3c2044aae6b375d3cba2fa0ac9ff1d78dd6779
  82. Author: tong <tongxiaoda@anzhi.com>
  83. Date: Tue Feb 27 16:27:53 2018 +0800
  84. One world
  85. diff --git a/hello b/hello
  86. index ce01362..562080a 100644
  87. --- a/hello
  88. +++ b/hello
  89. @@ -1 +1,3 @@
  90. hello
  91. +world
  92. +Yay!
  93. 不使用任何版本,继续修改file
  94. [root@localhost fz_t]# cat hello
  95. hello
  96. worldly ones
  97. Yay!
  98. [root@localhost fz_t]# git diff
  99. diff --cc hello
  100. index e63164d,562080a..0000000
  101. --- a/hello
  102. +++ b/hello
  103. @@@ -1,3 -1,3 +1,3 @@@
  104. hello
  105. - worlds
  106. -world
  107. ++worldly ones
  108. Yay!
  109. 结束解决冲突
  110. [root@localhost fz_t]# git add hello
  111. [root@localhost fz_t]# git ls-files -s
  112. 100644 01e406a245782525e6b02cadf6df25e212563967 0 hello
  113. [root@localhost fz_t]# cat .git/MERGE_MSG
  114. Merge branch 'alt'
  115. Conflicts:
  116. hello
  117. [root@localhost fz_t]# git commit
  118. [master af5cf67] Merge branch 'alt'
  119. [root@localhost fz_t]# git show
  120. commit af5cf675e311952e5377e5942dd1d7689427eec8
  121. Merge: 8f316ee ea3c204
  122. Author: tong <tongxiaoda@anzhi.com>
  123. Date: Tue Feb 27 16:38:29 2018 +0800
  124. Merge branch 'alt'
  125. Conflicts:
  126. hello
  127. diff --cc hello
  128. index e63164d,562080a..01e406a
  129. --- a/hello
  130. +++ b/hello
  131. @@@ -1,3 -1,3 +1,3 @@@
  132. hello
  133. - worlds
  134. -world
  135. ++worldly ones
  136. Yay!

(5)中止或重新启动合并

  1. git reset --hard HEAD
  2. 把工作目录和索引都还原到git merge命令之前
  3. git reset --hard ORIG_HEAD
  4. 中止或在它已经结束后放弃

GIT使用—分支与合并的更多相关文章

  1. Git入门指南十一:Git branch 分支与合并分支

    十五. Git branch 分支 查看当前有哪些branch bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch * master ...

  2. Git branch 分支与合并分支

    Git branch 分支 查看当前有哪些branch bixiaopeng@bixiaopengtekiMacBook-Pro xmrobotium$ git branch * master 新建一 ...

  3. Git 创建分支与合并分支

    下面以branchName=>aiMdTest为例介绍 1.  下载code git clone masterUrl iva(另存文件名) 2.  创建并切换分支 cd iva git chec ...

  4. git branch 分支与合并

    在使用 git 进行分支开发与合并的时候需要用到这些命令.其他基本 git 命令参考 Git 简易食用指南 git branch 查看分支 git branch 查看当前分支情况 创建分支 git b ...

  5. Git 创建分支并合并主分支

    首先,我们创建dev分支,然后切换到dev分支: $ git checkout -b dev(等价于 $ git branch dev $ git checkout dev ) Switched to ...

  6. Git的分支与合并

    在Git里面我们可以创建不同的分支,来进行调试.发布.维护等不同工作,而互不干扰.下面我们还是来创建一个试验仓库,看一下Git分支运作的台前幕后: $rm -rf test_branch_proj $ ...

  7. idea如何在git上将分支代码合并到主干

    1.首先将idea中的代码分支切换到master分支,可以看到我们在dev上提交的代码 在master上是没有的 2.如图所示,在remote branch 上选择分支,点击后面的三角图标,展开之后选 ...

  8. git创建分支与合并分支

    git branch myfeture 创建分支 git checkout myfeture git add --all git commit -m git push origin myfeture ...

  9. Git:创建与合并分支

    1.1创建dev分支,使用命令符 git branch 分支名称. 1.2将HEAD指针切换到dev分支,使用命名符git checkout 分支名称. 注:创建并且转移可以合并为一个步骤,使用命令符 ...

随机推荐

  1. 【黑金原创教程】【Modelsim】【第六章】结束就是开始

    声明:本文为黑金动力社区(http://www.heijin.org)原创教程,如需转载请注明出处,谢谢! 黑金动力社区2013年原创教程连载计划: http://www.cnblogs.com/al ...

  2. 【BZOJ4819】[Sdoi2017]新生舞会 01分数规划+费用流

    [BZOJ4819][Sdoi2017]新生舞会 Description 学校组织了一次新生舞会,Cathy作为经验丰富的老学姐,负责为同学们安排舞伴.有n个男生和n个女生参加舞会 买一个男生和一个女 ...

  3. ios 将图片做成圆形

    UIImageView * imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"oiuyfdsa.png ...

  4. PAT 甲级 1025 PAT Ranking

    1025. PAT Ranking (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programmi ...

  5. git 学习(2)--恢复版本

    查看修改历史记录 $ git log commit fba77877d316436c1b774b8933380ebcac668040 Author: keith <ustbfxx@163.com ...

  6. cmd运行php

    w D:\wamp64\bin\php\php7.0.4\php.exe  执行了 D:\wamp64\bin\php\php7.0.4\wtest.php

  7. jsp->jar

    (2)新建 WEB-INF目录 (3)在 WEB-INF/web.xml 中输入如下内容 <web-app xmlns="http://java.sun.com/xml/ns/j2ee ...

  8. Junit 3.8.1 源码分析(一)

    写在前面:本文基于Junit3.8.1版本,因为这是我第一次进行源码学习,先从简单的源码开始学起 1. 示例代码 1.1 准备工作 下载Junit3.8.1的JAR包 需要下载junit-3.8.1- ...

  9. ubuntu 打开 gbk编码的txt乱码

    iconv -f gbk -t utf8 filename.txt > filename.txt.utf8

  10. ubuntu配置Python-Django Nginx+uwsgi 安装配置

    安装Nginx sudo apt-get install nginx ubantu安装完Nginx后,文件结构大致为: 所有的配置文件都在 /etc/nginx下: 启动程序文件在 /usr/sbin ...