第一步,安装git服务器:
使用yum安装:
yum install -y git
第二步,创建一个git
用户,用来运行git
服务:
adduser git
第三步:客户端(sourcetree)生成ssh证书
第四步:将客户端生成的公钥上传到git服务器上
a.在git用户家目录下创建.ssh目录
mkdir /home/git/.sshb.将客户端的公钥导入到
authorized_keys
文件里
将客户端的公钥使用rz上传到服务器
cat 公钥文件 >> /home/git/.ssh/authorized_keys
第五步:初始化Git仓库
先选定一个目录作为Git仓库,假定是/data/sample.git
,在/
data
目录下输入命令:
cd /data
git init --bare sample.git
Git就会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git
结尾。然后,把owner改为git
:
chown -R git:git sample.git
第六步,禁用shell登录(这步可以不配置,影响不大)
出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd
文件完成。找到类似下面的一行:
git:x:1001:1001:,,,:/home/git:/bin/bash
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell这样,
git
用户可以正常通过ssh使用git,但无法登录shell,因为我们为
git
用户指定的
git-shell
每次一登录就自动退出。
第七步:修改客户端config配置文件
编辑客户端.git/config文件,增加:
[user] name = git email = [email protected] [receive] denyCurrentBranch = ignore #解决客户端不能push操作
第八步:
cd /data/sample.git
git config --bool core.bare true
第九步:修改sshd配置信息
vim /etc/ssh/sshd_config
#StrictModes yes MaxAuthTries 6 #MaxSessions 10 #RSAAuthentication yes PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys #默认使用.ssh目录下的authorized_keys文件进行ssh认证 #AuthorizedKeysCommand none #AuthorizedKeysCommandRunAs nobody
第十步:使用客户端软件(sourcetree)进行git克隆
a.点击”克隆/新建“
b.在“源路径/URL”输入远程git的仓库地址: ssh://[email protected]:22/data/sample.git
c.“目标路径”输入window环境下的存放目录
d.点击克隆按钮就可以完成git远程仓库的克隆了
参考:
http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/00137583770360579bc4b458f044ce7afed3df579123eca000
http://www.cnblogs.com/cosiray/archive/2012/06/01/2530967.html