分布式版本管理工具Git
安装
yum -y install git
检查版本
git version
配置GIT服务器
为git添加单独的用户,本步骤是出于远程仓库的安全考虑,不是必须的
groupadd git
useradd git -g git # -g 参数后是组名
passwd git # 需要设置git用户的密码,比如密码设置为git123456,也可以直接按回车密码设空。
禁用shell登录
本步骤是出于用户登录的安全考虑,不是必须的
vi /etc/passwd
修改文件中下面这行的内容
git:x:500:502::/home/git:/bin/bash
# 改为
git:x:500:502::/home/git:/usr/bin/git-shell
注意:对于某些新版的git,其git-shell的相关功能还需要进行一些简单的配置才行,要不要做下面的配置取决于你在客户端执行下面的远程访问命令会不会报错。
ssh -T git@remotehost # remotehost是上面在配置的这台服务器主机名或者IP地址
如果命令执行报错,那么请在服务器端做下面的操作
mkdir /home/git/git-shell-commands
cat >/home/git/git-shell-commands/no-interactive-login <<\EOF
> #!/bin/sh
> printf '%s\n' "Hi $USER! You've successfully authenticated, but I do not"
> printf '%s\n' "provide interactive shell access."
> exit 128
> EOF
chmod +x /home/git/git-shell-commands/no-interactive-login
创建共享仓库
本步骤是创建远程仓库的必须步骤
mkdir -p /home/git/woniuboss.git
git init --bare /home/git/woniuboss.git # 初始化一个裸仓库
chown -R git:git /home/git/woniuboss.git # 改变仓库目录的拥有者,此步骤有赖于步骤1
登陆证书
本步骤是为了避免客户端每次提交时都要输入密码,不是必须的
服务器
mkdir -p /home/git/.ssh
chmod 700 /home/git/.ssh # 注意此权限是必须步骤,千万不能错误
touch /home/git/.ssh/authorized_keys
chmod 600 /home/git/.ssh/authorized_keys # 注意此权限是必须步骤,千万不能错误
chown -R git:git /home/git/.ssh # 此步骤有赖于步骤1
将客户端公钥id_rsa.pub文件的内容写到服务器端 /home/git/.ssh/authorized_keys 文件里
vi /etc/ssh/sshd_config
修改sshd-config配置文件内容,打开下面内容的注释
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
重启sshd服务
service sshd restart # centos 6
systemctl restart sshd # centos 7
客户端
生成证书
ssh-keygen -t rsa -C "XXX@XXX.com"
执行结果如下(注意命令执行中出现的手工输入环节,都是直接按键盘回车键即可,不必输入其它)
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/XXX/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/XXX/.ssh/id_rsa.
Your public key has been saved in /c/Users/XXX/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:79kTL322MCX5Y/KNC6wr65YPVbo3O1BSZeBs/WSic/k XXX@XXX.com
The key's randomart image is:
+---[RSA 2048]----+
| .oo |
| o.o |
| .= o o|
| .+...* |
| S ooooo..|
| o.o.o+. |
| ..o.=B +E|
| +o =++O.=|
| oo+*.o+o*o|
+----[SHA256]-----+
其中id_rsa是私钥,id_rsa.pub是公钥,这个需交给git服务器端用于配置。
工作原理

使用方法
本地克隆仓库
git clone git@jacky-vpc:/home/git/woniuboss.git # url的格式:用户名@服务器地址:仓库路径
git remote add origin git@jacky-vpc:/home/git/woniuboss.git # 将本地已经存在的git仓库与刚刚创建的远程仓库关联
执行结果如下
Cloning into 'woniuboss'...
git@jacky-vpc's password:
warning: You appear to have cloned an empty repository.
本地提交代码前的信息配置
git config --global user.name "Your Name" # 配置git用户名字
git config --global user.email "email@example.com" # 配置git用户邮箱
WINDOWS下需要再添加一个配置,否则可能后续Git命令可能会报warning
warning: LF will be replaced by CRLF in readme.txt. # 这行是windows可能显示的警告信息
git config --global core.autocrlf false # 要添加的配置命令
一个配置,否则可能后续Git命令可能会报warning
warning: LF will be replaced by CRLF in readme.txt. # 这行是windows可能显示的警告信息
git config --global core.autocrlf false # 要添加的配置命令
3298

被折叠的 条评论
为什么被折叠?



