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 合并操作实例
//创建一个项目工作目录 [[email protected] git]# mkdir projectOne [[email protected] git]# cd projectOne/ [[email protected] projectOne]# git init Initialized empty Git repository in /root/git/projectOne/.git/ [[email protected] projectOne]# git status # On branch master # # Initial commit # nothing to commit (create/copy files and use "git add" to track) [[email protected] projectOne]# touch master_file1.txt [[email protected] projectOne]# touch master_file2.txt [[email protected] projectOne]# touch master_file3.txt [[email protected] projectOne]# touch module1_file1.txt [[email protected] projectOne]# touch module1_file2.txt [[email protected] projectOne]# touch module1_file3.txt [[email protected] projectOne]# git add . [[email protected] 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 [[email protected] projectOne]# git branch module1 [[email protected] projectOne]# git checkout module1 Switched to branch ‘module1‘ //创建分支的同时并切换到对应分支 [[email protected] projectOne]# git checkout -b module2 Switched to a new branch ‘module2‘ [[email protected] projectOne]# git branch master module1 * module2 [[email protected] projectOne]# git checkout module1 Switched to branch ‘module1‘ [[email protected] projectOne]# git branch master * module1 module2 //在module1分支上修改文件 [[email protected] projectOne]# vi module1_file1.txt [[email protected] 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") [[email protected] projectOne]# git commit -a -m "module1 v1.0" [module1 8541dfb] module1 v1.0 1 files changed, 4 insertions(+), 0 deletions(-) [[email protected] projectOne]# git status # On branch module1 nothing to commit (working directory clean) [[email protected] projectOne]# git branch master * module1 module2 [[email protected] projectOne]# git checkout master Switched to branch ‘master‘ [[email protected] projectOne]# git status # On branch master nothing to commit (working directory clean) //通过log可以看出master和module1的指针有所不同 [[email protected] projectOne]# git checkout module1 [[email protected] projectOne]# git log --pretty=oneline 8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0 475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project [[email protected] projectOne]# git checkout master Switched to branch ‘master‘ [[email protected] projectOne]# git log --pretty=oneline 475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project [[email protected] projectOne]# git checkout module1 Switched to branch ‘module1‘ [[email protected] projectOne]# git log --pretty=oneline 8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0 475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project [[email protected] projectOne]# vi module1_file2.txt [[email protected] projectOne]# git commit -a -m "file2 done...." [module1 44a5266] file2 done.... 1 files changed, 5 insertions(+), 0 deletions(-) [[email protected] projectOne]# git log --pretty=oneline 44a526677cdb3b6a95b8f47e35dc575e350c2f73 file2 done.... 8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0 475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project [[email protected] projectOne]# git branch master * module1 module2 [[email protected] projectOne]# git checkout master [[email protected] projectOne]# git log --pretty=oneline 475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project //合并分支,这里有一个Fast-forward,说明合并是沿着一条线向下的。 [[email protected] projectOne]# git merge module1 Updating 475feb1..44a5266 Fast-forward module1_file1.txt | 4 ++++ module1_file2.txt | 5 +++++ 2 files changed, 9 insertions(+), 0 deletions(-) [[email protected] projectOne]# cat module1_file2.txt module2_file_.....done... [[email protected] projectOne]# git log --pretty=oneline 44a526677cdb3b6a95b8f47e35dc575e350c2f73 file2 done.... 8541dfb34bc67a1d37076b0154de94f83b73d69a module1 v1.0 475feb1c57e9ea1e679e68ed7ef9ab2491f64308 initial project [[email protected] 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。
时间: 2024-09-30 14:36:15