Git 安装与配置(一)

1.安装与配置

笔记来源:Git 教程

其他教程:Git 教程 – 廖雪峰

在线练习:Learn Git Branching

一、安装

1. Linux 平台

yum -y install git
apt install -y  git

2. Windows 平台

  • 下载安装包后,默认安装即可。

  • 安装后可使用:

    • 命令行工具:Git Bash(在开始菜单中查找)
    • 图形界面工具:Git GUI

二、配置

Git 使用 git config 命令进行配置,配置信息存储于三个级别(优先级由低到高):

配置文件 作用范围 读写选项
/etc/gitconfig 系统所有用户 --system
~/.gitconfig 当前用户 --global
.git/config 当前仓库 (默认)

Windows 中用户主目录通常为 C:\Users\$USER​,Git 会查找 %HOME%\.gitconfig

1. 设置用户信息(必须)

git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

2. 设置默认文本编辑器

例如使用 VS Code:

git config --global core.editor "code --wait"

3. 设置差异分析工具(合并冲突时使用)

git config --global merge.tool vimdiff

支持的工具:kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, opendiff 等。

4. 查看配置

# 列出所有配置
git config --list

# 查看单个配置项
git config user.name

5. 直接编辑配置文件

vim ~/.gitconfig

三、生成 SSH 密钥(可选)

如果你需要通过 SSH 进行 Git 操作,可以生成 SSH 密钥并添加到你的 Git 托管服务(如 GitHub、GitLab 等)上。

ssh-keygen -t rsa -b 4096 -C "your.email@example.com"

按提示完成生成过程,然后将生成的公钥(默认 ~/.ssh/id_rsa.pub)添加到相应的平台。

四、验证安装

git --version
git config --list