网站公告

本次更新:没啥更新的

“钱塘江上潮信起,今日方知我是我”

Skip to content

安装与初次配置 ​

字数: 0 字 时长: 0 分钟

1.安装 Git ​

1.1 macOS ​

bash
git --version

如果系统没有 Git,可以通过 Xcode Command Line Tools 或 Homebrew 安装。

1.2 Windows ​

使用 Git for Windows。

参考: https://blog.csdn.net/mukes/article/details/115693833

1.3 Linux ​

使用发行版包管理器安装。

参考:https://blog.csdn.net/u012856866/article/details/125499320

2.操作空间 ​

2.1 macOS ​

用自带的终端就可以了,但是我习惯用 iTerm2。

2.2 windows ​

点击 Git Bash

就会看到类似的图

3. 用户信息 ​

3.1 配置用户信息 ​

bash
git config --global user.name "Your Name"
git config --global user.email "your@email.com"
命令作用说明
git config --global user.name "Your Name"设置用户名提交代码时记录作者名称,例如 Kerwim
git config --global user.email "your@email.com"设置邮箱提交代码时记录作者邮箱,GitHub 也可通过邮箱关联提交记录
--global全局配置对当前电脑上的所有 Git 仓库生效

3.2 查看配置 ​

bash
git config --list

3.3 常用别名 ​

bash
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit

你可以把用 st 当做 status 的简写,后面查看状态只需要打 st 就可以不需要打完整的 status 了,因为我喜欢打全。

配置命令中文意思简写后等价命令
git config --global alias.st status查看仓库当前状态git stgit status
git config --global alias.co checkout切换分支 / 恢复文件git cogit checkout
git config --global alias.br branch查看或管理分支git brgit branch
git config --global alias.cm commit提交代码到本地仓库git cmgit commit

3.4 默认分支名 ​

bash
git config --global init.defaultBranch main

设置默认分支名字为 main

3.5 中文乱码处理 ​

bash
git config --global core.quotepath false

3.6 查看某个配置 ​

bash
git config --global --get init.defaultBranch

查看默认分支名字就是 main,没有用常见的 master,其实正常还是用 master 的。

3.7 换行符配置 ​

macOS 默认这样配

bash
git config --global core.autocrlf input

windows 默认这样配

bash
git config --global core.autocrlf true

系统常用换行符推荐配置提交时检出时
macOSLF(\n)git config --global core.autocrlf inputCRLF → LF保持 LF
LinuxLF(\n)git config --global core.autocrlf inputCRLF → LF保持 LF
WindowsCRLF(\r\n)git config --global core.autocrlf trueCRLF → LFLF → CRLF
core.autocrlf 用来处理不同操作系统之间的换行符差异。macOS 和 Linux 通常使用 LF,Windows 通常使用 CRLF;Git 会在提交和检出时自动转换,避免因为换行符不同产生大量无意义的代码变更。

3.8 查看配置来源 ​

bash
git config --list --show-origin
配置来源配置项当前值作用
系统级 .../git-core/gitconfigcredential.helperosxkeychain使用 macOS 钥匙串保存 Git 的 HTTPS 凭据 / Token
系统级 .../git-core/gitconfiginit.defaultBranchmain新建 Git 仓库时默认使用 main 分支
用户级 ~/.gitconfiguser.namekerwimGit 提交时使用的作者名称
用户级 ~/.gitconfiguser.emailpopkerwim@yeah.netGit 提交时使用的作者邮箱
用户级 ~/.gitconfiginit.defaultBranchmain新建 Git 仓库时默认使用 main 分支
用户级 ~/.gitconfigcore.quotepathfalse中文文件名直接显示中文,不转义成 \xxx
用户级 ~/.gitconfigcore.autocrlfinput提交时将 CRLF 转为 LF,检出时不反向转换

3.9删除某个配置 ​

bash
git config --global --unset init.defaultBranch

然后用git config --list重新查看

就把用户级的默认分支名称删掉了,但是系统级的还在

3.10 删除同名的所有配置 ​

如果一个配置被重复设置,可以全部删除后重新配置。

bash
git config --global --unset-all init.defaultBranch

然后重新设置:

bash
git config --global init.defaultBranch master

然后查看

3.11 直接编辑全局配置文件 ​

bash
git config --global --edit

Noctiflorous Garden · MIT License