xxl520 发表于 2018-9-18 12:40:25

Git基本概念及操作(4)

  前面讲述的大部分操作都是针对本地服务器。实际使用中,因为我们通常都是分布的世界各地进行协同开发,因此一个中间的同步服务器是必不可少的。同步服务器架设比较简单,只需要一个简单的文件服务器就行了。通常客户端访问这台文件服务器可以通过直接文件读写file://,HTTP,SSH,git等四种方式,除了HTTP只能读取外,其它三种需要在服务端上安装GIT服务程序。下面分别讲述:
  1)服务器部署,需要首先说明的是服务器不保存工作目录中的文件,只保存.git目录下所有文件,因此首先要将这个文件导出来。
  git clone –bare ../mygit/ mygit.git
  然后将生成的mygit.git目录文件拷到服务器目录下,如下所示:
  # cd mygit
  # ls
  main.cREADMEtestdir
  # git clone --bare ../mygit/ mygit.git
  Cloning into bare repository 'mygit.git'...
  done.
  # ls
  main.cmygit.gitREADMEtestdir
  #
  2)账号管理,GIT没有自己的账号管理系统,如果采用SSH访问,通用的是服务器上的账号系统,只要这个账号对GIT目录有写权限,它就自己有读写权限。账号管理目前有几中通用的方法,一种是为每个用户建立一个账号,设置独立的权限。第二种是一个通用的账号,然后通过SSH KEY进行授权。第三种是LDAP.其中第二种是应用最多的。如GITHUB。第二种方式首先要创建id_rsa.pub.如下所示:
  # cd ~/.ssh/
  # ls
  known_hosts
  # 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>  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 root@wrlinux3.nsn-nsn.net
  The key's randomart image is:
  +--[ RSA 2048]----+
  |               |
  |      .      |
  |       . .       |
  |      ... .      |
  |       .S. . E   |
  |       .. . .|
  |      +. . . o.|
  |       + Oo.+..|
  |       .X=*   .o |
  +-----------------+

  # cat>
  id_rsa   >
  # cat>  ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAyumP9ubMyAcpx30cDntEYUqUnj/R4w2t5fXS30bf1+FvCEtPA69SZx00LfOKLp45uAls8hQ+1HwvFSgZSbOXZdB59OJQT3GC/KRyy3TT8tKlJyJKOq7FfS0ukW+oAQ/SBJyAGL0EaM7igcKUF77/xf50PN9yLKEJ0pggdSoYnyCTYoQKWQaUyQ5yNyu+dADT5IpdMii1OQ0Wx/C4W59dbvpKzUBvX0UbNjCh5UPVrVUBKT+0vHKsdY3Kca6Cl73FCLG9C405fAJJ77/kLR690QSQ+0kICmCktWCZUUaQoedrwG8s+M31zu5hR1e9Div/e5WTigmYsu63dhw17AgzZw== root@wrlinux3.nsn-nsn.net
  #
  在服务端创建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
  # mkdir myservgit
  # ls
  main.cmygit.gitmyservgitREADMEtestdir
  # cd myservgit/
  # ls
  # cp -r -f ../
  .git/      main.c   mygit.git/ myservgit/ README   testdir/
  # cp -r -f ../mygit.git/ .
  # ls
  mygit.git
  # cd ..
  # ls
  main.cmygit.gitmyservgitREADMEtestdir
  # mkdir mycligit
  # cd mycligit
  # ls
  # git clone /work/bongos/mygit/myservgit/mygit.git/
  Cloning into 'mygit'...
  done.
  # ls
  mygit
  # cd ..
  # ls
  main.cmycligitmygit.gitmyservgitREADMEtestdir
  # mkdir mycligit2
  # cd mycligit2/
  # ls
  # 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.
  # cd ..
  # ls
  main.cmycligitmycligit2mygit.gitmyservgitREADMEtestdir
  # mkdir mycligit3
  # cd 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
  # ps -ef|grep ssh
  root      1579   10 Apr17 ?      00:00:00 /usr/sbin/sshd
  root      9317   10 Apr19 ?      00:00:00 /usr/bin/ck-xinit-session /usr/bin/ssh-agent /etc/X11/xinit/Xclients
  root      942394220 Apr19 ?      00:00:01 /usr/bin/ssh-agent /etc/X11/xinit/Xclients
  500       9477   10 Apr19 ?      00:00:00 /usr/bin/ck-xinit-session /usr/bin/ssh-agent /etc/X11/xinit/Xclients
  500       955095490 Apr19 ?      00:00:01 /usr/bin/ssh-agent /etc/X11/xinit/Xclients
  root   32142 103660 16:13 pts/5    00:00:00 grep ssh
  # ls
  # cd ..
  # ls
  main.cmycligitmycligit2mycligit3mygit.gitmyservgitREADMEtestdir
  # cd mycligit3/
  # ls
  # git clone root@127.0.0.1:/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.
  root@127.0.0.1's password:
  Permission denied, please try again.
  root@127.0.0.1's password:
  Permission denied, please try again.
  root@127.0.0.1'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.
  # LS
  bash: LS: command not found
  # ls
  mygit
  #
  如果账号过多,可以采用Gitosis进行管理
  3)对外发布,可以采用Apache.GitWeb,这些安装时可以参考相关文档进行配置。
  4)托管服务,常见托管服务器有github(www.github.com)和http://gitorious.org/
  6)远程分支与本地分支的关系,这一部分前面并没有详述。实际上远端服务器的分支与本地分支并不存在必然的关系,需要手工映射或者同步才能将本地库与远程库关联起来。如下所示:
http://acreep.blog.51cto.com/attachment/201205/7/3333560_1336381201232S.png
  从上图可以看出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

页: [1]
查看完整版本: Git基本概念及操作(4)