频道栏目
首页 > 资讯 > 其他 > 正文

GIT:git代码检出与日常维护

16-08-01        来源:[db:作者]  
收藏   我要投稿

git的代码“检出”

其他版本库管理系统(例如SVN)一般都会使用checkout来直接检出服务端的版本库(即开发代码)到本地工作目录,而git checkout却不能实现这一功能,原因是:

If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is “clone” and not “checkout”. This is an important distinction – instead of getting just a working copy, Git receives a full copy of nearly(服务器会存在部分hooks或者其他文件,详情参考Git Internals) all data that the server has. Every version of every file for the history of the project is pulled down by default when you run git clone.

上文提到了git采用分布式无中心化设计,因此检出代码的同时需要将完整的仓库下载到本地。所以简单的checkout是不能完成这项任务的,在git中应该使用git clone将远程仓库检出到本地。

1. git clone的整体流程如下:

Created with Rapha?l 2.1.0locallocalremoteremotegit cloneinit .git dirpull all in .gitadd remote urlscheckout latest working copyjust do anything by yourself

从上图可以看出git clone也是一个复合高级指令,内部会执行git init(在本地初始化git repository,即创建.git隐藏目录)、git pull(将远程仓库的几乎所有拷贝到本地)、git remote add(将远程仓库的URL连接添加到记录),以及git checkout(将默认的仓库的master分支代码拷贝到本地工作目录working directory和缓冲区index)。
既然提到了其他的指令,这里一并介绍一下git pull、git checkout。

2. git pull的整体流程如下:

Created with Rapha?l 2.1.0locallocalremoteremotegit pullsub-command:git fetchsub-command:git mergejust do anything by yourself

【备注】:这里要注意git pull与git fetch的区别,由上图可以看出git pull是git fetch和git merge的复合指令,由此可以得出git fetch只负责将文件检出到本地,但并不会与当前工作区的代码进行合并。官方说明如下:

It’s important to note that the git fetch command only downloads the data to your local repository – it doesn’t automatically merge it with any of your work or modify what you’re currently working on.
【zssure】:即git fetch从服务器取回本地没有的文件,但不会自动merge本地working copy。不带参数的git fetch会取回所有的分支。

3. git checkout的整体流程如下:

Created with Rapha?l 2.1.0locallocalremoteremotegit checkoutchecking out fileschecking out commitschecking out all branchesdo anything by yourself

 

git的checkout

1. checkout的底层机制

通过上面两个章节“git的日常操作流workflow”“git的代码‘检出’”,我们大致了解了如何从仓库中将git所管理的各种文件拉取到本地,以及本地文件可能所处的各种状态。接下来就说一下日常最容易碰到的问题,诸如简单的git add、git commit和git reset就不赘述了,这几个指令日常使用最频繁就是将本地修改添加到git的仓库中进行管理和保存。这里着重介绍的是checkout,尤其是当需要同时管理多个分支,在多个分支之间切换时;甚至是当需要与同一个源的其他仓库进行合并时(即我们通常所说的多人协同开发)。

git checkout有多重指令格式,例如:

git checkout [-q] [-f] [-m] []
git checkout [-q] [-f] [-m] --detach []
git checkout [-q] [-f] [-m] [--detach] 
git checkout [-q] [-f] [-m] [[-b|-B|--orphan] ] []
git checkout [-f|--ours|--theirs|-m|--conflict=
相关TAG标签
上一篇:马云对话张瑞敏:要在阳光灿烂的日子修屋顶
下一篇:JAVA 多线程--线程同步安全
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站