1. 默认安装(不推荐)

执行命令:

yum install git

安装位置是:

/usr/libexec/git-core

卸载默认安装的 git:

yum remove git

2. 自定义安装(推荐)

2.1 下载压缩包

进入 git 官网下载: https://git-scm.com/download/linux

image.png

点击 on kernel.org 找到压缩包,下载最新版本 2.45.0。

image.png

下载后的压缩包:

image.png

2.2 上传到 linux 服务器

创建个 git 文件夹,上传压缩包这个文件夹。

image.png

2.3 解压压缩包
[root@zichen ~]# cd /git/
[root@zichen git]# ls
git-2.45.0.tar.gz
[root@zichen git]# tar -zxvf git-2.45.0.tar.gz
2.4 安装 git

进入解压后的文件夹 git-2.45.0,执行命令配置指定的安装路径,比如 /git/git

[root@zichen git]# cd git-2.45.0/
[root@zichen git-2.45.0]# ./configure --prefix=/git/git

然后进行编译安装

[root@zichen git-2.45.0]# make
[root@zichen git-2.45.0]# make install

查看 git 版本

[root@zichen /]# cd /git/git/bin
[root@zichen bin]# ls
git  git-cvsserver  gitk  git-receive-pack  git-shell  git-upload-archive  git-upload-pack  scalar
[root@zichen bin]# ./git --version
git version 2.45.0
2.5 配置环境变量

方法一:

编辑环境变量:

[root@zichen ~]# vim /etc/profile

修改 export PATH

image.png

修改为:export PATH=$PATH:/git/git/bin,将 /git/git/bin 目录添加到 PATH 环境变量中。

image.png

重新加载 /etc/profile 脚本,使修改生效。

[root@zichen ~]# source /etc/profile
[root@zichen ~]# git --version
git version 2.45.0

方法二:

添加软连接,将git软连接到/usr/bin/目录下

ln -s /git/git/bin/git /usr/bin/