系统环境

Centos 7.3

如果已安装git(Centos 7版本自带git 1.8.3.1),安装新版本前先卸载:yum remove git

一、源码安装(推荐):

使用源代码可以安装最新版本,有些 Linux 版本自带的安装包更新起来并不及时,所以除非你在用最新的 distro 或者 backports,那么从源代码安装其实该算是最佳选择。

git 的工作需要调用 curlzlibopensslexpatlibiconv 等库的代码,所以需要先安装这些依赖工具。可使用下面的命令安装:

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

然后,访问git官方站点下载最新版本源码:

https://git-scm.com/download/linux

或者直接访问以下地址获取源码包:

https://www.kernel.org/pub/software/scm/git/

安装:

# 下载
wget https://www.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz

# 解压
tar -zxvf git-2.9.5.tar.gz

# 进入解压后目录
cd git-2.9.5.tar.gz

# 安装
make prefix=/usr/local/git all 
sudo make prefix=/usr/local/git install

# 配置环境变量
echo "export PATH=/usr/local/git/bin:$PATH" >> /etc/profile
source /etc/profile

检查版本:

git --version

如果安装过程报类似以下错误:

libgit.a(utf8.o): In function `reencode_string_iconv':
/root/software/git-2.9.5/utf8.c:463: undefined reference to `libiconv'
libgit.a(utf8.o): In function `reencode_string_len':
/root/software/git-2.9.5/utf8.c:502: undefined reference to `libiconv_open'
/root/software/git-2.9.5/utf8.c:521: undefined reference to `libiconv_close'
/root/software/git-2.9.5/utf8.c:515: undefined reference to `libiconv_open'
collect2: error: ld returned 1 exit status
make: *** [git-credential-store] Error 1

解决:

# 其他版本可访问:https://ftp.gnu.org/pub/gnu/libiconv/ 获取
wget https://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.15.tar.gz  
tar -zxvf libiconv-1.15.tar.gz
cd libiconv-1.15
./configure --prefix=/usr/local/libiconv
make 
make install

继续安装git并指定iconv的位置:--with-iconv=/usr/local/libiconv

二、二进制安装包安装

安装:

yum install git

检查版本:

git --version

参考

Git - 安装Git

文章目录