夜间模式
Git 配置主要涉及用户信息、行为习惯、仓库设置等,分为 全局配置、系统配置 和 仓库级配置。这些配置项可以通过 Git 命令来修改,常见的配置项包括用户身份、编辑器设置、换行符处理等。
1. 用户身份配置
Git 需要记录提交的用户信息,包括用户名和邮箱地址。这些信息会出现在每个提交记录中。
设置全局用户名和邮箱(适用于所有仓库)
bashgit config --global user.name "Your Name" git config --global user.email "youremail@example.com"
1
2设置仓库级用户名和邮箱(只适用于当前仓库)
bashgit config user.name "Your Name" git config user.email "youremail@example.com"
1
2查看配置的用户名和邮箱
bashgit config --global user.name git config --global user.email
1
2
2. 配置编辑器
Git 使用默认编辑器(通常是 Vim)来编辑提交信息、合并冲突等。你可以将其更改为其他编辑器。
设置全局编辑器(例如:VSCode)
bashgit config --global core.editor "code --wait"
1
2设置为其他编辑器(例如:Notepad++)
bashgit config --global core.editor "notepad++ -multiInst -nosession"
1
3. 换行符处理
Git 可以配置自动转换换行符,尤其是在不同操作系统之间(如 Windows 和 Linux)时,可能出现换行符不一致的问题。
自动转换换行符(适用于 Windows)
bashgit config --global core.autocrlf true
1避免换行符转换(适用于 Linux/Mac)
bashgit config --global core.autocrlf input
1禁用换行符转换
bashgit config --global core.autocrlf false
1
4. 别名配置
Git 支持设置命令别名,可以让常用的 Git 命令更加简洁。
设置
git st
为git status
bashgit config --global alias.st status
1设置
git co
为git checkout
bashgit config --global alias.co checkout
1设置
git br
为git branch
bashgit config --global alias.br branch
1
5. 合并工具配置
如果出现合并冲突,可以配置 Git 使用特定的合并工具来帮助解决冲突。
设置合并工具(例如:Kdiff3)
bashgit config --global merge.tool kdiff3
1
6. Git 忽略配置
Git 使用 .gitignore
文件来忽略某些文件或目录,避免它们被提交到版本控制中。
- 创建
.gitignore
文件 在项目根目录下创建.gitignore
文件,并列出需要忽略的文件或目录。
7. 配置远程仓库
查看所有远程仓库
bashgit remote -v
1添加远程仓库
bashgit remote add origin https://github.com/username/repo.git
1修改远程仓库地址
bashgit remote set-url origin https://github.com/username/repo.git
1
8. 显示配置
查看所有 Git 配置项
bashgit config --list
1查看特定配置项
bashgit config --get user.name git config --get user.email
1
2
9. 全局与系统配置
查看全局配置文件 Git 配置文件有三种层级:系统级、全局级和仓库级。可以通过以下命令查看它们的内容:
- 系统配置文件:
/etc/gitconfig
- 全局配置文件:
~/.gitconfig
或~/.config/git/config
- 仓库级配置文件:
.git/config
(仓库目录下)
bashgit config --system --list # 查看系统级配置 git config --global --list # 查看全局级配置 git config --local --list # 查看仓库级配置
1
2
3- 系统配置文件:
10. 配置代理
有时在公司网络或防火墙后面,你可能需要配置 Git 使用 HTTP 或 HTTPS 代理服务器。
设置 HTTP 代理
bashgit config --global http.proxy http://proxyserver:port
1设置 HTTPS 代理
bashgit config --global https.proxy https://proxyserver:port
1
2取消代理
bashgit config --global --unset http.proxy git config --global --unset https.proxy
1
2