标签:
下图显示了Git在commit时,所保存的内容,5个blob对象,并且每个对象之间的关系,其中三个文件,一个tree,还有一个commit对象。
下图显示了当修改了文件,并提交后,每次提交与上一次提交之间的关系。
可以看出分支实际上和master一样,都是一个指针,并且指向了当前的commit对象。
HEAD指针是一个隐含的指针,它指向了你现在所在的分支是哪一个分支。
切换分支:
$ git checkout testing
当在分支上进行了commit,那么HEAD指针和分支一起向前移动,并且指向新的commit对象。
当不同分支进行操作后,就会产生不同的分支流。
背景应用环境:
1. 开发某个网站。
2. 为实现某个新的需求,创建一个分支。
3. 在这个分支上开展工作。
假设此时,你突然接到一个电话说有个很严重的问题需要紧急修补,那么可以按照下面的方式处理:
1. 返回到原先已经发布到生产服务器上的分支。
2. 为这次紧急修补建立一个新分支,并在其中修复问题。
3. 通过测试后,回到生产服务器所在的分支,将修补分支合并进来,然后再推送到生产服务器上。
4. 切换到之前实现新需求的分支,继续工作。
//创建一个项目工作目录 [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
$ git branch -d hotfix
上图中,master已经进行了一次更新,而分支iss53已经从C2处发展到C5,此时如果master进行合并,就需要C2 C4 C5的三方合并,合并后的结果如下图所示,这时就可以删除iss53的分支了。
这时通过git status可以看到unmerged状态的文件,去这些文件中,手工解决冲突,再commit。
标签:
原文地址:http://www.cnblogs.com/stevenotes/p/5887790.html