一. linux服务器现有项目添加到版本库
前提服务器搭建好git
cd /www/wwwroot/h5.test.com
git init
ls -al
项目里会出现仓库
drwxr-xr-x 7 root root 4096 May 30 18:23 .git
git add .
git commit -m "first commit"
到本地电脑git 客户端
phper@xx.xxx.xxx.xxx:/www/wwwroot/h5.test.com
尝试新增或者修改内容
git add readme.md
git commit -m “tijiao1”
git push origin master
到了这一步会报错
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable t
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing int
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in som
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, se
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To xxx.xxx.xxx.xxx:/www/wwwroot/h5.test.com
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'phper@xxx.xxx.xxx.xxx:/www/wwwroot/h5.test.com '
vim /www/wwwroot/h5.test.com/.git/config
修改.git/config添加如下代码:[receive]
denyCurrentBranch = ignore
自动部署检出到项目目录
touch /www/wwwroot/h5.test.com/.git/post-receive
chown phper:git post-receive
post-receive内容
git --work-tree=/www/wwwrooth5.test.com /
–git-dir=/www/wwwroot/h5.test.com /.git checkout -f
chmod +x post-receive
chwon -R phper:git h5.test.com/
再次提交
git push origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 483 bytes | 0 bytes/s, done.
Total 6 (delta 2), reused 0 (delta 0)
To 60.205.249.232:/www/wwwroot/h5.test.com
52711fe..6018ecb master -> master
忽略某个已经托管给git的文件,防止二次提交
# 执行命令将文件加入不提交队列
git update-index --assume-unchanged 你的文件路径
# 执行命令将文件取消加入不提交队列
git update-index --no-assume-unchanged 你的文件路径
禁止客户端shell登录
因为前面我们添加了客户端的ssh的公钥到远程服务器,所以客户端可以直接通过shell远程登录服务器,这不安全,也不是我们想要的。且看下面如何禁用shell登录
mkdir /home/phper/git-shell-commands
#phper:x:1002:1003::/home/phper:/bin/bash
phper:x:1002:1003::/home/phper:/bin/git-shell