在个人桌面虚拟机软件(如 VirtualBox)或是云平台上的虚拟机,我通常使用 CentOS 7 Minimal 系统。系统自带有一个版本比较老的 git-1.8,使用体验上与新版本相差甚远。所以这里会记录下如何替换/安装比较新的版本。
当前最新版本总是可以从源码编译安装。下面的 bash 脚本,可以实现移除旧版本的 git,并安装最新的版本,截至文章当前日期(2021-05-08)版本为 2.31.1
。在不提供给脚本一个版本参数时,会默认安装 2.31.1
。
#!/bin/bash
#
# Install git from source.
#
# Arguments:
# $1: git version number, eg: 2.31.1
set -o pipefail
set -o errtrace
# check if git already exists
if [ -x "$(command -v git)" ]; then
git_version_tmp=$(git --version)
if [[ ${git_version_tmp} == *" 1."* ]]; then
echo Remove legacy ${git_version_tmp} firstly
yum remove -y git
elif [[ ${git_version_tmp} == *" 2."* ]]; then
echo ${git_version_tmp} already installed
exit 0
else
echo Unknow, nothing to do
exit 0
fi
fi
GIT_VERSION=2.31.1
if [ "$1" != "" ]; then
GIT_VERSION=$1
fi
set -o nounset
echo Work in dir /opt/git
mkdir -p /opt/git
cd /opt/git
yum install -y gcc autoconf curl-devel openssl-devel zlib-devel bash-completion
if [ ! -f git-${GIT_VERSION}.tar.xz ]; then
curl -LO https://www.kernel.org/pub/software/scm/git/git-${GIT_VERSION}.tar.xz
echo git-${GIT_VERSION}.tar.xz was downloaded in /opt/git
fi
tar -xf git-${GIT_VERSION}.tar.xz
pushd git-${GIT_VERSION} > /dev/null
make configure
./configure --prefix=/usr
make all
make install
tee /etc/profile.d/git.sh <<-'EOF'
# git aliases
alias gs='git status'
alias gd='git diff'
alias gc='git checkout'
alias gb='git branch -va'
alias gps='git push'
alias gpl='git pull'
alias gl='git log'
alias glp='git log --pretty=format:"%h %s" --graph'
alias grprune='git remote prune origin'
alias gcmail='git config user.email'
alias gcname='git config user.name'
EOF
cp contrib/completion/git-completion.bash /etc/bash_completion.d/
popd > /dev/null
# clean temp work dir
rm -rf git-${GIT_VERSION}
echo $(git --version) installed
这个脚本也可从 dnposts git-install.sh 获取到。使用时,可以不提供参数直接执行,会安装默认的 2.31.1
版本:
./git-install.sh
或者显式指定一个版本号,如:
./git-install.sh 2.31.1
注: 这里没有安装 doc、html、info,如需安装,需要先安装其他一些依赖,参见官方文档:
make install install-doc install-html install-info
从源码编译安装需要预先安装一些依赖,这通常不是问题,不过“强迫症”可能更希望不安装这些编译期的工具。下面记录从两个可用的第三方 yum 源安装 git。
IUS Community Project 是官方推荐的一个源,安装方式:
yum install https://repo.ius.io/ius-release-el7.rpm
可以从 https://repo.ius.io/7/x86_64/packages/g/ 查看当前比较新的 git 版本。如当前比较新为 git236,通过 yum 直接安装:
yum install git236
End Point Software Package Repositories 也是一个不错的三方源,安装方式:
yum install https://packages.endpoint.com/rhel/7/os/x86_64/endpoint-repo-1.9-1.x86_64.rpm
相比 IUS,这个源中的 git 没有特殊命名。通过 yum 直接安装:
yum install git
你倾向于哪一种?
参考:
评论 / 共 0 条