文章目录
Git全解析之远程仓库交互
中央仓库的概念
虽然说git是分布式版本控制工具,但同样有远程仓库的概念。出于各种目的,我们有时需要有一个共享的远程仓库,如GitHub上的仓库,我们公司项目中用来测试和部署的仓库等。
一般的操作步骤是先在公共服务器上创建一个仓库,然后每个开发人员都clone这个仓库进行各自的开发,开发完成后再push到远程仓库进行测试部署。
从远程仓库clone项目:
1234567 |
[[email protected] gitwork]$ [[email protected] gitwork]$ git clone [email protected]:wustrive2008/gittest.gitInitialized empty Git repository in /home/centos/gitwork/gittest/.git/remote: Counting objects: 4, done.remote: Compressing objects: 100% (3/3), done.remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0Receiving objects: 100% (4/4), 4.15 KiB, done. |
上面示例的远程仓库在github上,当然如果我们做的是公司的项目,出于安全性和访问效率的考虑很少会将公司的私有项目放到github上。一般的做法是搭建自己的git服务器,这里推荐两种git服务器工具:
这两个工具网上都有很多教程。
说明:github也有付费的企业版,试用过,也很好用
本地分支与远程分支
平时我们使用 git branch 查看的都是本地分支,如:
12 |
[[email protected] gittest]$ git branch* master |
但是当需要经常与中央仓库同步代码时,特别是分支很多时,需要查看本地当前有哪些远程分支的引用,比便于在不同的分支之间进行切换开发。关于git分支的较好实践可以参考:git flow
查看远程分支的引用
12345 |
[[email protected] gittest]$ [[email protected] gittest]$ git branch -a* master remotes/origin/HEAD -> origin/master remotes/origin/master |
上面的结果表示:
当前本地有一个master分支,并且有一个remotes/origin/master(远程master)分支的引用,至于remotes/origin/HEAD分支可以理解为origin/master分支的一个引用。
其中origin为远程仓库引用的别名,此名称可以修改,也可以有多个,可以参考git remote命令了解更多
接下来在本地创建一个develop分支,用于正常的开发流程:
12345678910 |
[[email protected] gittest]$ git checkout -b developSwitched to a new branch ‘develop‘[[email protected] gittest]$ git br* develop master[[email protected] gittest]$ git br -a* develop master remotes/origin/HEAD -> origin/master remotes/origin/master |
在develop分支上进行开发,并提交:
123456 |
[[email protected] gittest]$ touch file1.txt[[email protected] gittest]$ git add .[[email protected] gittest]$ git commit -am "create file file1.txt"[develop 21053d7] create file file1.txt 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 file1.txt |
开发完成后将develop分支推送到服务器:
1234567 |
[[email protected] gittest]$ git push origin Counting objects: 4, done.Compressing objects: 100% (2/2), done.Writing objects: 100% (3/3), 311 bytes, done.Total 3 (delta 0), reused 0 (delta 0)To [email protected]:wustrive2008/gittest.git 8a9a114..21053d7 develop -> develop |
接下来正常的流程是测试人员拉取到远程的develop分支,然后对develop分支上提交的内容进行测试,测试通过后,合并到master分支,最后推送到部署服务器进行上线部署。
1234567891011 |
[[email protected] gittest]$ git checkout masterSwitched to branch ‘master‘[[email protected]gon gittest]$ git merge developUpdating 8a9a114..21053d7Fast-forward 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 file1.txt[[email protected] gittest]$ git push origin masterTotal 0 (delta 0), reused 0 (delta 0)To [email protected]:wustrive2008/gittest.git 8a9a114..21053d7 master -> master |
pull与fetch
如果要拉取远程仓库的代码,需要用到pull与fetch命令
这两个命令的区别是pull=fetch+merge
先来演示一下git pull,有其他开发者已经在develop分支上提交了新的内容,现在需要同步到本地
1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
[[email protected] gittest]$ git br* develop master[[email protected] gittest]$ git logcommit 21053d768d7af0c5cf90f63dc105891726094b43Author: wubaoguo <[email protected]126.com>Date: Mon Jan 11 22:35:50 2016 +0800 create file file1.txt commit 8a9a114ecbacfd5555ee417ab1dbe02a20db9a03Author: wubaoguo <[email protected]>Date: Mon Jan 11 22:08:39 2016 +0800 Initial commit[[email protected] gittest]$ git pull origin developremote: Counting objects: 2, done.remote: Compressing objects: 100% (2/2), done.remote: Total 2 (delta 1), reused 1 (delta 0), pack-reused 0Unpacking objects: 100% (2/2), done.From github.com:wustrive2008/gittest * branch develop -> FETCH_HEADUpdating 21053d7..2296978Fast-forward 0 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 file2.txt[[email protected] gittest]$ git logcommit 22969782f467cd04410c9ed3cf5c80e3987d212bAuthor: wubaoguo <[email protected]126.com>Date: Mon Jan 11 22:52:18 2016 +0800 create file file2.txt commit 21053d768d7af0c5cf90f63dc105891726094b43Author: wubaoguo <[email protected]126.com>Date: Mon Jan 11 22:35:50 2016 +0800 create file file1.txt commit 8a9a114ecbacfd5555ee417ab1dbe02a20db9a03Author: wubaoguo <[email protected]>Date: Mon Jan 11 22:08:39 2016 +0800 Initial commit |
很清楚的看到,新的代码已经拉取并合并到本地了,可以基于最新的代码进行开发了。
如果开发者B说他向远程仓库推送了新的分支fixbug,需要你在此基础上继续修改bug,可以这样做
1234567891011121314151617181920 |
[[email protected] gittest]$ git br -a #可以看到这时本地并没有新的分支与引用* develop master remotes/origin/HEAD -> origin/master remotes/origin/develop remotes/origin/master[[email protected] gittest]$ git fetch #拉取远程所有的变动remote: Counting objects: 3, done.remote: Compressing objects: 100% (1/1), done.remote: Total 3 (delta 1), reused 3 (delta 1), pack-reused 0Unpacking objects: 100% (3/3), done.From github.com:wustrive2008/gittest 21053d7..2296978 develop -> origin/develop * [new branch] fixbug -> origin/fixbug #注意这里拉取到一个新分支[[email protected] gittest]$ git br #这时本地并没有fixbug分支* develop master[[email protected] gittest]$ git checkout fixbug #创建并切换到fixbug分支,引用origin/fixbug分支Branch fixbug set up to track remote branch fixbug from origin.Switched to a new branch ‘fixbug‘ |
与远程仓库的交互操作基本就这些,还有一点要注意,推送之前最好先拉取一下,因为如果远程分支版本比本地新,直接推送会失败。