如何在 CentOS 6.x/7.x 上安装 Git 及最新版

Git 是一个分布式版本控制软件,最初由林纳斯·托瓦兹创作,于 2005 年以 GPL 发布。最初目的是为更好地管理 Linux 内核开发而设计。

更多介绍,详见:

方式一、yum安装

➜  yum info git
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Available Packages
Name : git
Arch : x86_64
Version : 1.8.3.1 <--- 说明:当前系统是 CentOS7.x,在 6.x 上是 1.7.1
Release : 12.el7_4
Size : 4.4 M
Repo : updates/7/x86_64
Summary : Fast Version Control System
URL : http://git-scm.com/
License : GPLv2
Description : Git is a fast, scalable, distributed revision control system with an
: unusually rich command set that provides both high-level operations
: and full access to internals.
:
: The git rpm installs the core tools with minimal dependencies. To
: install all git packages, including tools for integrating with other
: SCMs, install the git-all meta-package.

# 为了安装较新版本,需要手动添加一个仓库源
➜ yum install -y http://opensource.wandisco.com/centos/6/git/x86_64/wandisco-git-release-6-1.noarch.rpm
# 或
➜ yum install -y http://opensource.wandisco.com/centos/7/git/x86_64/wandisco-git-release-7-2.noarch.rpm

# 开始安装
➜ yum install -y git

方式二、源码包安装

提示:请使用 root 用户操作

2.1. 安装依赖包

➜  yum install -y curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUtils-MakeMaker

2.2. 卸载旧的git版本(如果之前有安装rpm包)

➜  yum remove -y git

2.3. 下载&解压

源码文件(当前最新版本 **v2.29.2 ** @ 2020年12月14日

cd ~
➜ wget -c https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.29.2.tar.gz
➜ tar -zxvf git-2.29.2.tar.gz

2.4. 编译安装

cd git-2.29.2
➜ ./configure --prefix=/usr/local/git
➜ make && make install
echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/bashrc
source /etc/bashrc

2.5. 检查git版本

➜  git --version
git version 2.5.0

⚠️ 注意:如果安装完查看版本不是我们安装的最新版,请重新执行下面的操作

➜  yum remove -y git
source /etc/bashrc
➜ git --versione

参考: