1.安裝git 並修改sshd配置文件
# yum -y install git
# git --version
git version 1.7.1 #注意服務器版本和git版本,不同版本的系統的git配置可能不同
# vim /etc/ssh/sshd_config #修改配置文件允許通過密鑰認證
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_key
2.添加用戶並修改倉庫屬組和屬主
# useradd git # 添加用戶,注意這裏要和你未來web服務器的名字要相同,如果不是做web服務器用的可以隨便取名字
# passwd git# 添加密碼
# chown -R git:git /path/to/projectDir/ # 修改倉庫的屬組和屬主
3.選定一個目錄作爲git倉庫假定是/srv/sample.git
,在/srv
目錄下輸入命令:
git init --bare sample.git
4.客戶端生成密鑰
$ download https://git-scm.com/download/win #在windows下下載git客戶端
$ 安裝、右鍵打開Git Bash
$ ssh git@gitserverIp #ssh連接你的web服務器,如ssh [email protected], 這裏是git是你剛剛添加的用戶。
輸入yes
這裏需要輸入yes,是ssh的安全機制,用於首次通信,第二次連接就不會有了,此時C:\Users\用戶\.ssh 多出一個known_hosts文件
$ ssh-keygen -t rsa #生成公鑰和密鑰,此時 C:\Users\用戶\.ssh\會多出兩個文件id_rsa.pub和id_rsa.pub,公鑰和私鑰,複製id_rsa.pub內容
5.複製本地公鑰到服務器
# mkdir -p /home/git/.ssh #由配置文件我們把認證信息放到了用戶家目錄下的.ssh文件夾中,www爲剛剛添加的用戶
# vim /home/git/.ssh/authorized_keys #粘貼你剛剛複製的id_rsa.pub內容
# chmod 700 /home/git/.ssh/ #爲了保證安全性,需要修改權限
# chmod 600 /home/git/.ssh/authorized_keys #修改文件權限
# usermod -s /usr/bin/git-shell git#不允許該用戶登錄,只能做git操作
如果想要簡潔操作可以在Windows git bash上使用這個命令:
ssh-copy-id -i /c/Users/用戶名/.ssh/id_rsa.pub [email protected]
linux也可以使用上面這條命令或者執行
# ssh-keygen //生成linux的公鑰和私鑰 /root/.ssh目錄下面
# ls
authorized_keys(公鑰) id_dsa known_hosts 將公鑰拷貝到到服務器的/home/git/.ssh/authorized_keys
# git clone [email protected]:/disk/git/test.git 從服務器克隆
Initialized empty Git repository in /root/.ssh/test/.git/
The authenticity of host '192.168.0.246 (192.168.0.246)' can't be established.
RSA key fingerprint is 8f:17:75:e3:1b:8b:91:0b:91:62:e1:a3:02:8d:83:ff.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.246' (RSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.(第一次是空的還沒放文件呢)
更新版本
# git add .
# git commit -m "first commit" //註釋
[master (root-commit) 69e2d7a] first commit
Committer: root <root@node1.(none)>
Your name and email address were configured automatically based //linuc用戶會自動註冊email和用戶
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:
git config --global user.name "Your Name" 或使用這2條命令 在windows下需要執行 不然會報錯
git config --global user.email [email protected]
If the identity used for this commit is wrong, you can fix it with:
git commit --amend --author='Your Name <[email protected]>'
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 1.test
# git remote add gitserv [email protected]:/disk/git/test.git 給遠程服務器起個別名
# git push -u gitserv master 上傳
注意:如果有多個人員參與,每個人的公鑰都要追加到authorized_keys裏面,ssh-copy-id命令會自動創建.ssh文件夾和追加公鑰到authorized_keys文件裏
6: tortoiseGit來管理項目(可選)
$ download
https://download.tortoisegit.org/tgit/2.4.0.0/TortoiseGit-2.4.0.2-64bit.msi #
https://download.tortoisegit.org/tgit/2.4.0.0/TortoiseGit-LanguagePack-2.4.0.0-64bit-zh_CN.msi #官方漢化工具
$ 在項目文件上,右鍵
URL:[email protected]:/path/to/projectDir/