centos上安装git

(Introduction)

Version control systems are an indispensable part of modern software development. 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.

版本控制系统是现代软件开发中必不可少的部分。 版本控制使您可以在源代码级别跟踪软件。 您可以跟踪更改,还原到上一个阶段以及分支以创建文件和目录的备用版本。

One of the most popular version control systems currently available is Git. Many projects’ files are maintained in a Git repository, and sites like GitHub, GitLab, and Bitbucket help to facilitate software development project sharing and collaboration.

当前可用的最流行的版本控制系统之一是Git。 许多项目的文件都保存在Git存储库中,而GitHub,GitLab和Bitbucket等站点则有助于促进软件开发项目的共享和协作。

In this guide, we will go through how to install and configure Git on a CentOS 8 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 has their own benefits depending on your specific needs.

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

(Prerequisites)

You will need a CentOS 8 server with a non-root superuser account.

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

To set this up, you can follow our Initial Server Setup Guide for CentOS 8.

要进行设置,您可以遵循我们的《 CentOS 8初始服务器设置指南》 。

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

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

(Installing Git with Default Packages)

Our first option to install Git is via CentOS’s default packages.

我们安装Git的第一个选择是通过CentOS的默认软件包。

This option is best for those who want to get up and running quickly with Git, those who prefer a widely-used stable version, or those who are not looking for the newest available options. If you are looking for the most recently release, you should jump to the section on installing from source.

对于那些希望通过Git快速启动并运行的用户,更喜欢使用广泛使用的稳定版本的用户,或者不希望找到最新可用选项的用户而言,此选项是最佳选择。 如果您正在寻找最新版本,则应跳至有关从源安装的部分。

We will be using the open-source package manager tool DNF, which stands for Dandified YUM the next-generation version of the Yellowdog Updater, Modified (that is, yum). DNF is a package manager that is now the default package manager for Red Hat based Linux systems like CentOS. It will let you install, update, and remove software packages on your server.

我们将使用开源程序包管理器工具DNF,它代表Dandified YUM,即Yellowdog Updater Modified(即yum)的下一代版本。 DNF是一个程序包管理器,现在是基于Red Hat的Linux系统(如CentOS)的默认程序包管理器。 它使您可以在服务器上安装,更新和删除软件包。

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

首先,使用DNF软件包管理工具来更新本地软件包索引。

sudo dnf update -y

The -y flag is used to alert the system that we are aware that we are making changes, preventing the terminal from prompting us to confirm.

-y标志用于警告系统我们知道我们正在进行更改,从而防止终端提示我们进行确认。

With the update complete, you can install Git:

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

  • sudo dnf install git -y

You can confirm that you have installed Git correctly by running the following command:

您可以通过运行以下命令来确认已正确安装了Git:

  • git --version

Output
   
   git version 2.18.2

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

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

(Installing Git from Source)

A more flexible method of installing Git is to compile the software from source. 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 some control over the options you include if you wish to customize.

安装Git的一种更灵活的方法是从源代码编译软件。 这将花费更长的时间,并且不会通过软件包管理器进行维护,但是它将允许您下载最新版本,并且如果您希望自定义的话,还可以控制您包括的选项。

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 packages.

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

  • sudo dnf update -y
  • sudo dnf install gettext-devel openssl-devel perl-CPAN perl-devel zlib-devel gcc autoconf -y

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
  • cd /tmp

From the Git project website, we can navigate to the Red Hat Linux distribution 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.0, 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/上提供的Red Hat Linux发行包列表,并下载所需的版本。 在撰写本文时,最新版本是2.26.0,因此我们将下载该版本以进行演示。 我们将使用curl并将下载的文件输出到git.tar.gz

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

Unpack the compressed tarball file:

解压缩压缩的tarball文件:

  • tar -zxf git.tar.gz

Next, move into the new Git directory:

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

  • cd git-*

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

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

  • make prefix=/usr/local all
  • sudo make prefix=/usr/local install

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

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

  • git --version

Output
   
   git version 2.26.0

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

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

(Setting Up Git)

Now that you have Git installed, you should configure it so that the generated commit messages will contain your correct information.

既然已经安装了Git,则应该对其进行配置,以便生成的提交消息将包含正确的信息。

This 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会将这些信息嵌入到我们所做的每次提交中。 我们可以继续输入以下内容来添加此信息:

  • 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

Output
   
   user.name=Your Name
user.email=youremail@domain.com
...

The information you enter is stored in your Git configuration file, which you can optionally edit by hand with a text editor like this:

输入的信息存储在Git配置文件中,您可以选择使用文本编辑器手动编辑该文件,如下所示:

  • vi ~/.gitconfig

~/.gitconfig contents

〜/ .gitconfig内容

[user]
  name = Your Name
  email = youremail@domain.com

Press ESC then :q to exit the text editor.

按ESC然后按:q退出文本编辑器。

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的更多信息,请查看以下文章和系列:

  • How To Use Git Effectively 如何有效使用Git
  • How To Use Git Branches 如何使用Git分支
  • An Introduction to Open Source 开源简介

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-git-on-centos-8

centos上安装git