前面讲述的大部分操作都是针对本地服务器。实际使用中,因为我们通常都是分布的世界各地进行协同开发,因此一个中间的同步服务器是必不可少的。同步服务器架设比较简单,只需要一个简单的文件服务器就行了。通常客户端访问这台文件服务器可以通过直接文件读写file://,HTTP,SSH,git等四种方式,除了HTTP只能读取外,其它三种需要在服务端上安装GIT服务程序。下面分别讲述:

1)服务器部署,需要首先说明的是服务器不保存工作目录中的文件,只保存.git目录下所有文件,因此首先要将这个文件导出来。

git clone –bare ../mygit/ mygit.git

然后将生成的mygit.git目录文件拷到服务器目录下,如下所示:

[root@wrlinux3 bongos]# cd mygit
[root@wrlinux3 mygit]# ls
main.c  README  testdir
[root@wrlinux3 mygit]# git clone --bare ../mygit/ mygit.git
Cloning into bare repository 'mygit.git'...
done.
[root@wrlinux3 mygit]# ls
main.c  mygit.git  README  testdir
[root@wrlinux3 mygit]#

2)账号管理,GIT没有自己的账号管理系统,如果采用SSH访问,通用的是服务器上的账号系统,只要这个账号对GIT目录有写权限,它就自己有读写权限。账号管理目前有几中通用的方法,一种是为每个用户建立一个账号,设置独立的权限。第二种是一个通用的账号,然后通过SSH KEY进行授权。第三种是LDAP.其中第二种是应用最多的。如GITHUB。第二种方式首先要创建id_rsa.pub.如下所示:

[root@wrlinux3 mygit]# cd ~/.ssh/
[root@wrlinux3 .ssh]# ls
known_hosts
[root@wrlinux3 .ssh]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
ec:b6:c9:9d:ac:0f:8f:eb:7c:1c:20:f5:ff:b3:35:06 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|        .        |
|       . .       |
|      ... .      |
|       .S. . E   |
|       .  . . .  |
|        +. . . o.|
|       + Oo.  +..|
|       .X=*   .o |
+-----------------+
[root@wrlinux3 .ssh]# cat id_rsa
id_rsa      id_rsa.pub 
[root@wrlinux3 .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyumP9ubMyAcpx30cDntEYUqUnj/R4w2t5fXS30bf1+FvCEtPA69SZx00LfOKLp45uAls8hQ+1HwvFSgZSbOXZdB59OJQT3GC/KRyy3TT8tKlJyJKOq7FfS0ukW+oAQ/SBJyAGL0EaM7igcKUF77/xf50PN9yLKEJ0pggdSoYnyCTYoQKWQaUyQ5yNyu+dADT5IpdMii1OQ0Wx/C4W59dbvpKzUBvX0UbNjCh5UPVrVUBKT+0vHKsdY3Kca6Cl73FCLG9C405fAJJ77/kLR690QSQ+0kICmCktWCZUUaQoedrwG8s+M31zu5hR1e9Div/e5WTigmYsu63dhw17AgzZw== [email protected]
[root@wrlinux3 .ssh]#

在服务端创建authorized.key

$ cat /tmp/id_rsa.john.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.josie.pub >> ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.jessica.pub >> ~/.ssh/authorized_keys

 

[root@wrlinux3 mygit]# mkdir myservgit
[root@wrlinux3 mygit]# ls
main.c  mygit.git  myservgit  README  testdir
[root@wrlinux3 mygit]# cd myservgit/
[root@wrlinux3 myservgit]# ls
[root@wrlinux3 myservgit]# cp -r -f ../
.git/      main.c     mygit.git/ myservgit/ README     testdir/  
[root@wrlinux3 myservgit]# cp -r -f ../mygit.git/ .
[root@wrlinux3 myservgit]# ls
mygit.git
[root@wrlinux3 myservgit]# cd ..
[root@wrlinux3 mygit]# ls
main.c  mygit.git  myservgit  README  testdir
[root@wrlinux3 mygit]# mkdir mycligit
[root@wrlinux3 mygit]# cd mycligit
[root@wrlinux3 mycligit]# ls
[root@wrlinux3 mycligit]# git clone /work/bongos/mygit/myservgit/mygit.git/
Cloning into 'mygit'...
done.
[root@wrlinux3 mycligit]# ls
mygit
[root@wrlinux3 mycligit]# cd ..
[root@wrlinux3 mygit]# ls
main.c  mycligit  mygit.git  myservgit  README  testdir
[root@wrlinux3 mygit]# mkdir mycligit2
[root@wrlinux3 mygit]# cd mycligit2/
[root@wrlinux3 mycligit2]# ls
[root@wrlinux3 mycligit2]# git clone file:///work/bongos/mygit/myservgit/mygit.git/
Cloning into 'mygit'...
remote: Counting objects: 39, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 39 (delta 4), reused 0 (delta 0)
Receiving objects: 100% (39/39), done.
Resolving deltas: 100% (4/4), done.
[root@wrlinux3 mycligit2]# cd ..
[root@wrlinux3 mygit]# ls
main.c  mycligit  mycligit2  mygit.git  myservgit  README  testdir
[root@wrlinux3 mygit]# mkdir mycligit3
[root@wrlinux3 mygit]# cd mycligit3/
[root@wrlinux3 mycligit3]# git clone ssh://root@localhost:mygit
Cloning into 'mygit'...
ssh: Could not resolve hostname : Name or service not known
fatal: The remote end hung up unexpectedly
[root@wrlinux3 mycligit3]# ps -ef|grep ssh
root      1579     1  0 Apr17 ?        00:00:00 /usr/sbin/sshd
root      9317     1  0 Apr19 ?        00:00:00 /usr/bin/ck-xinit-session /usr/bin/ssh-agent /etc/X11/xinit/Xclients
root      9423  9422  0 Apr19 ?        00:00:01 /usr/bin/ssh-agent /etc/X11/xinit/Xclients
500       9477     1  0 Apr19 ?        00:00:00 /usr/bin/ck-xinit-session /usr/bin/ssh-agent /etc/X11/xinit/Xclients
500       9550  9549  0 Apr19 ?        00:00:01 /usr/bin/ssh-agent /etc/X11/xinit/Xclients
root     32142 10366  0 16:13 pts/5    00:00:00 grep ssh
[root@wrlinux3 mycligit3]# ls
[root@wrlinux3 mycligit3]# cd ..
[root@wrlinux3 mygit]# ls
main.c  mycligit  mycligit2  mycligit3  mygit.git  myservgit  README  testdir
[root@wrlinux3 mygit]# cd mycligit3/
[root@wrlinux3 mycligit3]# ls
[root@wrlinux3 mycligit3]# git clone [email protected]:/work/bongos/mygit/myservgit/mygit.git/
Cloning into 'mygit'...
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
RSA key fingerprint is d5:df:d5:98:16:af:2f:a8:91:58:f3:56:a4:68:1e:f0.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (RSA) to the list of known hosts.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
Permission denied, please try again.
[email protected]'s password:
remote: Counting objects: 39, done.
remote: Compressing objects: 100% (34/34), done.
remote: Total 39 (delta 4), reused 0 (delta 0)
Receiving objects: 100% (39/39), done.
Resolving deltas: 100% (4/4), done.
[root@wrlinux3 mycligit3]# LS
bash: LS: command not found
[root@wrlinux3 mycligit3]# ls
mygit
[root@wrlinux3 mycligit3]#

如果账号过多,可以采用Gitosis进行管理

3)对外发布,可以采用Apache.GitWeb,这些安装时可以参考相关文档进行配置。

4)托管服务,常见托管服务器有github(www.github.com)和http://gitorious.org/

6)远程分支与本地分支的关系,这一部分前面并没有详述。实际上远端服务器的分支与本地分支并不存在必然的关系,需要手工映射或者同步才能将本地库与远程库关联起来。如下所示:

从上图可以看出GIT clone会创建出一个对应远端origin/master的本地master分支,但git feter只会同步本地的origin/master指针,不会同步master local,需要手动merger,当然可以使用git pull origin master。下面列出除了拉数据的几个常见操作:

【1】推数据到服务端 git push orign serverfix 也可以这样写git push origin serverfix:serverfix

【2】删除远程分支 git push origin :serverfix

【3】跟踪分支,实际就是映射远程分支的本地分支 git checkout –track origin/serverfix 或git checkout –b sf origin/serverfix