版本管理之gitlab实践教程:基础篇(2)
2017-07-04 22:53
253 查看
这篇文章将会重点介绍一件事情: rebase。
rebase用来做什么
git的rebase是非常常用的一个命令。了解它是做什么之前先看一下一个很熟悉的场景:我们有两个分支:master和feature分支,feature分支不是那种随时”用后即焚”的分支,它会存续一定时间。因为master同时也会在更新,所以feature需要经常和master的内容进行同步,为了达到这个目的,可以merge一下就OK了,但是git有一种更好的方法来解决这个问题,这种方法就是rebase。而这种操作的一个核心描述则是replay,既然是replay,则不会产生新的comment。
下面通过具体的例子来了解一下这个问题。
当前状态
只有一个master分支,其中只有一个README.md文件[root@ku8-1 ~]# git clone git@172.17.0.2:root/demoprj.git Cloning into 'demoprj'... remote: Counting objects: 7, done. remote: Compressing objects: 100% (3/3), done. remote: Total 7 (delta 1), reused 0 (delta 0) Receiving objects: 100% (7/7), done. Resolving deltas: 100% (1/1), done. [root@ku8-1 ~]# cd demoprj [root@ku8-1 demoprj]# git branch * master [root@ku8-1 demoprj]#
创建一个名为fea-restapi的分支
[root@ku8-1 demoprj]# git checkout -b fea-restapi Switched to a new branch 'fea-restapi' [root@ku8-1 demoprj]# git branch * fea-restapi master [root@ku8-1 demoprj]#
创建一个文件并提交
[root@ku8-1 demoprj]# echo "feature : restapi list" > restapi-list [root@ku8-1 demoprj]# git add restapi-list [root@ku8-1 demoprj]# git commit -m "create rest api list" [fea-restapi 555c090] create rest api list 1 file changed, 1 insertion(+) create mode 100644 restapi-list [root@ku8-1 demoprj]#
切换至master添加新文件并提交
[root@ku8-1 demoprj]# git checkout master Switched to branch 'master' [root@ku8-1 demoprj]# echo "new files in master" >newfile [root@ku8-1 demoprj]# git add newfile [root@ku8-1 demoprj]# git commit -m "New file added in Master" [master c2ce58b] New file added in Master 1 file changed, 1 insertion(+) create mode 100644 newfile [root@ku8-1 demoprj]#
在master里再添加一个文件
[root@ku8-1 demoprj]# echo "another new file in master" >anotherfile [root@ku8-1 demoprj]# git add anotherfile [root@ku8-1 demoprj]# git commit -m "Another new files in Master" [master ebb41c7] Another new files in Master 1 file changed, 1 insertion(+) create mode 100644 anotherfile [root@ku8-1 demoprj]#
切换到fea-restapi分支再次添加一个文件
[root@ku8-1 demoprj]# git checkout fea-restapi Switched to branch 'fea-restapi' [root@ku8-1 demoprj]# echo "another new feature: restapi list2" >restapi-list-2 [root@ku8-1 demoprj]# git add restapi-list-2 [root@ku8-1 demoprj]# git commit -m "create another rest api list" [fea-restapi 9c091eb] create another rest api list 1 file changed, 1 insertion(+) create mode 100644 restapi-list-2 [root@ku8-1 demoprj]#
执行rebase操作
执行rebase操作,很明显地可以看出这个场景就是用于同步fea-restapi分支和master分支,而方式就是在此分支上”replay”master分支所做的一切(Applying的内容)[root@ku8-1 demoprj]# git branch * fea-restapi master [root@ku8-1 demoprj]# git rebase master First, rewinding head to replay your work on top of it... Applying: create rest api list Applying: create another rest api list [root@ku8-1 demoprj]# [root@ku8-1 demoprj]# ls anotherfile newfile README.md restapi-list restapi-list-2 [root@ku8-1 demoprj]#
执行push操作
执行push操作,有时根据具体情况可能会需要-f选项,强制执行,使用-f时需要多加小心提示内容。[root@ku8-1 demoprj]# git push origin fea-restapi Counting objects: 13, done. Compressing objects: 100% (8/8), done. Writing objects: 100% (12/12), 1.08 KiB | 0 bytes/s, done. Total 12 (delta 2), reused 0 (delta 0) remote: remote: Create merge request for fea-restapi: remote: http://e5091e18d568/root/demoprj/merge_requests/new?merge_request%5Bsource_branch%5D=fea-restapi remote: To git@172.17.0.2:root/demoprj.git * [new branch] fea-restapi -> fea-restapi [root@ku8-1 demoprj]#
结果确认
在gitlab上可以看到rebase之后的结果正如我们所期待的那样已经成功同步.总结
这篇文章介绍了非常常用的rebase操作, rebase是git非常方便的同步机制,合理使用能够带来很好的效率.相关文章推荐
- 版本管理之gitlab实践教程:基础篇(6)
- 版本管理之gitlab实践教程:基础篇(1)
- 版本管理之gitlab实践教程:基础篇(8)
- 版本管理之gitlab实践教程:基础篇(4)
- 版本管理之gitlab实践教程:基础篇(7)
- 版本管理之gitlab实践教程:基础篇(3)
- 版本管理之gitlab实践教程:基础篇(5)
- 版本管理之gitlab实践教程:基础篇(12)
- 版本管理之gitlab实践教程:进阶篇(2)
- 版本管理之gitlab实践教程:进阶篇(1)
- 版本管理之gitlab实践教程:基础篇(11)
- 版本管理之gitlab实践教程:基础篇(10)
- 版本管理之gitlab实践教程:基础篇(9)
- 版本管理之gitlab实践教程:基础篇(13)
- 持续集成之jenkins实践教程:基础篇(2): 集成gitlab
- 使用GitLab版本管理工具教程
- Maven最佳实践:版本管理
- 项目管理实践教程一、工欲善其事,必先利其器【Basic Tools】
- 项目管理实践教程二、源代码控制【Source Control Using VisualSVN Server and TortoiseSVN】
- Photoshop CS4基础实践入门高清视频教程