Git 简明教程

Git - Environment Setup

在使用 Git 之前,您必须安装并执行一些基本配置更改。以下是有关在 Ubuntu 和 Centos Linux 上安装 Git 客户端的步骤。

Before you can use Git, you have to install and do some basic configuration changes. Below are the steps to install Git client on Ubuntu and Centos Linux.

Installation of Git Client

如果您使用 Debian 基础 GNU/Linux 发行版,则 apt-get 命令将完成必要的操作。

If you are using Debian base GNU/Linux distribution, then apt-get command will do the needful.

[ubuntu ~]$ sudo apt-get install git-core
[sudo] password for ubuntu:

[ubuntu ~]$ git --version
git version 1.8.1.2

如果您使用基于 RPM 的 GNU/Linux 发行版,请按照给定的使用 yum 命令。

And if you are using RPM based GNU/Linux distribution, then use yum command as given.

[CentOS ~]$
su -
Password:

[CentOS ~]# yum -y install git-core

[CentOS ~]# git --version
git version 1.7.1

Customize Git Environment

Git 提供了 git config 工具,允许您设置配置变量。Git 将所有全局配置存储在 .gitconfig 文件中,该文件位于您的主目录中。要将这些配置值设置为全局,请添加 --global 选项,如果您省略 --global 选项,则您的配置特定于当前 Git 存储库。

Git provides the git config tool, which allows you to set configuration variables. Git stores all global configurations in .gitconfig file, which is located in your home directory. To set these configuration values as global, add the --global option, and if you omit --global option, then your configurations are specific for the current Git repository.

您还可以设置系统范围内的配置。Git 将这些值存储在 /etc/gitconfig 文件中,其中包含系统上每个用户和存储库的配置。要设置这些值,您必须拥有 root 权限并使用 --system 选项。

You can also set up system wide configuration. Git stores these values in the /etc/gitconfig file, which contains the configuration for every user and repository on the system. To set these values, you must have the root rights and use the --system option.

编译并执行上述代码后,将产生以下结果 −

When the above code is compiled and executed, it produces the following result −

Setting username

Git 在每次提交时都会使用这些信息。

This information is used by Git for each commit.

[jerry@CentOS project]$ git config --global user.name "Jerry Mouse"

Setting email id

Git 在每次提交时都会使用这些信息。

This information is used by Git for each commit.

[jerry@CentOS project]$ git config --global user.email "jerry@tutorialspoint.com"

Avoid merge commits for pulling

您从远程存储库拉取最新的更改,如果这些更改是不同的,那么默认情况下 Git 会创建合并提交。我们可以通过以下设置避免这种情况。

You pull the latest changes from a remote repository, and if these changes are divergent, then by default Git creates merge commits. We can avoid this via following settings.

jerry@CentOS project]$ git config --global branch.autosetuprebase always

Color highlighting

以下命令为控制台中的 Git 启用颜色高亮显示。

The following commands enable color highlighting for Git in the console.

[jerry@CentOS project]$ git config --global color.ui true

[jerry@CentOS project]$ git config --global color.status auto

[jerry@CentOS project]$ git config --global color.branch auto

Setting default editor

默认情况下,Git 使用从 VISUAL 或 EDITOR 环境变量中获取的系统默认编辑器。我们可以使用 git config 配置不同的编辑器。

By default, Git uses the system default editor, which is taken from the VISUAL or EDITOR environment variable. We can configure a different one by using git config.

[jerry@CentOS project]$ git config --global core.editor vim

Setting default merge tool

Git 不提供用于将冲突更改集成到您的工作树中的默认合并工具。我们可以通过启用以下设置来设置默认合并工具。

Git does not provide a default merge tool for integrating conflicting changes into your working tree. We can set default merge tool by enabling following settings.

[jerry@CentOS project]$ git config --global merge.tool vimdiff

Listing Git settings

若要验证本地存储库的 Git 设置,请按照给出下文所示使用 git config –list 命令。

To verify your Git settings of the local repository, use git config –list command as given below.

[jerry@CentOS ~]$ git config --list

上述命令将产生以下结果。

The above command will produce the following result.

user.name=Jerry Mouse
user.email=jerry@tutorialspoint.com
push.default=nothing
branch.autosetuprebase=always
color.ui=true
color.status=auto
color.branch=auto
core.editor=vim
merge.tool=vimdiff