环境:centos7
- 安装git
[root@localhost home]#yum install ‐y git
- 创建一个git用户,用来运行git服务
[root@localhost home]# adduser git [root@localhost home]# passwd git
- 将用户的公钥,即C:\Users\Administrator\.ssh下的id_rsa.pub 文件导入到/home/git/.ssh/authorized_keys文件里,一行一个。
用户需安装git,教程:https://www.runoob.com/git/git-tutorial.html
若用户系统没有.ssh文件,使用git命令生成:
$ ssh-keygen -t rsa -C “邮箱地址”
Linux使用下面命令查看默认被隐藏的.ssh文件夹:
[root@localhost git]# ll -a
若没有/home/git/.ssh/authorized_keys文件则新建它:
[root@localhost git]# cd /home/git/ [root@localhost git]# mkdir .ssh chmod 700 .ssh [root@localhost git]# touch .ssh/authorized_keys [root@localhost git]# chmod 600 .ssh/authorized_keys
- 初始化Git仓库
选定一个目录作为Git仓库,假定是/home/gitrepo/runoob.git。
[root@localhost home]# mkdir gitrepo [root@localhost home]# cd gitrepo/ [root@localhost gitrepo]# git init --bare runoob.git
以上命令Git创建一个空仓库,服务器上的Git仓库通常都以.git结尾。把仓库所属用户改为git:
[root@localhost gitrepo]# chown -R git:git runoob.git
- 修改钩子文件,目的是在/var/local/shop下生成推送的代码,即推送的代码在这目录下显示
[root@localhost home]# cd /home/gitrepo/runoob.git/hooks [root@localhost hooks]# vim post-receive
内容:
#!/bin/bash git ‐‐work‐tree=/var/local/shop checkout ‐‐force
修改工作目录权限:
[root@localhost hooks]# chmod ‐R 777 /var/local/shop
- 禁用shell登录:
出于安全考虑,第二步创建的git用户不允许登录shell,这可以通过编辑/etc/passwd文件完成。修改git用户为:
git:x:1001:1001:,,,:/home/git:/usr/bin/git-shell
- 用户在本地使用git初始化仓库
Administrator@DESKTOP-I7LHT3R MINGW64 /e/wamp/projects/blog $ git init Initialized empty Git repository in E:/wamp/projects/blog/.git/‘’ //将本地目录关联至远程服务器的Git仓库 $ git remote add origin git@192.168.153.130:/home/gitrepo/runoob.git
-
用户提交代码到git服务器
$ git status //检查本地文件与服务器之间的区别 $ git add . //将所有修改添加进暂存区中 $ git commit ‐m 'v1.0' //将暂存区的内容提交至当前分支 $ git push origin master //将当前分支推送到远程服务器(输入git用户密码)
搭建好git服务器后,协同开发的人员可以将各自电脑公钥加入到服务器authorized_keys文件里,然后clone下来。
如果是php项目,可以在正式服务器搭建好git服务,然后第5步可以指向代码存放目录,这样在本地修改的代码可以直接提交到正式服务器,实现快速部署上线。
欢迎观看的仁兄指出错误或提出改进意见。