Git-搭建Git服务器并使用Git进行协同工作

Git可以在本地的仓库中使用,自己管理版本,但是要团队合作的话就需要一个Git服务器来进行协作,最典型的Git服务器是美国的GitHub、中国的码云等,也可以自己搭建类似GitHub的Git服务器例如免费的Gitlab,但是GitLab需要Nginx或者Apache的支持,搭建比较麻烦,而且对服务器的性能要求也高了不少,以后再用GitLab吧,现在先搭建一个基于控制台的Git服务器。

Git服务器运行条件:Ubuntu16.04、git、ssh等。

1、假设根据上级需求,我们要开发一个密码锁的项目,取名叫lock,在ubuntu服务器上创建一个lock文件夹并使用Git初始化成仓库:

tq@ubuntu:~/lock$ git --bare init
Initialized empty Git repository in /home/tq/lock/
tq@ubuntu:~/lock$ ls
branches  config  description  HEAD  hooks  info  objects  refs

注意使用的是git –bare init而不是git init,详细原因:一般个人使用,用git init,这时候你的工作区也在这里。你要是想建立一个固定的地址让大家一起用,就在服务器上用git –bare init。其实你可以看到,init建立的.git目录内容和–bare建立的目录内容是差不多的。在初始化远程仓库时最好使用 git –bare init 而不要使用:git init。这样在使用hooks的时候,会有用处。如果使用了git init初始化,则远程仓库的目录下,也包含work tree,当本地仓库向远程仓库push时, 如果远程仓库正在push的分支上(如果当时不在push的分支,就没有问题), 那么push后的结果不会反应在work tree上, 也即在远程仓库的目录下对应的文件还是之前的内容,必须得使用git reset –hard才能看到push后的内容。(参考http://blog.haohtml.com/archives/12265

如果使用git init初始化仓库的话,本地仓库向服务器执行push操作的时候如果所push的分支和服务器仓库工作区正处在的仓库是一样的话会报错:

remote: error: refusing to update checked out branch: refs/heads/master  

remote: error: By default, updating the current branch in a non-bare repository  

remote: error: is denied, because it will make the index and work tree inconsistent  

remote: error: with what you pushed, and will require 'git reset --hard' to match  

remote: error: the work tree to HEAD.

......

如果将远程仓库的分支切换到其他分支,然后本地分支在执行push操作,这时候服务器仓库工作区的分支和push操作的分支不一样push操作就能正常执行了,但是这样很不方便,所以还是不要用git init来初始化服务区上的仓库了,虽然用git –bare init初始化的仓库没有工作区,但是不妨碍服务器作为协同多人工作的任务,我们要工作在自己本地仓库的工作区工作就行了。

2、现在在服务器上创建了一个空的仓库,什么都没有,我们可以在本地创建一个非空的仓库然后上传到服务器仓库:

tangquan@yalishiquede MINGW32 /i/me
$ git init
Initialized empty Git repository in I:/me/.git/

tangquan@yalishiquede MINGW32 /i/me (master)
$ vi a

tangquan@yalishiquede MINGW32 /i/me (master)
$ git add a
warning: LF will be replaced by CRLF in a.
The file will have its original line endings in your working directory.

tangquan@yalishiquede MINGW32 /i/me (master)
$ git commit -m "add a"
[master (root-commit) 7ffebb8] add a
warning: LF will be replaced by CRLF in a.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 a

tangquan@yalishiquede MINGW32 /i/me (master)
$ git log
commit 7ffebb82156da03dc17c486c28308799fa7479a0
Author: tangquan <384998430@qq.com>
Date:   Tue Sep 19 10:43:17 2017 +0800

    add a

tangquan@yalishiquede MINGW32 /i/me (master)
$ git remote add origin tq@192.168.1.143:/home/tq/lock

tangquan@yalishiquede MINGW32 /i/me (master)
$ git push origin  master
tq@192.168.1.143's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 202 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To tq@192.168.1.143:/home/tq/lock
 * [new branch]      master -> master

这个本地仓库是在Windows下创建的,使用命令:git remote add origin tq@192.168.1.143:/home/tq/lock 添加远程仓库的地址,然后使用命令:git push origin master 将本地仓库提交到服务器仓库。这时候服务器仓库上就有了master分支了。

3、开发工作需要多人进行协同工作,假设lock项目组有一个成员叫tom,他需要先clone服务器仓库到本地:

tangquan@yalishiquede MINGW32 /i/tom (master)
$ git clone tq@192.168.1.143:/home/tq/lock
Cloning into 'lock'...
tq@192.168.1.143's password:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
Checking connectivity... done.

tangquan@yalishiquede MINGW32 /i/tom (master)
$ cd lock/

tangquan@yalishiquede MINGW32 /i/tom/lock (master)
$ git b
* master

好了现在tom已经获得了项目工程源码了,可以开始他的工作了。我们需要tom开发一个IIC驱动,tom不愿意也得做:
原来工程中的a文件内容只有一行tangquan:

tangquan@yalishiquede MINGW32 /i/tom/lock (master)
$ cat a
tangquan

tom要开发IIC驱动,他不能直接在master分支上开发,他需要创建一个新分支IIC:

tangquan@yalishiquede MINGW32 /i/tom/lock (master)
$ git co -b iic
Switched to a new branch 'iic'

然后tom添加了IIC驱动然后提交到本地仓库并且上传到服务器:

tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ vi a

tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ cat a
tangquan
tom add iic driver

tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ git add a

tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ git commit -m "tom add iic driver"
[iic f46a3d2] tom add iic driver
 1 file changed, 1 insertion(+)

tangquan@yalishiquede MINGW32 /i/tom/lock (iic)
$ git push origin iic
tq@192.168.1.143's password:
Counting objects: 3, done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To tq@192.168.1.143:/home/tq/lock
 * [new branch]      iic -> iic

然后我们查看服务器端的仓库内容:

tq@ubuntu:~/lock$ git b
  iic
* master
tq@ubuntu:~/lock$ git co iic 
fatal: This operation must be run in a work tree

我们试图在服务器上切换分支的时候提示这个操作必须在一个工作区上运行,然而我们服务器的仓库是由git –bare init创建的,没有工作区,好,那我们在原始创建仓库的工作目录下进行查看(这个原始创建仓库的人就是主导开发项目的经理,就是我):

tangquan@yalishiquede MINGW32 /i/me (master)
$ git fetch origin iic:iic
tq@192.168.1.143's password:
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From 192.168.1.143:/home/tq/lock
 * [new branch]      iic        -> iic
 + e3f3088...f46a3d2 iic        -> origin/iic  (forced update)

tangquan@yalishiquede MINGW32 /i/me (master)
$ git b
  iic
* master

tangquan@yalishiquede MINGW32 /i/me (master)
$ git co iic
Switched to branch 'iic'

tangquan@yalishiquede MINGW32 /i/me (iic)
$ cat a
tangquan
tom add iic driver

注意我使用的是git fetch命令将服务器上的iic分支获取到本地的,而不是使用git pull命令。切换到iic分之后查看a文件,的确是更新后的添加了iic驱动的文件,确定没有问题之后将iic分支merge到master分支:

tangquan@yalishiquede MINGW32 /i/me (iic)
$ git co master
Switched to branch 'master'

tangquan@yalishiquede MINGW32 /i/me (master)
$ git merge iic
Updating 7ffebb8..f46a3d2
Fast-forward
 a | 1 +
 1 file changed, 1 insertion(+)

tangquan@yalishiquede MINGW32 /i/me (master)
$ cat a
tangquan
tom add iic driver

到现在已经完成了iic驱动的开发了,可以删除掉iic分支了,就算新建分支删除分支麻烦那也不能直接在master分支上进行修改,master分支是一个稳定的分支,开发分支在确定没有问题之后才能merge到主分支上去。

如果现在项目组又进来一个人叫jack,他可以模仿tom的操作获得项目工程,我可以让他完成spi驱动的开发。这样下来一个小团队就由我、tom、jack组成了,并且可以使用Git完成协同工作。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值