一、分支名

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

二、创建分支

[root@localhost public_html]# git branch pu-1138

为了在软件2.3发布版本上修复一个BUG,可以指定rel-2.3的分支为开始提交
[root@localhost public_html]# git branch pu-1138 rel-2.3 [root@localhost public_html]# git branch
* master
pu-1138 [root@localhost public_html]# git show-branch
* [master] mv bar to foo
! [pu-1138] mv bar to foo
--
*+ [master] mv bar to foo
[root@localhost public_html]# git show-branch pu-1138
[pu-1138] mv bar to foo

例子:

[root@localhost public_html]# git branch testing
[root@localhost public_html]# git branch
* master
testing
[root@localhost public_html]# ls
foo.html index.html
[root@localhost public_html]# echo 'testing' > test.txt
[root@localhost public_html]# ls
foo.html index.html test.txt
[root@localhost public_html]# git add .
[root@localhost public_html]# git commit -m "add test.txt"
[master b0b257c] add test.txt
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test.txt
[root@localhost public_html]# ls
foo.html index.html test.txt
[root@localhost public_html]# git checkout testing
Switched to branch 'testing'
[root@localhost public_html]# ls
foo.html index.html
[root@localhost public_html]# git checkout master
Switched to branch 'master'
[root@localhost public_html]# ls
foo.html index.html test.txt [root@localhost public_html]# git checkout -b newtest
Switched to a new branch 'newtest'
[root@localhost public_html]# ls
foo.html index.html test.txt
[root@localhost public_html]# git rm test.txt
rm 'test.txt'
[root@localhost public_html]# ls
foo.html index.html
[root@localhost public_html]# git commit -am "removed test.txt"
[newtest 480b789] removed test.txt
1 files changed, 0 insertions(+), 1 deletions(-)
delete mode 100644 test.txt
[root@localhost public_html]# git checkout master
Switched to branch 'master'
[root@localhost public_html]# ls
foo.html index.html test.txt

三、检出分支

[root@localhost public_html]# git branch
* master
pu-1138
[root@localhost public_html]# git checkout pu-1138
Switched to branch 'pu-1138'
[root@localhost public_html]# ls
foo.html index.html
[root@localhost public_html]# git branch
master
* pu-1138

四、删除分支

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

五、合并分支

(1)合并一个分支

[root@localhost public_html]# git branch
* master
newtest
testing
[root@localhost public_html]# git checkout testing
Switched to branch 'testing'
[root@localhost public_html]# ls
foo.html index.html
[root@localhost public_html]# git merge newtest
Updating aa431d9..b488b19
Fast-forward
yes.html | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 yes.html
[root@localhost public_html]# ls
foo.html index.html yes.html

(2)合并两个分支

[root@localhost ~]# mkdir fenzhi_total
[root@localhost ~]# cd fenzhi_total/
[root@localhost fenzhi_total]# ls
[root@localhost fenzhi_total]# git init
Initialized empty Git repository in /root/fenzhi_total/.git/
[root@localhost fenzhi_total]# cat > file
Line 1 stuff
Line 2 stuff
Line 3 stuff
[root@localhost fenzhi_total]# ls
file
[root@localhost fenzhi_total]# git add file
[root@localhost fenzhi_total]# git commit -m "Initial 3 line file"
[master (root-commit) 2f793c2] Initial 3 line file
1 files changed, 3 insertions(+), 0 deletions(-)
create mode 100644 file
[root@localhost fenzhi_total]# cat > other_file
Here is stuff on another file!
[root@localhost fenzhi_total]# git add other_file
[root@localhost fenzhi_total]# git commit -m "Another file"
[master 9f175ec] Another file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 other_file
[root@localhost fenzhi_total]# git checkout -b alternate master^
Switched to a new branch 'alternate'
[root@localhost fenzhi_total]# git show-branch
* [alternate] Initial 3 line file
! [master] Another file
--
+ [master] Another file
*+ [alternate] Initial 3 line file
[root@localhost fenzhi_total]# cat >> file
Line 4 alternate stuff
[root@localhost fenzhi_total]# git commit -am "Add alternate's line 4"
[alternate c004281] Add alternate's line 4
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost fenzhi_total]# git checkout master
Switched to branch 'master'
[root@localhost fenzhi_total]# git status
# On branch master
nothing to commit (working directory clean)
[root@localhost fenzhi_total]# ls
file other_file
[root@localhost fenzhi_total]# git merge alternate
Merge made by recursive.
file | 1 +
1 files changed, 1 insertions(+), 0 deletions(-)
[root@localhost fenzhi_total]# git log --graph --pretty=oneline --abbrev-commit
* bdb04ee Merge branch 'alternate'
|\
| * c004281 Add alternate's line 4
* | 9f175ec Another file
|/
* 2f793c2 Initial 3 line file
[root@localhost fenzhi_total]# cat file
Line 1 stuff
Line 2 stuff
Line 3 stuff
Line 4 alternate stuff

(3)有冲突的分支

[root@localhost fenzhi_total]# git branch
alternate
* master
[root@localhost fenzhi_total]#
[root@localhost fenzhi_total]#
[root@localhost fenzhi_total]# git checkout master
Already on 'master'
[root@localhost fenzhi_total]# cat >> file
Line 5 stuff
Line 6 stuff
[root@localhost fenzhi_total]# git commit -am "Add line 5 and 6"
[master 6e1cf57] Add line 5 and 6
1 files changed, 2 insertions(+), 0 deletions(-)
[root@localhost fenzhi_total]# git checkout alternate
Switched to branch 'alternate'
[root@localhost fenzhi_total]# git show-branch
* [alternate] Add alternate's line 4
! [master] Add line 5 and 6
--
+ [master] Add line 5 and 6
*+ [alternate] Add alternate's line 4
[root@localhost fenzhi_total]# cat >> file
Line 5 alternate stuff
Line 6 alternate stuff
[root@localhost fenzhi_total]# cat file
Line 1 stuff
Line 2 stuff
Line 3 stuff
Line 4 alternate stuff
Line 5 alternate stuff
Line 6 alternate stuff
[root@localhost fenzhi_total]# git diff
diff --git a/file b/file
index a29c52b..802acf8 100644
--- a/file
+++ b/file
@@ -2,3 +2,5 @@ Line 1 stuff
Line 2 stuff
Line 3 stuff
Line 4 alternate stuff
+Line 5 alternate stuff
+Line 6 alternate stuff
[root@localhost fenzhi_total]# git commit -am "Add alternate line 5 and 6"
[alternate 4596a0d] Add alternate line 5 and 6
1 files changed, 2 insertions(+), 0 deletions(-)
[root@localhost fenzhi_total]# git show-branch
* [alternate] Add alternate line 5 and 6
! [master] Add line 5 and 6
--
* [alternate] Add alternate line 5 and 6
+ [master] Add line 5 and 6
*+ [alternate^] Add alternate's line 4
[root@localhost fenzhi_total]# git checkout master
Switched to branch 'master'
[root@localhost fenzhi_total]# git merge alternate
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result. 此时出现合并冲突
[root@localhost fenzhi_total]# git diff
diff --cc file
index 4d77dd1,802acf8..0000000
--- a/file
+++ b/file
@@@ -2,5 -2,5 +2,10 @@@ Line 1 stuf
Line 2 stuff
Line 3 stuff
Line 4 alternate stuff
++<<<<<<< HEAD
+Line 5 stuff
+Line 6 stuff
++=======
+ Line 5 alternate stuff
+ Line 6 alternate stuff
++>>>>>>> alternate
[root@localhost fenzhi_total]# cat file
Line 1 stuff
Line 2 stuff
Line 3 stuff
Line 4 alternate stuff
<<<<<<< HEAD
Line 5 stuff
Line 6 stuff
=======
Line 5 alternate stuff
Line 6 alternate stuff
>>>>>>> alternate 此时可以选择只取一边的版本或两边混合(需要手动修改)
[root@localhost fenzhi_total]# vim file
[root@localhost fenzhi_total]# cat file
Line 1 stuff
Line 2 stuff
Line 3 stuff
Line 4 alternate stuff
Line 5 alternate stuff
Line 6 stuff
[root@localhost fenzhi_total]# git add file
[root@localhost fenzhi_total]# git commit
[master 5a8e0fa] Merge branch 'alternate'
[root@localhost fenzhi_total]# git show-branch
! [alternate] Add alternate line 5 and 6
* [master] Merge branch 'alternate'
--
- [master] Merge branch 'alternate'
+* [alternate] Add alternate line 5 and 6
[root@localhost fenzhi_total]# git log
commit 5a8e0fa2318cc9d9aa4171d8aaff0105c4a06331
Merge: 6e1cf57 4596a0d
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:24:48 2018 +0800 Merge branch 'alternate' Conflicts:
file change file commit 4596a0d219ceb1ac33214a8b2f891d7788ea80e5
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:18:11 2018 +0800 Add alternate line 5 and 6 commit 6e1cf5717cd1012f527262d3108c8fad22dc4dde
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:16:26 2018 +0800 Add line 5 and 6 commit bdb04eec38411ea7d490054f4d236af783debb3c
Merge: 9f175ec c004281
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:13:20 2018 +0800 Merge branch 'alternate' commit c0042812ba692e1a16c0a5e0d9c9dc5e9b1f7a7e
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:12:39 2018 +0800 Add alternate's line 4 commit 9f175ec6e0e9b85de029969bb1da897a84abc4f5
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:11:03 2018 +0800 Another file commit 2f793c27b2bc1ab5b046eec2790e88e751727566
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:10:12 2018 +0800 Initial 3 line file

(4)处理合并冲突

[root@localhost fz_t]# git init
Initialized empty Git repository in /root/fz_t/.git/
[root@localhost fz_t]# echo hello > hello
[root@localhost fz_t]# git add hello
[root@localhost fz_t]# git commit -m "Initial hello file"
[master (root-commit) 296dfa0] Initial hello file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 hello
[root@localhost fz_t]# git checkout -b alt
Switched to a new branch 'alt'
[root@localhost fz_t]# echo world >> hello
[root@localhost fz_t]# echo 'Yay!' >> hello
[root@localhost fz_t]# git commit -am "One world"
[alt ea3c204] One world
1 files changed, 2 insertions(+), 0 deletions(-)
[root@localhost fz_t]# git checkout master
Switched to branch 'master'
[root@localhost fz_t]# echo worlds >> hello
[root@localhost fz_t]# echo 'Yay!' >> hello
[root@localhost fz_t]# git commit -am "All worlds"
[master 8f316ee] All worlds
1 files changed, 2 insertions(+), 0 deletions(-)
[root@localhost fz_t]# git merge alt
Auto-merging hello
CONFLICT (content): Merge conflict in hello
Automatic merge failed; fix conflicts and then commit the result. 定位冲突文件
[root@localhost fz_t]# git status
# On branch master
# Unmerged paths:
# (use "git add/rm <file>..." as appropriate to mark resolution)
#
# both modified: hello
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@localhost fz_t]# git ls-files -u
100644 ce013625030ba8dba906f756967f9e9ca394464a 1 hello
100644 e63164d9518b1e6caf28f455ac86c8246f78ab70 2 hello
100644 562080a4c6518e1bf67a9f58a32a67bff72d4f00 3 hello 检查冲突
[root@localhost fz_t]# git diff HEAD
diff --git a/hello b/hello
index e63164d..1f2f61c 100644
--- a/hello
+++ b/hello
@@ -1,3 +1,7 @@
hello
+<<<<<<< HEAD
worlds
+=======
+world
+>>>>>>> alt
Yay!
[root@localhost fz_t]# git diff MERGE_HEAD
diff --git a/hello b/hello
index 562080a..1f2f61c 100644
--- a/hello
+++ b/hello
@@ -1,3 +1,7 @@
hello
+<<<<<<< HEAD
+worlds
+=======
world
+>>>>>>> alt
Yay! 或使用以下命令检查
[root@localhost fz_t]# git log --merge --left-right -p hello
commit <8f316ee211109affd61eb89c001ac139f7274406
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:28:35 2018 +0800 All worlds diff --git a/hello b/hello
index ce01362..e63164d 100644
--- a/hello
+++ b/hello
@@ -1 +1,3 @@
hello
+worlds
+Yay! commit >ea3c2044aae6b375d3cba2fa0ac9ff1d78dd6779
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:27:53 2018 +0800 One world diff --git a/hello b/hello
index ce01362..562080a 100644
--- a/hello
+++ b/hello
@@ -1 +1,3 @@
hello
+world
+Yay! 不使用任何版本,继续修改file
[root@localhost fz_t]# cat hello
hello
worldly ones
Yay!
[root@localhost fz_t]# git diff
diff --cc hello
index e63164d,562080a..0000000
--- a/hello
+++ b/hello
@@@ -1,3 -1,3 +1,3 @@@
hello
- worlds
-world
++worldly ones
Yay! 结束解决冲突
[root@localhost fz_t]# git add hello
[root@localhost fz_t]# git ls-files -s
100644 01e406a245782525e6b02cadf6df25e212563967 0 hello
[root@localhost fz_t]# cat .git/MERGE_MSG
Merge branch 'alt' Conflicts:
hello
[root@localhost fz_t]# git commit
[master af5cf67] Merge branch 'alt'
[root@localhost fz_t]# git show
commit af5cf675e311952e5377e5942dd1d7689427eec8
Merge: 8f316ee ea3c204
Author: tong <tongxiaoda@anzhi.com>
Date: Tue Feb 27 16:38:29 2018 +0800 Merge branch 'alt' Conflicts:
hello diff --cc hello
index e63164d,562080a..01e406a
--- a/hello
+++ b/hello
@@@ -1,3 -1,3 +1,3 @@@
hello
- worlds
-world
++worldly ones
Yay!

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

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

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. SDL 威胁建模工具入门 threat modeling tool

    http://msdn.microsoft.com/zh-cn/magazine/dd347831.aspx threat modeling tool 威胁建模工具 minifuzz 文件模糊工具 c ...

  2. ios iphone、ipad启动画面尺寸

    以下是iphone.ipad启动画面的尺寸 iphone4(纵):320 x 480 iphone4 Retina(纵):640 x 960   iphone5(纵):320 x 568 iphone ...

  3. Maven开发系统

    Maven的优点: 自动从互联网中获取jar包,并实现了一步构建. pom.xml的配置 依赖管理(导入对应的jar包) 通过坐标(定位到仓库中的包的位置,并将jar包导入到项目中,如果版本升级,只需 ...

  4. 160401、关于cronExpression的介绍

    关于cronExpression的介绍:   每一个字段都有一套可以指定有效值,如 Seconds (秒):可以用数字0-59 表示, Minutes(分)          :可以用数字0-59 表 ...

  5. CentOS开启telnet连接

    开启telnet连接通道 yum安装telnet yum -y install telnet-server* 关闭防火墙 /etc/init.d/iptables stop 编辑配置文件 vim /e ...

  6. vim学习选取多行(转)

    在可视化模式下,可以对一个文本块的整体进行操作.例如,首先高亮选中一部分文本,然后用d命令删除这个文本块.可视化模式的好处在于,你可以在做改动之前,就看到操作将影响的文本.可视化模式可以分为以下三种: ...

  7. d3.js 之SVG:矢量化图形绘制

    SVG Scalable Vector Graphics 是一个成熟的W3C标准,被设计用来在web和移动平台 上展示可交互的图形.和HTML类似,SVG也支持CSS和JavaScript.尽管可以使 ...

  8. kvm_read_guest*函数分析

    2017-06-30 在KVM中基于其搞特权及,可以透明的读写客户机的内存信息,为此KVM提供了一套API,这里姑且称之为kvm_read_guest_virt*/kvm_write_guest_vi ...

  9. Data striping

    条带化是把连续的数据分割成相同大小的数据块,把每段数据分别写入到阵列中的不同磁盘上的方法. 当多个进程同时访问一个磁盘时,可能会出现磁盘冲突.大多数磁盘系统都对访问次数(每秒的 I/O 操作,IOPS ...

  10. iOS版微信6.5.21发布 适配iPhone X

    昨日,iOS版微信迎来v6.5.21正式版发布,本次升级主要适配iPhone X,在聊天中查找聊天内容时,可以查找交易消息.可以给聊天中的消息设置日期提醒.上一个正式版v6.5.16发布于9月13日, ...