天天看點

git在本地建立遠端倉庫 - shihuc

git在本地建立遠端倉庫,管理分布式開發的scm

類似的博文,在前面的文章裡面也提到過,當時講述的是一個入門級别的。其URL是ssh://username@repo-host-address/repo-path這種格式。

今天再說說如何建立類似GitHub那種以git@repo-host:/path/proj.git這種放個的遠端倉庫。這個是不是看起來很酷???

其實比較簡單:

第一步:在安裝git軟體。源碼安裝或者yum等都行。我的版本資訊如下

1 [root@CloudGame tools]# git --version
2 git version 2.6.0-rc1      

第二步:建立遠端倉庫目錄。看下面的操作就可以很清楚了。就是建立一個檔案夾而已。

1 [root@CloudGame home]# mkdir -p /data/git      

第三步:建立git使用者,并設定相關的組及安全。

1 [root@CloudGame home]# useradd -r -d /data/git git  #添加git使用者為系統使用者,并指定其home目錄為/data/git      
1 [root@CloudGame home]# chown -R git:git /data/git   #将git使用者的家目錄設定為git組,git使用者      
1 [root@CloudGame home]# cd /data
2 [root@CloudGame data]# ll
3 total 4
4 drwxr-xr-x 2 git git 4096 Jan 20 09:25 git
5 [root@CloudGame data]# cd git/
6 [root@CloudGame git]# ll                 #檢視目錄内容為空,說明目前裡面什麼也沒有,的确,還沒有做任何操作呢。
7 total 0      

第四步:建立一個空的git倉庫。并将倉庫裡面的所有的檔案設定為git組,git使用者

1 [root@CloudGame git]# git init --bare mueas.git    #注意,這裡最好帶上--bare指定一個空倉庫,否則用戶端clone後,送出代碼時,會遇到錯誤,要做系列配置
 2 Initialized empty Git repository in /data/git/mueas.git/
 3 [root@CloudGame git]# ll
 4 total 4
 5 drwxr-xr-x 7 root root 4096 Jan 20 09:28 mueas.git
 6 [root@CloudGame git]# ll -al
 7 total 12
 8 drwxr-xr-x 3 git  git  4096 Jan 20 09:28 .
 9 drwxr-xr-x 3 root root 4096 Jan 20 09:25 ..
10 drwxr-xr-x 7 root root 4096 Jan 20 09:28 mueas.git
11 [root@CloudGame git]# cd mueas.git/
12 [root@CloudGame mueas.git]# ll
13 total 32
14 drwxr-xr-x 2 root root 4096 Jan 20 09:28 branches
15 -rw-r--r-- 1 root root   66 Jan 20 09:28 config
16 -rw-r--r-- 1 root root   73 Jan 20 09:28 description
17 -rw-r--r-- 1 root root   23 Jan 20 09:28 HEAD
18 drwxr-xr-x 2 root root 4096 Jan 20 09:28 hooks
19 drwxr-xr-x 2 root root 4096 Jan 20 09:28 info
20 drwxr-xr-x 4 root root 4096 Jan 20 09:28 objects
21 drwxr-xr-x 4 root root 4096 Jan 20 09:28 refs      
1 [root@CloudGame git]# chown -R git.git /data/git/*
 2 [root@CloudGame git]# ll
 3 total 4
 4 drwxr-xr-x 7 git git 4096 Jan 20 09:28 mueas.git
 5 [root@CloudGame git]# cd mueas.git/
 6 [root@CloudGame mueas.git]# ll
 7 total 32
 8 drwxr-xr-x 2 git git 4096 Jan 20 09:28 branches
 9 -rw-r--r-- 1 git git   66 Jan 20 09:28 config
10 -rw-r--r-- 1 git git   73 Jan 20 09:28 description
11 -rw-r--r-- 1 git git   23 Jan 20 09:28 HEAD
12 drwxr-xr-x 2 git git 4096 Jan 20 09:28 hooks
13 drwxr-xr-x 2 git git 4096 Jan 20 09:28 info
14 drwxr-xr-x 4 git git 4096 Jan 20 09:28 objects
15 drwxr-xr-x 4 git git 4096 Jan 20 09:28 refs      

第五步:設定git使用者的安全政策,不允許其具有登入系統的權限。修改/etc/passwd檔案,找到git使用者行,如下紅色行為修改後的内容。修改前,是/bin/bash。

1 lighttpd:x:501:501::/home/lighttpd:/sbin/nologin
2 dockerroot:x:494:488:Docker User:/var/lib/docker:/sbin/nologin
3 stack:x:502:502::/opt/stack:/bin/bash
4 chrony:x:493:487::/var/lib/chrony:/sbin/nologin
5 git:x:492:486::/data/git:/usr/bin/git-shell
6 "/etc/passwd" 56L, 2976C         

第六步:為了能ssh連結,需要在這個遠端倉庫mueas.git的上一級目錄下建立.ssh目錄,并将用戶端機器上的公鑰存放在這個目錄裡面的authorized_keys檔案裡面。注意,若是多個用戶端要通路,則需要将多個客戶機的公鑰追加到這個檔案的後面。這裡,我就在我自己的機器上測試,是以,我隻需要将我自己機器的~/.ssh/id_rsa.pub檔案内容copy到authorized_keys檔案裡面即可。當然,也可以是dsa格式的檔案。【要是用戶端使用者目錄下沒有~/.ssh目錄,可以通過ssh-keygen -t rsa或ssh-keygen -t dsa進行建立,同時會生成key檔案,公私都有】

1 [root@CloudGame git]# mkdir .ssh        #建立.ssh目錄,注意目錄的層次關系
 2 [root@CloudGame git]# ll
 3 total 4
 4 drwxr-xr-x 7 git git 4096 Jan 20 09:28 mueas.git
 5 [root@CloudGame git]# ll -al
 6 total 16
 7 drwxr-xr-x 4 git  git  4096 Jan 20 09:32 .
 8 drwxr-xr-x 3 root root 4096 Jan 20 09:25 ..
 9 drwxr-xr-x 7 git  git  4096 Jan 20 09:28 mueas.git
10 drwxr-xr-x 2 root root 4096 Jan 20 09:33 .ssh      
1 [root@CloudGame .ssh]# cd ..          #在.ssh目錄下建立authorized_keys檔案
2 [root@CloudGame git]# cd .ssh
3 [root@CloudGame .ssh]# ll -al
4 total 8
5 drwxr-xr-x 2 root root 4096 Jan 20 10:16 .
6 drwxr-xr-x 4 git  git  4096 Jan 20 10:16 ..
7 [root@CloudGame .ssh]# vim authorized_keys      
1 [root@CloudGame tools]# cat ~/.ssh/id_rsa.pub    #檢視id_rsa.pub内容,并将其顯示出來的内容(藍色部分)copy到/data/git/.ssh/authorized_keys檔案裡面
2 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAutquVDcyjoxwXzbrgLcu/wlK9SkXykkd5mktSPqA4exUc6flDv5dYzT3sWMYaH4LP/fiT2mhAoPRU8HaejOfnU3+ALunjXBtxr8XDZQDNrHnZ31477IUSBJ6XRlEj+sDVBDujAxGhNpP41B4v/bSpbrkOJGuVhUtcl81V/nKrCwvhpX+mGRviuiIRsv7E8HEb3AZ7hLXibuDP7kSe3M5nO3JOnsE7e3h8Ob7WAmkxPU/bGqALAodrp0vUyyLsdUt1lynauUZmOgaowL9C+eTbEtFQvCrVrRbXz6GE0VfS7WUA7rxtMujIxuh2fdCWIH4J/wuA+ul3qPsKEDa1MiBSQ== root@CloudGame      

到此,一個空的遠端倉庫就算建立好了。可以測試了。我在另外一個terminal下執行git clone這個mueas.git倉庫,但是這個時候,比較常見的問題如下:

1 [root@CloudGame tmp]# git clone [email protected]:/data/git/mueas.git
2 Cloning into \'mueas\'...
3 Agent admitted failure to sign using the key.
4 [email protected]\'s password: 
5 Permission denied, please try again.      

上面這個問題,要求輸入密碼,不對的話,會再提示輸入,一直到正确為止,真是扯淡,輸入密碼多費勁啊,我都提供了公鑰了啊,呵呵,這裡遺忘了一步,就是将目前使用者的私鑰添加到添加到ssh-agent的高速緩存中。看如下操作:

1 [root@CloudGame tmp]# ssh-add
2 Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)      

我再試試,看是不是管用:

1 [root@CloudGame tmp]# git clone [email protected]:/data/git/mueas.git 
2 Cloning into \'mueas\'...
3 warning: You appear to have cloned an empty repository.
4 Checking connectivity... done.      

^_^,是不是搞定,可以正常的clone遠端的倉庫了。

下面,我是不是要試試,在本地倉庫修改一下檔案,能否push到遠端倉庫呢?

1 [root@CloudGame mueas]# ll
 2 total 8
 3 -rw-r--r-- 1 root root 14 Jan 20 10:57 file.java
 4 -rw-r--r-- 1 root root  6 Jan 20 10:31 test.txt
 5 [root@CloudGame mueas]# git add file.java 
 6 [root@CloudGame mueas]# git commit -m "New file added"
 7 [master 7f3f3b0] New file added
 8  1 file changed, 1 insertion(+)
 9  create mode 100644 file.java
10 [root@CloudGame mueas]# git push
11 Counting objects: 3, done.
12 Delta compression using up to 4 threads.
13 Compressing objects: 100% (2/2), done.
14 Writing objects: 100% (3/3), 291 bytes | 0 bytes/s, done.
15 Total 3 (delta 0), reused 0 (delta 0)
16 To [email protected]:/data/git/mueas.git
17    5837025..7f3f3b0  master -> master      

是不是沒有問題,爽吧,下面再看看換一個使用者(shihuc)測試的結果。這裡需要注意的是,要将shihuc使用者目錄下的pubkey放入/data/git/.ssh/authorized_keys檔案裡面喲。很簡單,scp拷貝過去然後cat一下,append一下就ok了。不多說這個。

看看這步的clone和修改檔案上傳檔案是否有問題:

1 [shihuc@CloudGame Music]$ git clone [email protected]:/data/git/mueas.git
 2 Cloning into \'mueas\'...
 3 remote: Counting objects: 6, done.
 4 remote: Compressing objects: 100% (3/3), done.
 5 remote: Total 6 (delta 0), reused 0 (delta 0)
 6 Receiving objects: 100% (6/6), done.
 7 Checking connectivity... done.
 8 [shihuc@CloudGame Music]$ ll
 9 total 4
10 drwxrwxr-x 3 shihuc shihuc 4096 Jan 20 10:59 mueas
11 [shihuc@CloudGame Music]$ cd mueas/
12 [shihuc@CloudGame mueas]$ ll
13 total 8
14 -rw-rw-r-- 1 shihuc shihuc 14 Jan 20 10:59 file.java
15 -rw-rw-r-- 1 shihuc shihuc  6 Jan 20 10:59 test.txt
16 [shihuc@CloudGame mueas]$ vim file.java 
17 [shihuc@CloudGame mueas]$ 
18 [shihuc@CloudGame mueas]$ 
19 [shihuc@CloudGame mueas]$ 
20 [shihuc@CloudGame mueas]$ git status
21 On branch master
22 Your branch is up-to-date with \'origin/master\'.
23 Changes not staged for commit:
24   (use "git add <file>..." to update what will be committed)
25   (use "git checkout -- <file>..." to discard changes in working directory)
26 
27     modified:   file.java
28 
29 no changes added to commit (use "git add" and/or "git commit -a")
30 [shihuc@CloudGame mueas]$ git add file.java 
31 [shihuc@CloudGame mueas]$ git commit -m "modify with another user"
32 
33 *** Please tell me who you are.
34 
35 Run
36 
37   git config --global user.email "[email protected]"
38   git config --global user.name "Your Name"
39 
40 to set your account\'s default identity.
41 Omit --global to set the identity only in this repository.
42 
43 fatal: unable to auto-detect email address (got \'shihuc@CloudGame.(none)\')
44 [shihuc@CloudGame mueas]$ git config --global user.email "[email protected]"
45 [shihuc@CloudGame mueas]$ git config --global user.name "shihuc"
46 [shihuc@CloudGame mueas]$ 
47 [shihuc@CloudGame mueas]$ git commit -m "modify with another user"
48 [master 41d96e1] modify with another user
49  1 file changed, 8 insertions(+)
50 [shihuc@CloudGame mueas]$ 
51 [shihuc@CloudGame mueas]$ git push
52 warning: push.default is unset; its implicit value has changed in
53 Git 2.0 from \'matching\' to \'simple\'. To squelch this message
54 and maintain the traditional behavior, use:
55 
56   git config --global push.default matching
57 
58 To squelch this message and adopt the new behavior now, use:
59 
60   git config --global push.default simple
61 
62 When push.default is set to \'matching\', git will push local branches
63 to the remote branches that already exist with the same name.
64 
65 Since Git 2.0, Git defaults to the more conservative \'simple\'
66 behavior, which only pushes the current branch to the corresponding
67 remote branch that \'git pull\' uses to update the current branch.
68 
69 See \'git help config\' and search for \'push.default\' for further information.
70 (the \'simple\' mode was introduced in Git 1.7.11. Use the similar mode
71 \'current\' instead of \'simple\' if you sometimes use older versions of Git)
72 
73 Counting objects: 3, done.
74 Delta compression using up to 4 threads.
75 Compressing objects: 100% (3/3), done.
76 Writing objects: 100% (3/3), 363 bytes | 0 bytes/s, done.
77 Total 3 (delta 0), reused 0 (delta 0)
78 To [email protected]:/data/git/mueas.git
79    7f3f3b0..41d96e1  master -> master      

是不是也沒有問題,當然中間有點配置git的問題,就不多說,因為這個使用者很少用,是以環境沒有怎麼配置好。

git在本地建立遠端倉庫 - shihuc