1. 程式人生 > >本地git倉庫推送到伺服器自建的git倉庫實現目錄檔案同步教程[自整理]

本地git倉庫推送到伺服器自建的git倉庫實現目錄檔案同步教程[自整理]

1.首先,先在伺服器上安裝git,如果有git的話就不用走這一步了

 yum安裝git
[[email protected] ~]# cd src/
[[email protected] src]# wget http://dl.fedoraproject.org/pub/epel/5/x86_64/epel-release-5-4.noarch.rpm
[[email protected] src]# rpm -ivh epel-release-5-4.noarch.rpm
Preparing...                ########################################### [100%]
    package epel-release-5-4.noarch is already installed
[
[email protected]
~]# yum list [[email protected] ~]# yum install -y git

2.建立一個使用者來執行git服務

[[email protected] ~]# adduser git    //建立使用者git
[[email protected] ~]# passwd git     //設定使用者git的密碼

3.需要在你本地建立一個key公鑰證書,說白了就是在本地弄個標識好讓伺服器知道是你推送過來
開啟本地git客戶端介面

$ ssh-keygen -t rsa     //生成key證書公鑰私鑰 一路回車就ok了
$ cat  .ssh/id_rsa.pub  //開啟公鑰


類似圖上這種 然後複製一下
在伺服器操作命令

[[email protected] /]# su git       //切換git使用者
[[email protected] /]$ cd home       //進去home(git使用者的公鑰存放)
[[email protected] home]$ cd git     //開啟git資料夾
[[email protected] ~]$ cd .ssh       //開啟公鑰資料夾
[[email protected] .ssh]$ touch authorized_keys   //建立公鑰檔案
[[email protected]
.ssh]$ vim authorized_keys //將剛才複製的那一串公鑰貼上進去

如果沒有.ssh檔案 需要你在上一步生成祕鑰和公鑰
這樣的話,如果是多人需要用到這個使用者的話,就需要每個人把自己電腦上的公鑰給管理員,然後管理員在伺服器進行新增就可以,這樣下次登入就不需要密碼驗證了,直接驗證你電腦上的公鑰即可.
後邊會寫到鉤子自動同步更新,所以這裡生成這個公鑰,同樣在伺服器上也要生成一個然後放進.ssh裡邊的authorized_keys裡邊就好

[[email protected] ~]$ chmod 700 .ssh
[[email protected] ~]$ cd .ssh
[[email protected] .ssh]$ chmod 600 authorized_keys

按照上邊給予許可權即可!
4.這一步特別重要,很多網友都會忽略,導致伺服器上公鑰沒作用!
記得切換root使用者,git沒許可權!

開啟檔案/etc/ssh/sshd_config
RSAAuthentication yes            #開啟RSA認證功能
PubkeyAuthentication yes      #開啟公匙認證
StricModes no                          #據說不改會強制要求登入使用者和檔案擁有者使用者相同

找到以上三個然後把註釋去掉就ok
5.接下來,在伺服器上初始化一個git倉庫

[[email protected] svnrepos]$ su root                                //切換root使用者, 因為git沒有任何許可權
Password: 
[[email protected] svnrepos]# git init --bare hello.git             //在該目錄初始化一個倉庫,倉庫名叫hello.git
Initialized empty Git repository in /data/wwwroot/default/svnrepos/hello.git/  //你倉庫的地址,記好了後邊要要用到[[email protected] svnrepos]# cd hello.git/
[[email protected] hello.git]# ls
branches  config  description  HEAD  hooks  info  objects  refs

5.完事後會建立一個裸倉庫,這個倉庫沒有工作區,因為只是純粹用來共享而已,所以不讓使用者直接登入到伺服器上去改工作區,並且伺服器上的Git倉庫通常都以.git結尾。然後,把git使用者的許可權設定為

[[email protected] svnrepos]# chown -R git.git hello.git
[[email protected] svnrepos]# ls -l
total 4
drwxr-xr-x 7 git git 4096 Apr 13 10:39 hello.git
[[email protected] svnrepos]# 

6.許可權給成功後,可以看出hello.git這個倉庫git也有許可權操作了.
這時候可以在本地建立個倉庫了

[email protected] MINGW64 /d (master)
$ cd hello

[email protected] MINGW64 /d/hello (master)
$ git add 1.txt

[email protected] MINGW64 /d/hello (master)
$ git commit -m "1.txt"
[master (root-commit) 8d3e977] 1.txt
 1 file changed, 1 insertion(+)
 create mode 100644 hello/1.txt

好了,現在建立了一個1.txt檔案.接下來,推送到伺服器上的倉庫

$ git remote add origin [email protected]:/data/wwwroot/default/svnrepos/hello.git  //本地連線遠端庫

連線ok後可以通過 git remote -v 來檢視 如果不對可以用 git remote rm origin 來刪除
接下來推送到伺服器的倉庫

$ git push origin master //推送到遠端倉庫

ok 如果推送成功後 現在伺服器倉庫就有剛才所新增的檔案了
但是.如果報錯的話! 貼個類似的錯誤


如果是報這個錯誤,是因為你本地剛才建立的那個key公鑰已經被匹配或者是沒有該目錄git使用者沒有許可權.
可以把本地公鑰刪除了重新再生成或者是伺服器倉庫git使用者的許可權就可以!!

6.接下來,在伺服器上將倉庫的檔案給克隆下來!

[[email protected] svnrepos]$ git clone [email protected]:/data/wwwroot/default/svnrepos/hello.git //克隆伺服器倉庫資料

克隆後,我們要用git 的鉤子寫個自動執行程式.

[[email protected] svnrepos]$ cd hello.git
[[email protected] hello.git]$ cd hooks
[[email protected] hooks]$touch post-receive          //建立自動執行檔案
[[email protected] hooks]$chmod -R 777 post-receive   //給個許可權
[[email protected] hooks]$vim post-receive

7.開啟後 寫入下邊這些自動執行命令

#!/bin/sh  
export LANG=zh_CN.UTF-8
cd /data/wwwroot/default/svnrepos/hello      //這個是你每次要同步的資料夾
unset GIT_DIR                                //這個很重要! 很多同學沒有寫這個就同步不了,因為git執行自動指令碼的時候有執行一些自定義變數,所以我們在這裡unset一下
git pull origin ceshi                        //這個當然就是更新了 因為我建立了個ceshi的分支,這個可以更改為你們要同步的分支

寫完儲存退出!
到這裡基本都可以實現同步了,在本地客戶端推送一個上去然後檢視伺服器有沒有同步就可以了!

如果沒有的話就是你這中間出了什麼問題了配置錯誤了,改一下就ok! 感謝大家評論指出問題 謝謝 ~