跳到主要内容

02 安装和初始化配置

一、安装 Git

1.1 Windows 系统

  1. 访问 Git 官方下载页面
  2. 下载适合 Windows 的安装包并运行。
  3. 按照安装向导完成安装,推荐保持默认设置。

1.2 Linux 系统

# Debian/Ubuntu
sudo apt update
sudo apt install git

二、获取帮助

2.1 git 帮助命令

使用 git --help 或者 git [子命令] --help 获取帮助信息。

< AsciinemaDemo file = "/asciinema/git--help.cast" theme = "tango" autoplay = rows =50 />

2.2 cheat.sh 查询

cheat.sh 收集了许多命令的用法示例,你可以在终端中查询命令的用法:

curl cheat.sh/要查询的命令

例如查询 git 命令:

$ curl cheat.sh/git
cheat.sheets:git
# Set your identity.
git config --global user.name "John Doe"
git config --global user.email johndoe@example.com

# Set your editor.
git config --global core.editor emacs

# Enable color support for commands like `git diff`. Disable with `never` or
# partially disable -- unless otherwise applied -- with `false`.
git config --global color.ui true

# Stage all changes for commit.
git add [--all|-A

...

三、初始化全局配置

语法:

git config --list 	#查看当前仓库配置
git config --global --list #查看全局配置

git config user.name #设置当前仓库的作者
git config --global #设置所有仓库的默认作者

在安装 Git 后,建议配置以下全局选项:

3.1 设置用户名

配置提交记录中显示的用户名。

git config --global user.name "你的用户名"

3.2 设置邮箱

配置提交记录中显示的邮箱地址。

git config --global user.email "你的邮箱地址"

3.3 设置默认编辑器(可选)

在执行 git commit,也就是提交命令时,会启动一个编辑器,让我们输入本次提交的备注信息。默认启动 nano/Vim 编辑器,可以设置为其他编辑器:

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

3.4 对换行符的处理

Windows: 提交时把 CRLF 转为 LF,检出时把 LF 转为 CRLF

git config --global core.autocrlf true

Linux/Mac: 提交时转 LF,检出保持不变

 git config --global core.autocrlf input

禁用自动转换:Git 保留文件原样

 git config --global core.autocrlf false

3.5 对中文目录的处理

默认情况下,git status 等命令的输出会将中文转换为 ASCII 字符。要正确输出中文目录,需关闭 ASCII 转换:

git config --global core.quotepath false

默认 ASCII 转换:

➜  my-website git:(master) ✗ git status
位于分支 master
您的分支与上游分支 'gitea/master' 一致。
尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git restore <文件>..." 丢弃工作区的改动)
修改: "docs/\350\256\241\347\256\227\346\234\272/Git/B\347\253\231 GeekHour/13 Gitee \347\232\204\344\275\277\347\224\250\345\222\214 GitLab \346\234\254\345\234\260\345\214\226\351\203\250\347\275\262.md"
修改: "docs/\350\256\241\347\256\227\346\234\272/Git/B\347\253\231 GeekHour/14 GUI \345\267\245\345\205\267.md"
修改: "docs/\350\256\241\347\256\227\346\234\272/Git/B\347\253\231 GeekHour/15 \345\234\250 VSCode \344\270\255\344\275\277\347\224\250 Git.md"
修改尚未加入提交(使用 "git add" 和/或 "git commit -a")

关闭 ASCII 转换:

➜  my-website git:(master) ✗ git config --global core.quotepath false
➜ my-website git:(master) ✗ git status
位于分支 master
您的分支与上游分支 'gitea/master' 一致。

尚未暂存以备提交的变更:
(使用 "git add <文件>..." 更新要提交的内容)
(使用 "git restore <文件>..." 丢弃工作区的改动)
修改: docs/计算机/Git/B站 GeekHour/13 Gitee 的使用和 GitLab 本地化部署.md
修改: docs/计算机/Git/B站 GeekHour/14 GUI 工具.md
修改: docs/计算机/Git/B站 GeekHour/15 在 VSCode 中使用 Git.md

修改尚未加入提交(使用 "git add" 和/或 "git commit -a")**查看当前配置**: