linux 下安装git
Linux下更新git版本
- 查看git版本,卸载旧版本(如果没有安装git请直接到下一步)
git --version
yum remove git
yum -y install git
服务器端创建 git 用户,用来管理 Git 服务,并为 git 用户设置密码
[root@localhost home]# id git
id: git:无此用户
[root@localhost home]# useradd git
[root@localhost home]# passwd git
服务器端创建 Git 仓库
设置 /home/data/git/gittest.git 为 Git 仓库
然后把 Git 仓库的 owner 修改为 gi
[root@localhost home]# mkdir -p data/git/gittest.git
[root@localhost home]# git init --bare data/git/gittest.git
Initialized empty Git repository in /home/data/git/gittest.git/
[root@localhost home]# cd data/git/
[root@localhost git]# chown -R git:git gittest.git/
客户端:
下载 Git for Windows
安装完之后,可以使用 Git Bash 作为命令行客户端。
安装完之后,查看 Git 版本
git --version
客户端 clone 远程仓库
然后从 Linux Git 服务器上 clone 项目:
git clone git@192.168.56.101:/home/data/gittest.git
如果SSH用的不是默认的22端口,则需要使用以下的命令(假设SSH端口号是7700):
git clone ssh://git@192.168.56.101:7700/home/data/gittest.git
当第一次连接到目标 Git 服务器时会得到一个提示:
The authenticity of host '192.168.56.101 (192.168.56.101)' can't be established.
RSA key fingerprint is SHA256:Ve6WV/SCA059EqoUOzbFoZdfmMh3B259nigfmvdadqQ.
Are you sure you want to continue connecting (yes/no)?
选择 yes:
Warning: Permanently added '192.168.56.101' (RSA) to the list of known hosts.
此时 C:\Users\用户名.ssh 下会多出一个文件 known_hosts,以后在这台电脑上再次连接目标 Git 服务器时不会再提示上面的语句。
后面提示要输入密码,可以采用 SSH 公钥来进行验证。
实现自动同步到站点目录(www):
就比如刚才我们往远程仓库推送了index.php文件,虽然提示推送成功,但是我们现在在服务器端还看不到效果,心理总是不爽。又比如我写了个html页面,我想在站点中马上看到,那自动同步就派上用场了。
自动同步功能用到的是 git 的钩子功能,
服务器端:进入裸仓库:/home/testgit/sample.git
cd /home/testgit/sample.git
cd hooks
//这里我们创建post-receive文件
vim post-receive
//在该文件里输入以下内容
!/bin/bash
git --work-tree=/home/www checkout -f
//保存退出后,将该文件用户及用户组都设置成git
chown git:git post-receive
//由于该文件其实就是一个shell文件,我们还应该为其设置可执行权限
chmod +x post-receive
现在我们可以在本地计算机中修改index.php文件,或者添加一个新文件,提交到远程仓库,然后到/home/www下面,看看有没有我们刚才提交的文件。
如果你在Git推送的工程中发现推送成功 但是在www目录下并没有自己的代码,这时候你可要注意了:这是由于文件夹的权限的原因造成的! 假设你的www目录的所属的用户组为root,你可以将你的git用户加入这个组;并给git添加写入权限,或者其他解决方法,反正你要服务器上的git用户有权限进入www文件夹。
作者:Mr_敬zZ
链接:https://www.jianshu.com/p/7a695fe06b18
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
第二步 创建 裸仓库
sudo git init --bare test.git
注意给test.git git用户权限 sudo chown -R git:git test.git
第三步 添加钩子文件
1、cd test.git/
2、cd hooks/
3、touch post-receive 钩子文件
第四步 编辑钩子shell脚本
vi post-receive 文件
第一种方式 使用 --work-tree --git-dir 实现
注:--work-tree 是服务器工作目录文件夹(实际运行的线上环境)
--git-dir 是裸仓库.git 文件夹
创建 --work-tree 文件夹时一定要添加git用户权限 否则 执行失败
#!/bin/bash
IS_BARE=$(git rev-parse --is-bare-repository)
# 如果不是远程裸仓库就瑞出
if [ -z "$IS_BARE" ]; then
echo >&2 "fatal: post-receive: IS_NOT_BARE"
exit 1
fi
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch to production..."
git --work-tree=/bydata/www/master --git-dir=/home/srv/test.git checkout -f 'master'
elif [[ $ref =~ .*/test$ ]];
then
echo "Test ref received. Deploying master branch to production..."
git --work-tree=/bydata/www/test --git-dir=/home/srv/test.git checkout -f 'test'
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
第二种方式
在服务创建工作环境 使用本地git pull代码方式实现
cd /bydata/www
// 创建线上环境 master 文件夹
git clone /home/srv/test.git master
// 创建测试环境文件夹 test
git clone -b test /home/srv/test.git test (如果有test分支就-b test 没有就删掉)
给这两个文件夹添加 git用户权限 否则会不成功
sudo chown -R git:git master/
#!/bin/bash
IS_BARE=$(git rev-parse --is-bare-repository)
# 如果不是远程裸仓库就瑞出
if [ -z "$IS_BARE" ]; then
echo >&2 "fatal: post-receive: IS_NOT_BARE"
exit 1
fi
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "Master ref received. Deploying master branch to production..."
cd /bydata/www/master
env -i git fetch --all
env -i git reset --hard origin/master
elif [[ $ref =~ .*/test$ ]];
then
echo "Test ref received. Deploying Test branch to production..."
cd /bydata/www/test
env -i git fetch --all
env -i git reset --hard origin/test
else
echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server."
fi
done
作者:你是一匹马
链接:https://www.jianshu.com/p/ab44938b2934
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?