Git基本概念及操作(4)

前面講述的大部分操作都是針對本地服務器。實際使用中,因爲我們通常都是分佈的世界各地進行協同開發,因此一箇中間的同步服務器是必不可少的。同步服務器架設比較簡單,只需要一個簡單的文件服務器就行了。通常客戶端訪問這臺文件服務器可以通過直接文件讀寫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)遠程分支與本地分支的關係,這一部分前面並沒有詳述。實際上遠端服務器的分支與本地分支並不存在必然的關係,需要手工映射或者同步才能將本地庫與遠程庫關聯起來。如下所示:

image

從上圖可以看出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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章