Git系列之四管理分支结构

1.管理分支结构

分支就是科幻电影里面的平行宇宙,当你正在电脑前努力学习Git的时候,另一个你正在另一个平行宇宙里努力学习SVN。

如果两个平行宇宙互不干扰,那对现在的你也没啥影响。不过,在某个时间点,两个平行宇宙合并了,结果,你既学会了Git又学会了SVN!

分支在实际中有什么用呢?假设你准备开发一个新功能,但是需要两周才能完成,第一周你写了50%的代码,如果立刻提交,由于代码还没写完,不完整的代码库会导致别人不能干活了。如果等代码全部写完再一次提交,又存在丢失每天进度的巨大风险。

现在有了分支,就不用怕了。你创建了一个属于你自己的分支,别人看不到,还继续在原来的分支上正常工作,而你在自己的分支上干活,想提交就提交,直到开发完毕后,再一次性合并到原来的分支上,这样,既安全,又不影响别人工作。

其他版本控制系统如SVN等都有分支管理,但是用过之后你会发现,这些版本控制系统创建和切换分支比蜗牛还慢,简直让人无法忍受,结果分支功能成了摆设,大家都不去用。

但Git的分支是与众不同的,无论创建、切换和删除分支,Git在1秒钟之内就能完成!无论你的版本库是1个文件还是1万个文件。

假设你在为某个手机系统研发拍照功能,代码已经完成了80%,但如果将这不完整的代码直接提交到git仓库中,又有可能影响到其他人的工作,此时我们便可以在该软件的项目之上创建一个名叫“拍照功能”的分支,这种分支只会属于你自己,而其他人看不到,等代码编写完成后再与原来的项目主分支合并下即可,这样即能保证代码不丢失,又不影响其他人的工作。

一般在实际的项目开发中,我们要尽量保证master分支是非常稳定的,仅用于发布新版本,平时不要随便直接修改里面的数据文件,而工作的时候则可以新建不同的工作分支,等到工作完成后在合并到master分支上面。

git会将每次的提交操作串成一个时间线,而在前面的实验中实际都是在对master分支进行操作,Git会在创建分支后默认创建一个叫做Photograph的指针,所以我们还需要再将HEAD指针切换到“Photograph”的位置才正式使用上了新分支

1.1 创建分支

1.首先,我们创建linuxprobe分支,然后切换到linuxprobe分支:

[root@test linuxprobe]# git checkout -b linuxprobe
Switched to a new branch 'linuxprobe'

git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:

[root@test linuxprobe]# git branch linuxprobe 
[root@test linuxprobe]# git checkout linuxprobe 
Switched to branch 'linuxprobe'

然后,用git branch命令查看当前分支:

[root@test linuxprobe]# git branch 
* linuxprobe
 master

git branch命令会列出所有分支,当前分支前面会标一个*号。

2.我们对文件再追加一行字符串

[root@test linuxprobe]# echo "Creating a new branch is quick." >> readme.txt

3.将文件提交到git仓库

[root@test linuxprobe]# git add readme.txt
[root@test linuxprobe]# git commit -m "new branch"
[linuxprobe 318ba35] new branch
 1 file changed, 1 insertion(+)

为了更好理解分支的作用,我们在提交文件后再切换回master分支

[root@test linuxprobe]# git checkout master 
Switched to branch 'master'

然后查看下文件内容,发现并没有新追加的字符串

[root@test linuxprobe]# cat readme.txt 
Initialization Git repository
Git is a distributed version control system.
Git is free software.
Git is a distributed version control system.
Something not important
Modified again

2.合并分支

2.1使用”git merge“命令

现在,我们想把linuxprobe的工作成果合并到master分支上了,则可以使用”git merge"命令来将指定的的分支与当前分支合并:

1.切换到linuxprobe分支上

[root@test linuxprobe]# git merge linuxprobe 
Updating 245824d..318ba35
Fast-forward
 readme.txt | 1 +
 1 file changed, 1 insertion(+)

2.查看合并后的readme.txt文件

[root@test linuxprobe]# cat readme.txt 
Initialization Git repository
Git is a distributed version control system.
Git is free software.
Git is a distributed version control system.
Something not important
Modified again
Creating a new branch is quick.

3.删除分支并查看

确认合并完成后,就可以放心地删除linuxprobe分支了

[root@test linuxprobe]# git branch -d linuxprobe 
Deleted branch linuxprobe (was 318ba35).
[root@test linuxprobe]# git branch 
* master

3.解决内容冲突

Git并不能每次都为我们自动的合并分支,当遇到了内容冲突比较复杂的情况,则必须手工将差异内容处理掉,比如这样的情况:

1.创建分支并切换到该分支命令:git checkout -b 分支名称

[root@test linuxprobe]# git checkout -b linuxprobe
Switched to a new branch 'linuxprobe'

2.修改文件内容

[root@test linuxprobe]# vim readme.txt
Initialization Git repository
Git is a distributed version control system.
Git is free software.
Git is a distributed version control system.
Something not important
Modified again
Creating a new branch is quick.
Creating a new branch is quick & simple.

3.在linuxprobe分支上提交

[root@test linuxprobe]# git add readme.txt
[root@test linuxprobe]# git commit -m "Creating a new branch is quick & simple."
[linuxprobe 201e1a9] Creating a new branch is quick & simple.
 1 file changed, 1 insertion(+)

4.切换到master分支

[root@test linuxprobe]# git checkout master 
Switched to branch 'master'

5.在在master分支上修改readme.txt文件同一行的内容

[root@test linuxprobe]# vim readme.txt
Initialization Git repository
Git is a distributed version control system.
Git is free software.
Git is a distributed version control system.
Something not important
Modified again
Creating a new branch is quick.
Creating a new branch is quick AND simple.

6.在主分支上提交至Git版本仓库

[root@test linuxprobe]# git add readme.txt
[root@test linuxprobe]# git commit -m "Creating a new branch is quick AND simple. "
[master 1c45a4b] Creating a new branch is quick AND simple.
 1 file changed, 1 insertion(+)

7.合并linuxprobe分支

那么此时,我们在master与linuxprobe分支上都分别对中readme.txt文件进行了修改并提交了,那这种情况下Git就没法再为我们自动的快速合并了,它只能告诉我们readme.txt文件的内容有冲突,需要手工处理冲突的内容后才能继续合并:

[root@test linuxprobe]# git merge linuxprobe 
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

冲突的内容为:

[root@test linuxprobe]# vim readme.txt 
Initialization Git repository
Git is a distributed version control system.
Git is free software.
Git is a distributed version control system.
Something not important
Modified again
Creating a new branch is quick.
<<<<<<< HEAD
Creating a new branch is quick AND simple.
=======
Creating a new branch is quick & simple.
>>>>>>> linuxprobe

Git用<<<<<<<=======>>>>>>>标记出不同分支的内容,我们修改如下后保存:

Initialization Git repository
Git is a distributed version control system.
Git is free software.
Git is a distributed version control system.
Something not important
Modified again
Creating a new branch is quick.
Creating a new branch is quick and simple.

8.修改之后再次提交

[root@test linuxprobe]# git add readme.txt
[root@test linuxprobe]# git commit -m "conflict fixed"
[master f5e555b] conflict fixed

9.查看Git历史提交记录(可以看到分支的变化):

[root@test linuxprobe]# git log --graph --pretty=oneline --abbrev-commit
* f5e555b conflict fixed
|\ 
| * 201e1a9 Creating a new branch is quick & simple.
* | 1c45a4b Creating a new branch is quick AND simple.
|/ 
* 318ba35 new branch
* 245824d changed the file name again
* 1aa44b7 changed name
* 8ebd54f delete git3-4.c
* db2c1ee add secoend the .gitignore file
* 2959951 Modified again
* 245e943 add the .gitignore file
* 855b9cf added a line of words
* 96a36d4 add the readme file

10.最后,放心的删除linuxprobe分支

[root@test linuxprobe]# git branch -d linuxprobe 
Deleted branch linuxprobe (was 201e1a9).
[root@test linuxprobe]# git branch 
* master

4.命令小结

git branch   ##创建分支 列出分支  
git branch -v   ##显示分支详细信息
git branch --merged  ##显示merge的分支
git branch --no-merged  ##显示没有merge的分支
git branch -d    ##删除分支
git checkout     ##切换分支
git merge        ##合并分支
git log          ##显示log
git stash        ###会把所有未提交的修改(包括暂存的和非暂存的)都保存起来,用于后续恢复当前工作目录
git tag          ##打标机
0
如无特殊说明,文章均为本站原创,转载请注明出处

该文章由 发布

这货来去如风,什么鬼都没留下!!!
发表我的评论

Hi,请填写昵称和邮箱!

取消评论
代码 贴图 加粗 链接 删除线 签到