安装最新版本的Git

使用yum安装不到最新版本,根据Git官网的提示,得知CentOS需要下载源码编译安装:

Red Hat Enterprise Linux, Oracle Linux, CentOS, Scientific Linux, et al.

RHEL and derivatives typically ship older versions of git. You can download a tarball and build from source, or use a 3rd-party repository such as the IUS Community Project to obtain a more recent version of git.

RHEL 和衍生产品通常提供旧版本的 git。 您可以下载 tarball 并从源代码构建,或使用第3方存储库(例如 IUS 社区项目)来获取新版本的 git。

本文记录时最新版本为2.37.3

1.安装依赖软件

[root@doracoin ~]# yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel asciidoc
[root@doracoin ~]# yum install gcc perl-ExtUtils-MakeMaker

在安装上面的依赖软件过程中,部分软件又会依赖git,所以此处如果先执行了yum remove git,则旧版本git(1.8.X)又会安装回来。所以应该先执行这个环节,再卸载旧版本的git。

2.卸载系统自带的低版本git(1.8.5.1)

[root@doracoin ~]# git --version
git version 1.8.5.1
[root@doracoin ~]# yum remove git

3.编译安装最新的git版本

[root@doracoin ~]# cd /usr/local/src/
[root@doracoin src]# wget https://www.kernel.org/pub/software/scm/git/git-2.37.3.tar.xz
[root@doracoin src]# tar -vxf git-2.37.3.tar.xz
[root@doracoin src]# cd git-2.37.3
[root@doracoin git-2.37.3]# ./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv
[root@doracoin git-2.37.3]# make && make install
[root@doracoin git-2.37.3]# echo "export PATH=$PATH:/usr/local/git/bin" >> /etc/profile
[root@doracoin git-2.37.3]# source /etc/profile

4.查看新的版本

[root@doracoin ~]# git --version
git version 2.37.3

注:如果是非root用户使用git,则需要配置下该用户下的环境变量

# 切换到该用户,执行以下命令
[git@doracoin ~]$ echo "export PATH=$PATH:/usr/local/git/bin" >> ~/.bashrc
[git@doracoin ~]$ source ~/.bashrc
[git@doracoin ~]$ git --version
git version 2.37.3

问题记录

undefined reference to `libiconv'

编译过程中,在执行make命令时:

make prefix=/usr/local/git all

可能会遇到以下错误:

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

libiconv官网 下载安装最新版本即可,本文操作时最新版为1.17

wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.17.tar.gz
tar -zxvf libiconv-1.17.tar.gz
cd libiconv-1.17
./configure --prefix=/usr/local/libiconv
make
make install

创建软链接到/usr/lib

ln -s /usr/local/lib/libiconv.so /usr/lib
ln -s /usr/local/lib/libiconv.so.2 /usr/lib

然后回到git目录继续编译

./configure --prefix=/usr/local/git --with-iconv=/usr/local/libiconv
make
make install

接下来git就可以顺利安装了

Required C99 support is in a test phase.

笔者在安装2.37.3版本时遇到了这个问题,根据源码 git-compat-util.h 中的描述:

#ifndef GIT_COMPAT_UTIL_H
#define GIT_COMPAT_UTIL_H

#if __STDC_VERSION__ - 0 < 199901L
/*
 * Git is in a testing period for mandatory C99 support in the compiler.  If
 * your compiler is reasonably recent, you can try to enable C99 support (or,
 * for MSVC, C11 support).  If you encounter a problem and can't enable C99
 * support with your compiler (such as with "-std=gnu99") and don't have access
 * to one with this support, such as GCC or Clang, you can remove this #if
 * directive, but please report the details of your system to
 * git@vger.kernel.org.
 */
#error "Required C99 support is in a test phase.  Please see git-compat-util.h for more details."
#endif

GoogleTranslate译文:

Git 处于编译器强制 C99 支持的测试期。 如果您的编译器相当新,您可以尝试启用 C99 支持(或者,对于 MSVC,C11 支持)。 如果您遇到问题并且无法启用 C99支持您的编译器(例如使用“-std=gnu99”)并且无权访问对于具有此支持的一个,例如 GCC 或 Clang,您可以删除此 #if指令,但请将您系统的详细信息报告给 git@vger.kernel.org

看起来是Git目前使用一项编译功能的新特性,解决办法是升级安装使用新版的gcc参考文章:升级gcc 解决make时c99错误

yum install centos-release-scl
yum install devtoolset-7-gcc*
scl enable devtoolset-7 bash
# 确认GCC程序位置和版本
which gcc
gcc --version

参考链接: