码迷,mamicode.com
首页 > 系统相关 > 详细

Linux------Git-3

时间:2016-09-20 12:00:47      阅读:237      评论:0      收藏:0      [点我收藏+]

标签:

3. Git中的分支

3.1 commit提交时Git元数据的结构

下图显示了Git在commit时,所保存的内容,5个blob对象,并且每个对象之间的关系,其中三个文件,一个tree,还有一个commit对象。

技术分享

下图显示了当修改了文件,并提交后,每次提交与上一次提交之间的关系。

技术分享

 

3.2 Git中分支的概念

技术分享

技术分享

可以看出分支实际上和master一样,都是一个指针,并且指向了当前的commit对象。

3.3 如何区分你当前在哪个分支上?

技术分享

HEAD指针是一个隐含的指针,它指向了你现在所在的分支是哪一个分支。

切换分支:

$ git checkout testing

技术分享

3.4 在分支上进行commit

技术分享

当在分支上进行了commit,那么HEAD指针和分支一起向前移动,并且指向新的commit对象。

3.5 不同流向的分支

技术分享

当不同分支进行操作后,就会产生不同的分支流。

3.6 分支的新建与合并

背景应用环境:

1. 开发某个网站。

2. 为实现某个新的需求,创建一个分支。

3. 在这个分支上开展工作。

假设此时,你突然接到一个电话说有个很严重的问题需要紧急修补,那么可以按照下面的方式处理:

1. 返回到原先已经发布到生产服务器上的分支。

2. 为这次紧急修补建立一个新分支,并在其中修复问题。

3. 通过测试后,回到生产服务器所在的分支,将修补分支合并进来,然后再推送到生产服务器上。

4. 切换到之前实现新需求的分支,继续工作。

技术分享

技术分享

3.7 合并操作实例

//创建一个项目工作目录
[root@taofu git]# mkdir projectOne
[root@taofu git]# cd projectOne/
[root@taofu projectOne]# git init
Initialized empty Git repository in /root/git/projectOne/.git/
[root@taofu projectOne]# git status
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
[root@taofu projectOne]# touch master_file1.txt
[root@taofu projectOne]# touch master_file2.txt
[root@taofu projectOne]# touch master_file3.txt
[root@taofu projectOne]# touch module1_file1.txt
[root@taofu projectOne]# touch module1_file2.txt
[root@taofu projectOne]# touch module1_file3.txt
[root@taofu projectOne]# git add .
[root@taofu projectOne]# git commit -m "initial project"
[master (root-commit) 475feb1] initial project
 0 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 master_file1.txt
 create mode 100644 master_file2.txt
 create mode 100644 master_file3.txt
 create mode 100644 module1_file1.txt
 create mode 100644 module1_file2.txt
 create mode 100644 module1_file3.txt

//创建两个分支module1和module2
[root@taofu projectOne]# git branch module1
[root@taofu projectOne]# git checkout module1
Switched to branch module1
//创建分支的同时并切换到对应分支
[root@taofu projectOne]# git checkout -b module2
Switched to a new branch module2
[root@taofu projectOne]# git branch
  master
  module1
* module2
[root@taofu projectOne]# git checkout module1
Switched to branch module1
[root@taofu projectOne]# git branch
  master
* module1
  module2 

//在module1分支上修改文件
[root@taofu projectOne]# vi module1_file1.txt 
[root@taofu projectOne]# git commit -m "module1 v1.0"
# On branch module1
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#    modified:   module1_file1.txt
#
no changes added to commit (use "git add" and/or "git commit -a")
[root@taofu projectOne]# git commit -a -m "module1 v1.0"
[module1 8541dfb] module1 v1.0
 1 files changed, 4 insertions(+), 0 deletions(-)
[root@taofu projectOne]# git status
# On branch module1
nothing to commit (working directory clean)
[root@taofu projectOne]# git branch
  master
* module1
  module2
[root@taofu projectOne]# git checkout master
Switched to branch master
[root@taofu projectOne]# git status
# On branch master
nothing to commit (working directory clean)

//通过log可以看出master和module1的指针有所不同
[root@taofu projectOne]# git checkout module1
[root@taofu projectOne]# git log --pretty=oneline
8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0
475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project
[root@taofu projectOne]# git checkout master
Switched to branch master
[root@taofu projectOne]# git log --pretty=oneline
475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project
[root@taofu projectOne]# git checkout module1
Switched to branch module1
[root@taofu projectOne]# git log --pretty=oneline
8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0
475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project
[root@taofu projectOne]# vi module1_file2.txt 
[root@taofu projectOne]# git commit -a -m "file2 done...."
[module1 44a5266] file2 done....
 1 files changed, 5 insertions(+), 0 deletions(-) 
[root@taofu projectOne]# git log --pretty=oneline
44a526677cdb3b6a95b8f47e35dc575e350c2f73 file2 done....
8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0
475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project 
[root@taofu projectOne]# git branch
  master
* module1
  module2
[root@taofu projectOne]# git checkout master 
[root@taofu projectOne]# git log --pretty=oneline
475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project

//合并分支,这里有一个Fast-forward,说明合并是沿着一条线向下的。
[root@taofu projectOne]# git merge module1
Updating 475feb1..44a5266
Fast-forward
 module1_file1.txt |    4 ++++
 module1_file2.txt |    5 +++++
 2 files changed, 9 insertions(+), 0 deletions(-)
[root@taofu projectOne]# cat module1_file2.txt 




module2_file_.....done...
[root@taofu projectOne]# git log --pretty=oneline
44a526677cdb3b6a95b8f47e35dc575e350c2f73 file2 done....
8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0
475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project
[root@taofu projectOne]# git branch
* master
  module1
  module2

3.8 删除分支

$ git branch -d hotfix

3.9 非线性合并

技术分享

 

上图中,master已经进行了一次更新,而分支iss53已经从C2处发展到C5,此时如果master进行合并,就需要C2 C4 C5的三方合并,合并后的结果如下图所示,这时就可以删除iss53的分支了。

技术分享

 

3.10 当合并的时候产生冲突

这时通过git status可以看到unmerged状态的文件,去这些文件中,手工解决冲突,再commit。

Linux------Git-3

标签:

原文地址:http://www.cnblogs.com/stevenotes/p/5887790.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!