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

安装 Git 客户端

如果您使用的是 Debian 基础 GNU/Linux 发行版,那么apt-get命令将完成需要的工作。

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

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

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

[CentOS ~]$
su -
Password:

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

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

自定义Git 环境

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

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

编译并执行上述代码时,会产生以下结果 –

设置用户名

Git 将此信息用于每次提交。

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

设置电子邮件 ID

Git 将此信息用于每次提交。

[jerry@CentOS project]$ git config --global user.email "jerry@ipba.online"

避免合并提交以进行拉取

您从远程存储库中提取最新的更改,如果这些更改有分歧,则默认情况下 Git 会创建合并提交。我们可以通过以下设置来避免这种情况。

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

颜色突出显示

以下命令在控制台中为 Git 启用颜色突出显示。

[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

设置默认编辑器

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

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

设置默认合并工具

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

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

列出 Git 设置

要验证本地存储库的 Git 设置,请使用git config –list命令,如下所示。

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

上面的命令将产生以下结果。

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

Git环境设置 推荐阅读

删除Git分支 (本地和远程)

如何使用GPT-4审查和重构代码

领券有优惠