关于设置和用户信息
修改用户名和邮箱
bash
git config --global user.name yourname
git config --global user.email your@email.com查看配置信息
bash
git config --list关于仓库
修改Git远程地址仓库
bash
# 查看远程仓库地址
git remote -v
# 移除远程仓库地址
git remote rm origin
# 添加新的远程仓库地址
git remote add origin (ssh or https 仓库地址)克隆远程仓库
bash
git clone (ssh or https 仓库地址)关于提交
压缩提交信息
bash
git rebase -i HEAD~(n-1) # n 为需要压缩的提交次数
# 在编辑器中,将除第一个提交(父提交)外的每个提交都改成"s"或者"squash",然后保存关闭编辑器。
git rebase --abort # 若结果不符合预期,还可以通过git rebase --abort取消rebase操作。快捷替换
bash
# 将所有的 pick 替换为 s
:%s/pick/s/g
# 清空提交信息
:%d提交规范
Commit Message 格式
| Type | Description |
|---|---|
| feat | Introduce a new feature to the codebase |
| fix | Fix a bug in the codebase |
| docs | Create/update documentation |
| style | Feature and updates related to styling |
| refactor | Refactor a specific section of the codebase |
| test | Add or update code related to testing |
| chore | Regular code maintenance |
| perf | Codebase is optimized |
| ci | Continuous integration related |
| build | Update build scripts and packages |
| temp | Temporary commit that won't be in the final codebase |
| readme | Update README.md |
| git | Update .gitignore or .gitattributes |
| config | Update configuration files |
| revert | Revert to a previous commit |
其他常见问题
LF 和 CRLF 问题
当 Git 处理 package.json 文件时,会将换行符从 LF (Line Feed, 用于 Unix 系统) 替换为 CRLF (Carriage Return + Line Feed, 用于 Windows 系统)。
windows
bash
git config --global core.autocrlf true这将告诉 Git 在检出代码库时将 LF 转换为 CRLF,在提交时将 CRLF 转换回 LF
mac 和 linux
bash
git config --global core.autocrlf input这将告诉 Git 仅在提交时将 CRLF 转换为 LF
.gitattributes 文件
bash
echo "* text=auto eol=lf" > .gitattributes