Git在Linux环境安装

第一节我们提到过 Git 的前世故事,它的诞生离不开 Linux。最初 Git 也只支持 Linux 和 unix 系统安装使用,后来慢慢扩展到了 windows 等其他系统上。要想学习后续 Git 的知识,必须先安装 Git 环境。本节课我们就来讲解下 Git 在 Linux 系统上的安装以及一些相关知识点,听我慢慢道来。

安装前可以先查看下自己的系统是否已经安装 Git,像下面这样简单地输入一个 git 命令,如果像这样,就是没有安装。

[root@localhost ~]# git
bash: git: command not found

本节我主要基于 CentOS7 系统版本演示安装流程,大家在安装的时候一定要找到自己系统版本对应的安装方法,有些地方可能会有区别。那么,接下来我给大家介绍其中两种安装方法。

1. 通过 Linux 自带的包管理工具安装

这种方式需要我们先查看对应的 Linux 版本,通过以下命令可以查看:

cat /proc/version

不同的版本对应不同的包管理工具,它们基本上都是一键安装 git ,非常方便,下面分别介绍:

1.1 使用 yum install -y git 命令安装

如果你的版本是 Red Hat,也就是我们所说的红帽,那么可以使用如下命令安装:

yum install -y git

安装完毕,需要配置 Git 全局标识:

git config --global user.name "Your Name"
git config --global user.email "Your Email"

将 “Your Name” 和 “Your Email” 替换为你的用户名和邮箱即可。

附:安装步骤视频。

可以看到,我的版本是 Red Hat 4.8.5-39。

图片描述


我将安装操作录了视频,如下:

1.2. 使用 apt-get 命令安装

如果是 Debian 或 Ubuntu Linux 的话,执行一条 sudo apt-get install git 就可以直接完成 Git 的安装,也非常简单。

当然,安装完毕后别忘了同样也要配置身份信息。

2. 使用 git 源码安装包进行安装

一般情况下我们通过前两种方法就可以很快安装成功,源码安装稍显麻烦,如果有兴趣的可以参考如下步骤:
这种方法需要自己下载安装包、解压、编译等一步一步进行操作,我们一起看下:
(1) 先下载 git 依赖的包

[root@localhost opt]# yum -y install zlib-devel openssl-devel perl cpio expat-devel gettext-devel

(2) wget 下载 git 安装包

[root@localhost opt]# wget https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
--2020-09-03 17:38:22--  https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.9.5.tar.gz
正在解析主机 mirrors.edge.kernel.org... 147.75.95.133, 2604:1380:3000:1500::1
Connecting to mirrors.edge.kernel.org|147.75.95.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:5928730 (5.7M) [application/x-gzip]
Saving to: `git-2.9.5.tar.gz'




100%[======================================>] 5,928,730   1.43M/s   in 5.0s    




2020-09-03 17:38:29 (1.13 MB/s) - `git-2.9.5.tar.gz' saved [5928730/5928730]

出现以上信息则说明下载成功,我们看下当前目录是否有下载的 tar 包。
(3)查看当前目录的文件

[root@localhost opt]# ls
git-2.9.5.tar.gz

git-2.9.5.tar.gz 就是我们下载的 git 安装包,说明已经下载成功。

(4)解压压缩包
接下来需要将压缩包解压,执行如下命令:

[root@localhost opt]# tar zxvf git-2.9.5
[root@localhost opt]# ls
git-2.9.5  git-2.9.5.tar.gz

可以看到,解压后当前目录已经多了一个解压后的文件夹,之后我们都在这个文件夹进行安装操作。
(5)安装 autoconf

[root@localhost opt]# yum install autoconf

(6)进入安装文件目录,执行 autoconf:

[root@localhost opt]# cd git-2.9.5
[root@localhost git-2.9.5]# autoconf

(7)继续执行如下命令:

[root@localhost git-2.9.5]# ./configure --with-curl=/usr/local
configure: Setting lib to 'lib' (the default)
configure: Will try -pthread then -lpthread to enable POSIX Threads.
configure: CHECKS for site configuration
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/opt/git-2.9.5':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

(8)执行编译:
由于 Git 是使用 C 写的,因此需要进行编译操作。

[root@localhost git-2.9.5]# make
    CC credential-store.o
/bin/sh: cc: command not found
make: *** [credential-store.o] 错误 127

咦!好像报错了,这是怎么回事?
别慌,由报错信息可以知道缺少某个执行文件。我们需要继续安装 gcc 等相关包,才可以执行 make。那么我们继续安装:
(9)安装 C 环境

[root@localhost git-2.9.5]# yum -y install gcc gcc-c++ libstdc++-devel

(10)重新执行 make 命令即可成功。

[root@localhost git-2.9.5]# make
[root@localhost git-2.9.5]# make install

(11)编译完成后,我们用 git 命令验证下是否安装成功

[root@localhost git-2.9.5]# git --version
git version 2.9.5

现在提示正常,显示当前 Git 版本是 2.9.5,也就是我们安装的版本。至此,说明已经安装成功。

但是,别忘了配置当前用户身份标识,因为每个 Git 提交都使用此信息,并且将它永久地记录到您开始创建的提交中。
(12)配置身份信息

[root@localhost git-2.9.5]# git config --global user.name "Your Name"
[root@localhost git-2.9.5]# git config --global user.email "Your Email"
   <font color=#DC143C>将Your Name 和Your Email分别用你自己的名称和邮箱替换即可。</font>

到这里,CentOS7 操作系统 Git 的安装就已经结束了。

3. 总结

本节课的知识点,可以总结为 “两点一注意”:
两点:Linux 环境下其中两种 Git 的安装方式:

  • 通过包管理工具命令安装。
  • 下载 git 源码进行安装。

注意:无论哪种方式进行安装完毕后,都不要忘了配置身份信息!