Centos7 源码安装git

背景介绍

由于运行gitlab pipeline job遇到如下错误

fatal: git fetch-pack: expected shallow list
ERROR: Job failed: exit status 1

网上搜索原因, 是因为git的版本过低, 经查询确认, 确实很低还是1.x的git版本. 想通过yum源升级, 发现yum源上的最新版也才1.8.3.1

到目前2021-12-14为止要在centos7上安装2.x的 git还是要通过源码安装, 以下是源码安装git的全过程.

博客更新

安装完成后发现这里有第三方提供的rpm包 可以下载, 所以现在可以有另一种安装方式可供选择了 即rpm包安装
https://pkgs.org/download/git

下载gitlab源码

cd /opt
wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.22.0.tar.gz

这里我下载的是2.22.0版本的,如果要下载其他版本,把URL后面的版本号修改即可.
如果提示cannot verify mirrors.edge.kernel.org's certificate, 参考 troubleshooting章节.

解压、编译、安装gIt

解压源码包

tar -xvf git-2.22.0.tar.gz

编译
进入解压后的源码包, 编译源码
编译前需要安装依赖包

yum -y install openssl-devel curl-devel expat-devel

编译安装

./configure ./configure --prefix=/usr/local
make prefix=/usr/local all
make prefix=/usr/local install

注意:

  1. 这里需要指定prefix, 如果不制定默认会安装到${HOME} 目录下.

  2. 如果你知道如何使用checkinstall, 可以尝试使用checkinstall进行安装, 可以参考这篇文章
    永远不要通过运行将任何东西安装到系统中 make install!如今,大多数makefile都不支持“卸载”目标,因为它们习惯于安装到专用的暂存位置进行测试或制作软件包(.rpm,.deb等),然后软件包管理器负责清理。如果需要安装某些软件,请尝试找到一个官方软件包,或者尝试从较新版本的操作系统中向后移植另一个官方软件包(如果有)。作为最后的选择,请尝试使用checkinstall尝试在运行时创建二进制包的工具make install。这很烂,但仍然比光秃秃的要好make install。

如何卸载

很多教程都只讲述了如何安装, 但是知道如何安全,彻底的卸载也很重要. 比如我在安装时,使用了默认的安装目录, git被安装到了/root/git/bin下面, 不是很理想,我希望git 被安装到/usr/local目录下. 所以需要卸载, 重新安装. 此时了解卸载对安装过程,以及安装过程中具体做了些什么很有好处. 以下就是卸载过程.

卸载git

这是从Makefile截取的一段代码, 从这段代码大致可以看出,git相关的文件路径
二进制文件应该都在 $(prefix)/bin, 由于我没有指定prefix,应该使用的是默认prefix 比如我之前没有指定prefix所以默认prefix应为$(HOME) 即/root

prefix = $(HOME)                              #/root                                                                     
bindir = $(prefix)/bin                        #/root/bin                                                                  
mandir = $(prefix)/share/man                  #/root/share/man                                                                 
infodir = $(prefix)/share/info                #/root/share/info                                                                    
gitexecdir = libexec/git-core                                                                         
mergetoolsdir = $(gitexecdir)/mergetools      # /root/libexec/git-core/mergetools                                                           
sharedir = $(prefix)/share                    # /root/share                                                            
gitwebdir = $(sharedir)/gitweb                # /root/share/gitweb                                                       
perllibdir = $(sharedir)/perl5                # /root/share/perl5                                                   
localedir = $(sharedir)/locale                # /root/share/locale                                                  
template_dir = share/git-core/templates                                                                
htmldir = $(prefix)/share/doc/git-doc         # /root/share/doc/git-doc                                                   
ETC_GITCONFIG = $(sysconfdir)/gitconfig       # /etc/gitconfig                                                     
ETC_GITATTRIBUTES = $(sysconfdir)/gitattributes # /etc/gitattributes

所以大致可以确定设计到的目录和文件有以下几个, 如果你没有使用源码方式安装过其他软件, 基本上这些目录下都是git相关的文件, 如果你以源码方式安装过其他软件, 请小心卸载, 以免影响其他程序.
通过grep Makefile得到
gitexec_instdir = $(prefix)/$(gitexecdir) 实际值为/root/libexec/git-core
$(sysconfdir)应该等于/etc
所以在没有源码安装其他软件的前提下,再次强调是没有没有源码安装其他软件的前提下, 如果有其它软件干扰请谨慎移除.
应该移除以下目录和文件为:
/root/bin 目录

$tree /root/bin
/root/bin
├── git
├── git-cvsserver
├── gitk
├── git-receive-pack
├── git-shell
├── git-upload-archive
└── git-upload-pack

/root/share 目录

/root/share
├── git-core
├── git-gui
├── gitk
├── gitweb
├── locale
└── perl5

/root/libexec 目录

/root/libexec/
└── git-core

/etc/gitconfig 文件
/etc/gitattributes 文件

问题排查

--2021-12-14 21:38:58--  https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.22.0.tar.gz
Resolving mirrors.edge.kernel.org (mirrors.edge.kernel.org)... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org (mirrors.edge.kernel.org)|147.75.95.133|:443... connected.
ERROR: cannot verify mirrors.edge.kernel.org's certificate, issued by ‘/C=US/O=Let's Encrypt/CN=R3’:
  Issued certificate has expired.

错误:无法验证Gcc.GNU.org的证书,由'/C= U/O==让我们加密/CN=让加密权限X3’:
无法在本地验证发行者的权限。

为了安全地连接到GC.GNU.ORG,请使用“无检查证书”。
此时使用以下命令即可
[root@gaojingbo yum.repos.d]# yum install -y ca-certificates

参考文章

Centos7安装Git2.x + Apache 搭建Git Http Server

Centos 7.2安装git和卸载git

转载请注明出处, 更多博文请访问https://www.cnblogs.com/guoapeng/
原文地址:https://www.cnblogs.com/guoapeng/p/15689846.html