当前位置:   article > 正文

如何在Ubuntu 20.04上安装Git

ubuntu 20.04 安装 gitserver

介绍 (Introduction)

Version control systems like Git are essential to modern software development best practices. Versioning allows you to keep track of your software at the source level. You can track changes, revert to previous stages, and branch to create alternate versions of files and directories.

像Git这样的版本控制系统对于现代软件开发最佳实践至关重要。 版本控制使您可以在源代码级别跟踪软件。 您可以跟踪更改,还原到上一个阶段以及分支以创建文件和目录的备用版本。

Many software projects’ files are maintained in Git repositories, and platforms like GitHub, GitLab, and Bitbucket help to facilitate software development project sharing and collaboration.

许多软件项目的文件都保存在Git存储库中,而GitHub,GitLab和Bitbucket等平台则有助于促进软件开发项目的共享和协作。

In this guide, we will go through how to install and configure Git on an Ubuntu 20.04 server. We will cover how to install the software two different ways: via the built-in package manager, and via source. Each of these approaches come with their own benefits depending on your specific needs.

在本指南中,我们将介绍如何在Ubuntu 20.04服务器上安装和配置Git。 我们将介绍如何以两种不同的方式安装软件:通过内置的软件包管理器和通过source 。 这些方法中的每一种都有自己的优点,具体取决于您的特定需求。

先决条件 (Prerequisites)

You will need an Ubuntu 20.04 server with a non-root superuser account.

您将需要具有非超级用户超级帐户的Ubuntu 20.04服务器。

To set this up, you can follow our Initial Server Setup Guide for Ubuntu 20.04.

要进行设置,可以遵循我们的《适用于Ubuntu 20.04的初始服务器设置指南》

With your server and user set up, you are ready to begin.

设置好服务器和用户后,就可以开始了。

使用默认软件包安装Git (Installing Git with Default Packages)

The option of installing with default packages is best if you want to get up and running quickly with Git, if you prefer a widely-used stable version, or if you are not looking for the newest available functionalities. If you are looking for the most recent release, you should jump to the section on installing from source.

如果要使用Git快速启动并运行,如果您希望使用广泛使用的稳定版本,或者如果您不寻找最新的可用功能,则最好使用默认软件包进行安装。 如果您正在寻找最新版本,则应跳至有关从源代码安装的部分。

Git is likely already installed in your Ubuntu 20.04 server. You can confirm this is the case on your server with the following command:

您的Ubuntu 20.04服务器中可能已经安装了Git。 您可以使用以下命令在服务器上确认这种情况:

  • git --version

    git --version

If you receive output similar to the following, then Git is already installed.

如果收到类似以下的输出,则说明已经安装了Git。

  1. Output
  2. git version 2.25.1

If this is the case for you, then you can move onto setting up Git, or you can read the next section on how to install from source if you need a more up-to-date version.

如果您是这种情况,则可以继续设置Git ,或者如果需要最新版本,可以阅读下一节有关如何从源代码安装的内容

However, if you did not get output of a Git version number, you can install it with the Ubuntu default package manager APT.

但是,如果未获得Git版本号的输出,则可以使用Ubuntu默认软件包管理器APT安装它。

First, use the apt package management tools to update your local package index.

首先,使用apt软件包管理工具更新您的本地软件包索引。

  • sudo apt update

    sudo apt更新

With the update complete, you can install Git:

更新完成后,您可以安装Git:

  • sudo apt install git

    sudo apt安装git

You can confirm that you have installed Git correctly by running the following command and checking that you receive relevant output.

您可以通过运行以下命令并检查是否收到相关输出来确认已正确安装了Git。

  • git --version

    git --version
  1. Output
  2. git version 2.25.1

With Git successfully installed, you can now move on to the Setting Up Git section of this tutorial to complete your setup.

成功安装Git后,您现在可以转到本教程的“ 设置Git”部分以完成设置。

从源代码安装Git (Installing Git from Source)

If you’re looking for a more flexible method of installing Git, you may want to compile the software from source, which we will go over in this section. This takes longer and will not be maintained through your package manager, but it will allow you to download the latest release and will give you greater control over the options you include if you wish to make customizations.

如果您正在寻找一种更灵活的Git安装方法,则可能需要从源代码编译该软件,我们将在本节中进行介绍。 这将花费更长的时间,并且不会通过软件包管理器进行维护,但是它将允许您下载最新版本,并且如果希望进行自定义,则可以更好地控制您包括的选项。

Verify the version of Git currently installed on the server:

验证服务器上当前安装的Git版本:

  • git --version

    git --version

If Git is installed, you’ll receive output similar to the following:

如果安装了Git,您将收到类似于以下内容的输出:

  1. Output
  2. git version 2.25.1

Before you begin, you need to install the software that Git depends on. This is all available in the default repositories, so we can update our local package index and then install the relevant packages.

在开始之前,您需要安装Git依赖的软件。 这些都在默认存储库中可用,因此我们可以更新本地软件包索引,然后安装相关软件包。

  • sudo apt update

    sudo apt更新
  • sudo apt install libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc

    sudo apt安装libz-dev libssl-dev libcurl4-gnutls-dev libexpat1-dev gettext cmake gcc

After you have installed the necessary dependencies, create a temporary directory and move into it. This is where we will download our Git tarball.

安装必要的依赖项后,创建一个临时目录并移入该目录。 这是我们将下载Git tarball的地方。

  • mkdir tmp

    mkdir tmp
  • cd /tmp

    cd / tmp

From the Git project website, we can navigate to the tarball list available at https://mirrors.edge.kernel.org/pub/software/scm/git/ and download the version you would like. At the time of writing, the most recent version is 2.26.2, so we will download that for demonstration purposes. We’ll use curl and output the file we download to git.tar.gz.

Git项目网站上 ,我们可以导航到https://mirrors.edge.kernel.org/pub/software/scm/git/上的压缩包列表,然后下载所需的版本。 在撰写本文时,最新版本是2.26.2,因此我们将下载该版本以进行演示。 我们将使用curl并将下载的文件输出到git.tar.gz

  • curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git-2.26.2.tar.gz

    curl -o git.tar.gz https://mirrors.edge.kernel.org/pub/software/scm/git/git- 2.26.2 .tar.gz

Unpack the compressed tarball file:

解压缩压缩的tarball文件:

  • tar -zxf git.tar.gz

    tar -zxf git.tar.gz

Next, move into the new Git directory:

接下来,进入新的Git目录:

  • cd git-*

    cd git- *

Now, you can make the package and install it by typing these two commands:

现在,您可以通过键入以下两个命令来制作软件包并进行安装:

  • make prefix=/usr/local all

    使prefix = / usr / local全部
  • sudo make prefix=/usr/local install

    sudo make prefix = / usr /本地安装

Now, replace the shell process so that the version of Git we just installed will be used:

现在,替换外壳程序,以便使用我们刚安装的Git版本:

  • exec bash

    执行重击

With this complete, you can be sure that your install was successful by checking the version.

完成此操作后,您可以通过检查版本来确保安装成功。

  • git --version

    git --version
  1. Output
  2. git version 2.26.2

With Git successfully installed, you can now complete your setup.

成功安装Git后,您现在可以完成设置。

设置Git (Setting Up Git)

After you are satisfied with your Git version, you should configure Git so that the generated commit messages you make will contain your correct information and support you as you build your software project.

对Git版本满意后,应配置Git,以便生成的提交消息将包含正确的信息,并在构建软件项目时为您提供支持。

Configuration can be achieved by using the git config command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do. We can go ahead and add this information by typing:

可以使用git config命令来完成git config 。 具体来说,我们需要提供我们的姓名和电子邮件地址,因为Git会将这些信息嵌入到我们所做的每次提交中。 我们可以继续输入以下内容来添加此信息:

  • git config --global user.name "Your Name"

    git config --global user.name“ 您的名字 ”

  • git config --global user.email "youremail@domain.com"

    git config --global user.email“ youremail@domain.com ”

We can display all of the configuration items that have been set by typing:

我们可以通过键入以下内容显示已设置的所有配置项:

  • git config --list

    git config --list
  1. Output
  2. user.name=Your Name
  3. user.email=youremail@domain.com
  4. ...

The information you enter is stored in your Git configuration file, which you can optionally edit by hand with a text editor of your choice like this (we’ll use nano):

输入的信息存储在Git配置文件中,您可以选择使用这样选择的文本编辑器手动编辑该文件(我们将使用nano):

  • nano ~/.gitconfig

    纳米〜/ .gitconfig
~/.gitconfig contents
〜/ .gitconfig内容
  1. [user]
  2. name = Your Name
  3. email = youremail@domain.com

Press CTRL and X, then Y then ENTER to exit the text editor.

CTRLX ,然后按Y再按ENTER退出文本编辑器。

There are many other options that you can set, but these are the two essential ones needed. If you skip this step, you’ll likely see warnings when you commit to Git. This makes more work for you because you will then have to revise the commits you have done with the corrected information.

您可以设置许多其他选项,但这是需要的两个基本选项。 如果跳过此步骤,则在提交到Git时可能会看到警告。 这将为您带来更多的工作,因为您将不得不使用更正的信息来修改您所做的提交。

结论 (Conclusion)

You should now have Git installed and ready to use on your system.

现在,您应该已经安装了Git,可以在系统上使用它了。

To learn more about how to use Git, check out these articles and series:

要了解有关如何使用Git的更多信息,请查看以下文章和系列:

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-git-on-ubuntu-20-04

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/689838
推荐阅读
相关标签
  

闽ICP备14008679号