#創建一個工作區
mkdir test_work
#創建一個git 倉庫(服務庫,非工作區)
mkdir test_git
cd test_git
git init --bare
# 創建一個post-receive 勾子,用於提交代碼后,自動將代碼更新到 工作區 test_work
vim ./hooks/post-receive
#! /bin/sh
GIT_WORK_TREE=test_work git checkout -f
sudo chmod -R 777 test_work
# 將以上三行shell 寫入 post-receive 文件里,
# 上面test_work 是你的工作區,路徑寫絕對路徑
# 寫完之后,保存退出vim 編輯器
#修改工作區的用戶和用戶組,用於 git 的receive 勾子有權限更新
chown git:git -R test_work
#修改test_git 倉庫的用戶和用戶組,並給 post-receive 文件 增加執行權限
chown git:git -R test_git
chmod +x test_git/hooks/post-receive
# 下面詳細步驟轉載 https://www.cnblogs.com/wangfg/p/9501441.html
# 1、安裝
$ yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-devel
$ yum install git
# 2、接下來我們 創建一個git用戶組和用戶,用來運行git服務:
$ groupadd git
$ useradd git -g git
$ passwd ******
# 3、創建git倉庫
$ cd /home/git
$ git init --bare gitrepo.git
$ chown -R git:git gitrepo.git
# 4、禁用shell登錄(為安全考慮,git用戶不允許登錄shell)
# 修改/etc/passwd
$ vim /etc/passwd
# 找到git的用戶設置 如:
git:x:502:503::/home/git:/bin/bash
改為
git:x:502:503::/home/git:/bin/git-shell
# 這樣用戶用git賬戶ssh連接后只能使用git命令了
# 5、打開 RSA 認證
# 進入 /etc/ssh 目錄,編輯 sshd_config,打開以下三個配置的注釋:
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
# 保存並重啟 sshd 服務:
$ /etc/rc.d/init.d/sshd restart
# 6、客戶端安裝git並在(window的git bash)git操作提交
# 進入一個空的工作目錄
# 設置用戶信息:
$ git config --global user.name "username"
$ git config --global user.email "your@example.com"
# 7、#初始化git
$ git init
# 8、管理公鑰
$ ssh-keygen -C 'your@email.com' -t rsa #為你生成rsa密鑰,可以直接一路回車,執行默認操作
# 客戶端生成密要方式同上。
# 生成密鑰后,一般C:\Users\用戶名\.ssh會出現
.ssh
├── id_rsa
└── id_rsa.pub # 公鑰 服務端需要里邊內容驗證連接着身份
# 將id_rsa.pub 里邊內容復制到authorized_keys
# 如找不到authorized_keys文件,則需在服務器端新建
$ cd /home/git
$ mkdir .ssh
$ chmod 755 .ssh
$ touch .ssh/authorized_keys
$ chmod 644 .ssh/authorized_keys
# 在客戶端上,打開 id_rsa.pub 復制里邊內容,一行一個
$ vim /home/git/.ssh/authorized_keys
# 粘貼客戶端生成的公鑰,保存退出
# 9、測試本地文件提交到遠程倉庫
$ vim readme.md
# 編輯些內容保存退出
$ git add readme.md #添加到git緩存中
$ git commit -m 'write readme file' #提交修改
# 添加遠程git倉庫
$ git remote add origin git@your_host_name:/home/git/gitrepo.git
$ git push origin master #這樣就同步到服務器了
# 其他人要同步
# 克隆和推送:
$ git clone git@your_host_name:/home/git/gitrepo.git
$ vim readme.md
$ git commit -am 'fix for the README file'
$ git push origin master
# 代碼同步(HOOK)
# 上邊git用於做了中心的版本控制
# 但是還想讓服務器接到修改更新后自動同步代碼到網站目錄中,便於測試開發
# 如下操作是可以實現
# 假定網站目錄在/www/web下
cd /home/git/gitrepo.git/hooks
vim post-receive #創建一個鈎子
# 寫入下面內容
GIT_WORK_TREE=/www/web git checkout -f
# 保存退出
chown git:git post-receive
chmod +x post-receive
# 如此,下次提交修改,代碼會自動同步到指定目錄中