服务器端:Centos 7.3环境搭建git服务器
客户端:git客户端可以是windows、linux和mac
1、git服务器和客户端都安装Git
1
<code>[root@localhost ~]</code><code># yum install git</code>
2、git服务器上创建一个git用户组和用户,用来运行git服务
2
<code>[root@localhost ~]</code><code># groupadd git</code>
<code>[root@localhost ~]</code><code># useradd git -g git</code>
3、创建证书登录(如果用ssh key操作,要操作这步。如果用密码登录不需要操作这步)
收集所有需要登录的客户端的公钥,公钥位于id_rsa.pub文件中。ssh key可以让客户端与git服务器安全加密连接,而且不需要输入密码。
(1)客户端生成公钥和私钥。
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<code>[root@localhost ~]</code><code># ssh-keygen -t rsa -C "[email protected]"</code>
<code>Generating public</code><code>/private</code> <code>rsa key pair.</code>
<code>Enter </code><code>file</code> <code>in</code> <code>which</code> <code>to save the key (</code><code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>): </code>
<code>Created directory </code><code>'/root/.ssh'</code><code>.</code>
<code>Enter passphrase (empty </code><code>for</code> <code>no passphrase): </code>
<code>Enter same passphrase again: </code>
<code>Your identification has been saved </code><code>in</code> <code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.</code>
<code>Your public key has been saved </code><code>in</code> <code>/root/</code><code>.</code><code>ssh</code><code>/id_rsa</code><code>.pub.</code>
<code>The key fingerprint is:</code>
<code>64:78:e9:5d:72:d0:d5:0c:51:f9:</code><code>dc</code><code>:25:ff:b5:5b:d9 [email protected]</code>
<code>The key's randomart image is:</code>
<code>+--[ RSA 2048]----+</code>
<code>| .. .+*o|</code>
<code>| . . .. ..+|</code>
<code>| . = . o ++|</code>
<code>| = . + *|</code>
<code>| S . *|</code>
<code>| oE|</code>
<code>| o|</code>
<code>| . |</code>
<code>| |</code>
<code>+-----------------+</code>
(2)查看客户端生成的公钥。
<code>[root@localhost ~]</code><code># cat ~/.ssh/id_rsa.pub</code>
(3)git服务器上创建/home/git/.ssh/authorized_keys文件,并设置权限。
<code>[root@localhost ~]</code><code># cd /home/git/</code>
<code>[root@localhost git]</code><code># mkdir .ssh</code>
<code>[root@localhost git]</code><code># chmod 700 .ssh</code>
<code>[root@localhost git]</code><code># chown -R git.git .ssh</code>
<code>[root@localhost git]</code><code># touch .ssh/authorized_keys</code>
<code>[root@localhost git]</code><code># chmod 600 .ssh/authorized_keys (网上还有说法最好644)</code>
(4)把客户端公钥内容复制到/home/git/.ssh/authorized_keys文件
(5)git服务器上修改ssh配置文件,将密码验证关掉,开启ssh key验证。
<code>vi</code> <code>/etc/ssh/sshd_config</code>
<code>找到PasswordAuthentication节点并设置为no;</code>
<code>开启RSA认证,将前面的</code><code>#去掉,并确保如下配置:</code>
<code>RSAAuthentication </code><code>yes</code>
<code>PubkeyAuthentication </code><code>yes</code>
<code>AuthorizedKeysFile .</code><code>ssh</code><code>/authorized_keys</code>
(6)git服务器上重启SSH服务使配置生效:
<code>[root@localhost git]</code><code># systemctl restart sshd</code>
<code>[root@localhost git]</code><code># service sshd restart</code>
4、git服务器上初始化Git仓库
首先我们选定一个目录作为Git仓库,比如是/home/gitrepo/runoob.git(叫这个名字,是因为参考完善别的文章):
<code>[root@localhost git]</code><code># cd /home</code>
<code>[root@localhost home]</code><code># mkdir gitrepo</code>
<code>[root@localhost home]</code><code># chown git:git gitrepo/</code>
<code>[root@localhost home]</code><code># cd gitrepo</code>
<code>[root@localhost gitrepo]</code><code># git init --bare runoob.git</code>
<code>初始化空的 Git 版本库于 </code><code>/home/gitrepo/runoob</code><code>.git/</code>
<code>[root@localhost gitrepo]</code><code># chown -R git:git runoob.git</code>
<code>备注:服务器上的Git仓库名一般都以.git结尾。然后,把仓库所属用户改为git:</code>
5、客户端操作,克隆仓库
<code>[root@localhost ~]</code><code># mkdir testdata</code>
<code>[root@localhost testdata]</code><code># git clone [email protected]:/home/gitrepo/runoob.git</code>
<code>Initialized empty Git repository </code><code>in</code> <code>/root/testdata/runoob/</code><code>.git/</code>
<code>warning: You appear to have cloned an empty repository.</code>
6、客户端操作,提交文件
23
24
<code>[root@localhost testdata]</code><code># cd runoob/</code>
<code>[root@localhost runoob]</code><code># vi test.sh</code>
<code>[root@localhost runoob]</code><code># git add test.sh </code>
<code>[root@localhost runoob]</code><code># git commit -m "测试"</code>
<code>[master (root-commit) ee961b2] 测试</code>
<code> </code><code>1 files changed, 1 insertions(+), 0 deletions(-)</code>
<code> </code><code>create mode 100644 </code><code>test</code><code>.sh</code>
<code>[root@localhost runoob]</code><code># git status</code>
<code># On branch master</code>
<code>nothing to commit (working directory clean)</code>
<code>[root@localhost runoob]</code><code># git log</code>
<code>commit ee961b270d4541ff7440765a4c32d9ea722e3611</code>
<code>Author: gxm <gxm@</code><code>test</code><code>.com></code>
<code>Date: Sun May 22 09:02:40 2016 +0800</code>
<code> </code><code>测试</code>
<code>[root@localhost runoob]</code><code># git remote -v</code>
<code>origin [email protected]:</code><code>/home/gitrepo/runoob</code><code>.git (fetch)</code>
<code>origin [email protected]:</code><code>/home/gitrepo/runoob</code><code>.git (push)</code>
<code>[root@localhost runoob]</code><code># git push origin master</code>
<code>Counting objects: 3, </code><code>done</code><code>.</code>
<code>Writing objects: 100% (3</code><code>/3</code><code>), 216 bytes, </code><code>done</code><code>.</code>
<code>Total 3 (delta 0), reused 0 (delta 0)</code>
<code>To [email protected]:</code><code>/home/gitrepo/runoob</code><code>.git</code>
<code> </code><code>* [new branch] master -> master</code>
7、git服务器上,可以查看objects这个时间知道是否提交了
<code>[root@localhost runoob.git]</code><code># ll</code>
<code>总用量 12</code>
<code>drwxr-xr-x. 2 git git 6 9月 14 00:12 branches</code>
<code>-rw-r--r--. 1 git git 66 9月 14 00:12 config</code>
<code>-rw-r--r--. 1 git git 73 9月 14 00:12 description</code>
<code>-rw-r--r--. 1 git git 23 9月 14 00:12 HEAD</code>
<code>drwxr-xr-x. 2 git git 242 9月 14 00:12 hooks</code>
<code>drwxr-xr-x. 2 git git 21 9月 14 00:12 info</code>
<code>drwxr-xr-x. 10 git git 90 9月 14 00:55 objects</code>
<code>drwxr-xr-x. 4 git git 31 9月 14 00:12 refs</code>
本文转自 sailikung 51CTO博客,原文链接:http://blog.51cto.com/net881004/2068517,如需转载请自行联系原作者