如何在 Ubuntu 上安装最新版本的 Git
Git
在 Ubuntu 上安装 Git 非常容易。它存在于 [Ubuntu 的主仓库](https://itsfoss.com/ubuntu-repositories/)中，你可以像这样[使用 apt 命令](https://itsfoss.com/apt-command-guide/)安装它：



```
sudo apt install git
```

很简单？是不是？


只有一点点小问题（这可能根本不是问题），就是它安装的 [Git](https://git-scm.com/) 版本。


在 LTS 系统上，软件稳定性至关重要，这就是为什么 Ubuntu 18.04 和其他发行版经常提供较旧但稳定的软件版本的原因，它们都经过发行版的良好测试。


这就是为什么当你检查 Git 版本时，会看到安装的版本会比 [Git 网站上当前最新 Git 版本](https://git-scm.com/downloads)旧：



```
$ git --version
git version 2.17.1
```

在编写本教程时，网站上提供的版本为 2.25。那么，如何在 Ubuntu 上安装最新的 Git？


### 在基于 Ubuntu 的 Linux 发行版上安装最新的 Git


![](/data/attachment/album/202002/25/114909wvsxtp5gsyprx75s.png)


一种方法是[从源代码安装](https://itsfoss.com/install-software-from-source-code/)。这种很酷又老派的方法不适合所有人。值得庆幸的是，Ubuntu Git 维护团队提供了 [PPA](https://launchpad.net/%7Egit-core/+archive/ubuntu/ppa)，莫可以使用它轻松地安装最新的稳定 Git 版本。



```
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git
```

即使你以前使用 `apt` 安装了 Git，它也将更新为最新的稳定版本。



```
$ git --version
git version 2.25.0
```

[使用PPA](https://itsfoss.com/ppa-guide/) 的好处在于，如果发布了新的 Git 稳定版本，那么就可以通过系统更新获得它。[仅更新 Ubuntu](https://itsfoss.com/update-ubuntu/) 来获取最新的 Git 稳定版本。


### 配置 Git （推荐给开发者）


如果你出于开发目的安装了 Git，你会很快开始克隆仓库，进行更改并提交更改。


如果你尝试提交代码，那么你可能会看到 “Please tell me who you are” 这样的错误：



```
$ git commit -m "update readme"

*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'abhishek@itsfoss.(none)')
```

这是因为你还没配置必要的个人信息。


正如错误已经暗示的那样，你可以像这样设置全局 Git 配置：



```
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
```

你可以使用以下命令检查 Git 配置：



```
git config --list
```

它应该显示如下输出：



```
user.email=you@example.com
user.name=Your Name
```

配置保存在 `~/.gitconfig` 中。你可以手动修改配置。


### 结尾


我希望这个小教程可以帮助你在 Ubuntu 上安装 Git。使用 PPA，你可以轻松获得最新的 Git 版本。


如果你有任何疑问或建议，请随时在评论部分提问。也欢迎直接写“谢谢” :)




---


via: <https://itsfoss.com/install-git-ubuntu/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

Installing Git on Ubuntu is very easy. It is available in the [main repository of Ubuntu](https://itsfoss.com/ubuntu-repositories/) and you can install it [using the apt command](https://itsfoss.com/apt-command-guide/) like this:

`sudo apt install git`


Easy? Isn’t it?

There is only a slight little problem (which might not be a problem at all) and that is the version of [Git](https://git-scm.com/?ref=itsfoss.com) it installs.

On an LTS system, software stability is of utmost importance this is why Ubuntu 18.04 and other distributions often provide older but stable versions of software that are well-tested with the distribution release.

This is why when you check the Git version, you’ll see that it installs a version which is older than the [current Git version available on Git project’s website](https://git-scm.com/downloads?ref=itsfoss.com):

```
abhishek@itsfoss:~$ git --version
git version 2.17.1
```


At the time of writing this tutorial, the version available on its website is 2.25. So how do you install the latest Git on Ubuntu then?

## Install latest Git on Ubuntu-based Linux distributions

One way would be to [install from source code](https://itsfoss.com/install-software-from-source-code/). That cool, old school method is not everyone’s cup of tea. Thankfully, there is a [PPA available from Ubuntu Git Maintainers team](https://launchpad.net/~git-core/+archive/ubuntu/ppa?ref=itsfoss.com) that you can use to easily install the latest stable Git version.

```
sudo add-apt-repository ppa:git-core/ppa
sudo apt update
sudo apt install git
```


Even if you had installed Git using apt previously, it will get updated to the latest stable version.

```
abhishek@itsfoss:~$ git --version
git version 2.25.0
```


The beauty of [using PPA](https://itsfoss.com/ppa-guide/) is that if there is a new stable version of Git released, you’ll get it with the system updates. [Just update Ubuntu](https://itsfoss.com/update-ubuntu/) to get the latest Git stable version.

[Linux creator Linus Torvalds](https://itsfoss.com/linus-torvalds-facts/)?

## Configure Git [Recommended for developers]

If you have installed Git for development purposes, you’ll soon start cloning repos, make your changes and commit your change.

If you try to commit your code, you may see a ‘Please tell me who you are’ error like this:

```
abhishek@itsfoss:~/compress-pdf$ git commit -m "update readme"
*** Please tell me who you are.
Run
git config --global user.email "
```[[email protected]](/cdn-cgi/l/email-protection)"
git config --global user.name "Your Name"
to set your account's default identity.
Omit --global to set the identity only in this repository.
fatal: unable to auto-detect email address (got 'abhishek@itsfoss.(none)')


This is because you haven’t configured Git with your personal information which is mandatory.

As the error already hints, you can set up global Git configuration like this:

```
git config --global user.name "Your Name"
git config --global user.email "
```[[email protected]](/cdn-cgi/l/email-protection)"


You can check the Git configuration with this command:

`git config --list`


It should show an output like this:

[[email protected]](/cdn-cgi/l/email-protection)
user.name=abhishek


This configuration is stored in ~/.gitconfig file. You may also change it manually to change the configuration.

## Start learning Git

If you are new to Git, here's a quick primer into [basic Git commands](https://itsfoss.com/basic-git-commands-cheat-sheet/).

[Basic Git Commands You Must Know [Download Cheat Sheet]This quick guide shows you all the basic Git commands and their usage. You can also download these commands for quick reference.](https://itsfoss.com/basic-git-commands-cheat-sheet/)![](https://itsfoss.com/content/images/wordpress/2016/06/Download-Git-Sheet.jpg)


![](https://itsfoss.com/content/images/wordpress/2016/06/Download-Git-Sheet.jpg)

I hope this quick little tutorial helped you to install Git on Ubuntu. With the PPA, you easily get the latest Git version.

If you have any questions or suggestions, please feel free to ask in the comment section. A quick ‘thank you’ is also welcomed :)