git实战-分支管理与合并

显示所有分支

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git branch

* master

增加分支

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git branch b1

切换分支

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git checkout b1

Switched to branch 'b1'

 

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$

切换到不同的分支对 helloworld.naxsu 这个文件进行修改并提交,并查看他们的内容

原先 helloworld.naxsu 的内容是

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$ cat helloworld.naxsu

hello world

add something

b1 分支修改并提交,然后查看文件内容

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$ vim helloworld.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$ git commit -a -m "b1 update helloworld.naxsu"

warning: LF will be replaced by CRLF in helloworld.naxsu.

The file will have its original line endings in your working

[b1 warning: LF will be replaced by CRLF in helloworld.naxsu

The file will have its original line endings in your working

b9c5de1] b1 update helloworld.naxsu

warning: LF will be replaced by CRLF in helloworld.naxsu.

The file will have its original line endings in your working

 1 file changed, 1 insertion(+)

 

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$ git status

# On branch b1

nothing to commit (working directory clean)

 

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$ cat helloworld.naxsu

hello world

add something

b1 branch add something

切换到 master 分支,进行和上面一样的操作

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$ git checkout master

Switched to branch 'master'

Your branch is ahead of 'origin/master' by 7 commits.

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ vim helloworld.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git commit -a -m "master update helloworld.naxsu"

[master d9f15c9] master update helloworld.naxsu

 1 file changed, 1 insertion(+)

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ cat helloworld.naxsu

hello world

add something

master branch add something

从中可以看出两个分支是没有影响的。

##########NextPage##########

列出各分支之间的信息

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git show-branch

! [b1] b1 update helloworld.naxsu

 * [master] master update helloworld.naxsu

--

 * [master] master update helloworld.naxsu

+  [b1] b1 update helloworld.naxsu

+* [master^] delete test.naxsu

比较 masterb1 这两个分支文件的不同

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git diff master b1

diff --git a/helloworld.naxsu b/helloworld.naxsu

index 9e73a56..7929722 100644

--- a/helloworld.naxsu

+++ b/helloworld.naxsu

@@ -1,3 +1,3 @@

 hello world

 add something

-master branch add something

+b1 branch add something

分支合并 merge

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git merge "merge" HEAD b1

Auto-merging helloworld.naxsu

CONFLICT (content): Merge conflict in helloworld.naxsu

Automatic merge failed; fix conflicts and then commit the result.

显示自动合并失败,我们看一下状态,可以看到 helloworld.naxsu 被两个分支修改过了

huangyineng@HUANGYINENG-PC ~/hello-world (master|MERGING)

$ git status

# On branch master

# Your branch is ahead of 'origin/master' by 8 commits.

#

# Unmerged paths:

#   (use "git add/rm ..." as appropriate to mark resolution)

#

#       both modified:      helloworld.naxsu

#

no changes added to commit (use "git add" and/or "git commit -a")

我们来看一下 helloworld.naxsu 的内容

huangyineng@HUANGYINENG-PC ~/hello-world (master|MERGING)

$ cat helloworld.naxsu

hello world

add something

<<<<<<< HEAD

master branch add something

=======

b1 branch add something

>>>>>>> b1

把们用 vim 编辑工具人为的把冲突去掉,然后保存、提交,切换到其他分支,也可以看到没有冲突了

huangyineng@HUANGYINENG-PC ~/hello-world (master|MERGING)

$ vim helloworld.naxsu

 

huangyineng@HUANGYINENG-PC ~/hello-world (master|MERGING)

$ git commit -a -m "merging"

[master 34908f4] merging

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ cat helloworld.naxsu

hello world

add something

master branch add something

b1 branch add something

 

huangyineng@HUANGYINENG-PC ~/hello-world (master)

$ git checkout b1

Switched to branch 'b1'

 

huangyineng@HUANGYINENG-PC ~/hello-world (b1)

$ cat helloworld.naxsu

hello world

add something

b1 branch add something

另一种合并的做法,自行去查帮助文档:

$ git checkout master

$ git pull . b1

本文链接: http://www.656463.com/portal.php?mod=view&aid=7 2 ,转载请注明出处

下一节: 标签

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值