1. 用git --version命令检查是否已经安装

  1. 2

    在CentOS5的版本,由于yum源中没有git,所以需要预先安装一系列的依赖包。在CentOS6的yum源中已经有git的版本了,可以直接使用yum源进行安装。

  1. 3

    yum -y install git

    但是yum源中安装的git版本是1.7.1,Github等需要的Git版本最低都不能低于1.7.2 。所以我们一般不用上面的方法,而是下载git源码编译安装。

END

编译安装git

  1. 首先更新系统

    yum  -y update

    更新完成之后有6.5变成6.7了

  1. 安装依赖的包

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

  2. 3

    下载git源码并解压

    $ wget https://github.com/git/git/archive/v2.3.0.zip

    $ unzip v2.3.0.zip

    $ cd git-2.3.0

  1. 编译安装:

  2. 将其安装在“/usr/local/git”目录下。

  3. make prefix=/usr/local/git all

  4. make prefix=/usr/local/git install


如果编译时提示错误:
LINK git-credential-store
libgit.a(utf8.o): In function `reencode_string_iconv’:
/opt/git-master/utf8.c:530: undefined reference to `libiconv’
libgit.a(utf8.o): In function `reencode_string_len’:
/opt/git-master/utf8.c:569: undefined reference to `libiconv_open’
/opt/git-master/utf8.c:588: undefined reference to `libiconv_close’
/opt/git-master/utf8.c:582: undefined reference to `libiconv_open’
collect2: ld 返回 1
make: *** [git-credential-store] 错误 1
解决办法:
 
cd /usr/local/src/
wget http://ftp.gnu.org/pub/gnu/libiconv/libiconv-1.14.tar.gz
tar -zxvf libiconv-1.14.tar.gz
cd libiconv-1.14
./configure -prefix=/usr/local/libiconv  &&  make  && sudo  make install
然后回到git继续编译:
 

此处要进入的是git的解压后的目录
cd /usr/local/src/git-1.8.5

进行下面一些操作
make configure
./configure --prefix=/usr/local -with-iconv=/usr/local/libiconv
make
make install



  1. 编译完成之后使用git --version 查看git版本,居然还是1.7.1,这是因为它默认使用了"/usr/bin"下的git。

    你可以用下面的命令查看git所在的路径:

    $ whereis git

    git: /usr/bin/git /usr/local/git /usr/share/man/man1/git.1.gz

  1. 我们要把编译安装的git路径放到环境变量里,让它替换"/usr/bin"下的git。为此我们可以修改“/etc/profile”文件(或者/etc/bashrc文件)。

    vim /etc/profile

    然后在文件的最后一行,添加下面的内容,然后保存退出。

    export PATH=/usr/local/git/bin:$PATH

  1. 7

    不想重启系统,使用source命令立即生效

    source /etc/profile

  2. 8

    然后再次使用git --version 查看git版本,发现输出2.3.0,表明安装成功。

本文转自 IT阿飞 51CTO博客,原文链接:http://blog.51cto.com/itafei/1951074