如何在 Ubuntu 上安裝最新版本的 Git

Abhishek Prakash發表於2020-02-25

在 Ubuntu 上安裝 Git 非常容易。它存在於 Ubuntu 的主倉庫中,你可以像這樣使用 apt 命令安裝它:

sudo apt install git

很簡單?是不是?

只有一點點小問題(這可能根本不是問題),就是它安裝的 Git 版本。

在 LTS 系統上,軟體穩定性至關重要,這就是為什麼 Ubuntu 18.04 和其他發行版經常提供較舊但穩定的軟體版本的原因,它們都經過發行版的良好測試。

這就是為什麼當你檢查 Git 版本時,會看到安裝的版本會比 Git 網站上當前最新 Git 版本舊:

$ git --version
git version 2.17.1

在編寫本教程時,網站上提供的版本為 2.25。那麼,如何在 Ubuntu 上安裝最新的 Git?

在基於 Ubuntu 的 Linux 發行版上安裝最新的 Git

一種方法是從原始碼安裝。這種很酷又老派的方法不適合所有人。值得慶幸的是,Ubuntu Git 維護團隊提供了 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 的好處在於,如果釋出了新的 Git 穩定版本,那麼就可以通過系統更新獲得它。僅更新 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 選題:lujun9972 譯者:geekpi 校對:wxy

本文由 LCTT 原創編譯,Linux中國 榮譽推出

如何在 Ubuntu 上安裝最新版本的 Git

訂閱“Linux 中國”官方小程式來檢視

相關文章