Git 常用命令与技巧

基础配置

rebase

# 在 pull 时使用 rebase
git config --global pull.rebase true

为什么使用 rebase?

默认情况下,git pull 等同于 git fetch + git merge,当本地和远程都有新提交时,会产生一个额外的合并提交(merge commit),导致提交历史出现分叉:

* Merge branch 'main' of origin
|\
| * 远程提交
* | 本地提交
|/

设置 pull.rebase true 后,git pull 变为 git fetch + git rebase,会将本地提交”变基”到远程分支之上,保持线性历史:

* 本地提交(重新应用)
* 远程提交

优点:提交历史更清晰,避免无意义的合并提交,方便 code review 和问题追溯。

用户信息配置

# 设置用户名
git config --global user.name "yourname"
# 设置邮箱
git config --global user.email "your@email.com"
# 查看所有配置
git config --list

常用别名配置

简单别名

# 查看状态
git config --global alias.st status
# 切换分支
git config --global alias.co checkout
# 分支操作
git config --global alias.br branch

高级别名(Shell 函数)

利用 Shell 函数实现更强大的别名:

# 一键添加、提交并推送(默认消息 "update")
git config --global alias.update '!f() { git add . && git commit -m "${*:-update}" && git push; }; f'
# 一键添加并提交(默认消息 "update")
git config --global alias.save '!f() { git add . && git commit -m "${*:-update}"; }; f'
# 美化的提交日志(默认显示 5 条)
git config --global alias.lg '!f() {
if [ $# -eq 0 ]; then
git log --oneline --decorate --graph --all -5
elif [ $# -eq 1 ] && [[ "$1" =~ ^[0-9]+$ ]]; then
git log --oneline --decorate --graph --all -n "$1"
else
git log --oneline --decorate --graph --all "$@"
fi
}; f'

别名使用示例

命令说明
git update添加所有文件、提交 “update” 并推送
git update fix bug添加所有文件、提交 “fix bug” 并推送
git save添加所有文件并提交 “update”
git save add feature添加所有文件并提交 “add feature”
git lg显示最近 5 条美化日志
git lg 10显示最近 10 条美化日志
git lg --author="name"按作者筛选日志

提示:别名中的 ! 表示执行 Shell 命令,${*:-default} 是 Bash 语法,表示使用所有参数,若无参数则使用默认值。

远程仓库管理

# 查看远程仓库地址
git remote -v
# 添加远程仓库
git remote add origin <repository-url>
# 删除远程仓库关联
git remote rm origin
# 修改远程仓库地址
git remote set-url origin <new-url>
# 克隆仓库
git clone <repository-url>

提交规范 (Conventional Commits)

推荐使用规范化的提交信息格式:<type>(<scope>): <description>

类型说明
feat新功能
fix修复 bug
docs文档变更
style代码格式(不影响功能)
refactor代码重构(既不是新功能也不是修复)
perf性能优化
test测试相关
build构建系统或外部依赖变更
ciCI 配置变更
chore其他不修改 src 或 test 的变更
revert回退提交

合并多个提交 (Squash)

使用交互式 rebase 将多个提交合并为一个:

# 合并最近 n 个提交
git rebase -i HEAD~n

例如合并最近 3 个提交:

git rebase -i HEAD~3

执行后会打开编辑器,显示类似:

pick abc1234 第一个提交
pick def5678 第二个提交
pick ghi9012 第三个提交

将需要合并的提交前的 pick 改为 squash(或简写 s):

pick abc1234 第一个提交
squash def5678 第二个提交
squash ghi9012 第三个提交

保存后会弹出编辑器让你编辑合并后的提交信息。

注意HEAD~n 中的 n 是你要操作的提交数量。第一个提交必须保持 pick,其他提交改为 squash 会被合并到第一个提交中。

换行符问题 (CRLF/LF)

不同操作系统使用不同的换行符:

  • Windows: CRLF (\r\n)
  • macOS/Linux: LF (\n)

解决方案一:配置 core.autocrlf

设置提交时检出时推荐系统
trueCRLF 转 LFLF 转 CRLFWindows
inputCRLF 转 LF不转换macOS/Linux
false不转换不转换-
# Windows 用户
git config --global core.autocrlf true
# macOS/Linux 用户
git config --global core.autocrlf input

解决方案二:使用 .gitattributes(推荐)

在项目根目录创建 .gitattributes 文件,统一团队换行符规则:

# 自动检测文本文件并统一使用 LF
* text=auto eol=lf

这种方式的优势是配置跟随仓库,确保所有开发者使用相同的规则。

克隆到非空目录

当目标目录已存在且非空时,git clone 会报错:

fatal: destination path already exists and is not an empty directory

解决方案

通过 git init 初始化后手动关联远程仓库:

# 进入目标目录
cd /your/target/directory
# 初始化 Git 仓库
git init
# 添加远程仓库地址
git remote add origin <your-repo-url>
# 拉取远程代码
git pull
# 切换到 main 分支(-f 强制覆盖本地文件)
git checkout main -f
# 设置本地分支跟踪远程分支
git branch --set-upstream-to origin/main

注意:使用 -f 参数会强制覆盖本地文件,执行前请备份重要文件。

参考资料

Read Next

Flutter 底层原理深度解析

Read Previous

Claude Code Architecture Deep Dive