Git 备忘录
日常高频命令速查。按"场景"而非"命令字母"组织,便于出问题时直接翻。
一、配置
# 用户信息(全局)
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
# 换行符(Windows 推荐)
git config --global core.autocrlf true
# 中文文件名不乱码
git config --global core.quotepath false
# 默认分支名
git config --global init.defaultBranch main
# 常用别名
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.lg "log --oneline --graph --decorate --all"
# 查看所有配置
git config --list --show-origin
二、基础工作流
git status # 查看工作区状态
git add <file> # 暂存指定文件
git add . # 暂存所有改动(不含删除)
git add -A # 暂存所有改动(含删除)
git commit -m "message" # 提交
git commit -am "message" # 跳过 add,直接提交已跟踪文件
git push # 推送
git pull # 拉取并合并
查看差异
git diff # 工作区 vs 暂存区
git diff --staged # 暂存区 vs 最新提交
git diff HEAD # 工作区 + 暂存区 vs 最新提交
git diff <branch1>..<branch2> # 两分支差异
三、分支
git branch # 查看本地分支
git branch -a # 查看所有分支(含远程)
git branch -vv # 查看分支及跟踪关系
git branch <name> # 创建分支
git switch <name> # 切换分支(新版推荐)
git switch -c <name> # 创建并切换
git checkout -b <name> # 同上(旧版写法)
git branch -d <name> # 删除已合并分支
git branch -D <name> # 强制删除
git branch -m <old> <new> # 重命名分支
合并与变基
git merge <branch> # 合并到当前分支(保留历史)
git merge --no-ff <branch> # 强制生成合并提交
git rebase <branch> # 变基(线性历史)
git rebase -i HEAD~3 # 交互式变基,重写最近 3 条提交
原则:已推送到共享远程的分支不要 rebase;只在本地分支上用。
四、撤销与回退
这是最容易踩坑的地方,先分清三种"撤销":
| 命令 | 作用范围 | 是否修改历史 | 使用场景 |
|---|---|---|---|
git restore | 工作区 / 暂存区 | 否 | 丢弃未提交的修改 |
git reset | 暂存区 / 提交历史 | 是 | 撤销提交(未推送) |
git revert | 提交历史 | 否(新增反向提交) | 撤销已推送的提交 |
丢弃工作区修改
git restore <file> # 丢弃单个文件的未暂存修改
git restore . # 丢弃所有未暂存修改(危险)
git checkout -- <file> # 旧版写法
取消暂存
git restore --staged <file> # 从暂存区移回工作区
git reset HEAD <file> # 旧版写法
撤销提交
# 保留改动在工作区
git reset --soft HEAD~1
# 保留改动在暂存区
git reset --mixed HEAD~1 # 默认
# 彻底丢弃改动(危险)
git reset --hard HEAD~1
# 撤销已推送的提交(安全)
git revert <commit-hash>
找回丢失的提交
git reflog # 查看 HEAD 移动历史
git reset --hard <hash> # 回到某个历史位置
reset --hard 后如果后悔,reflog 通常能救回来,除非已经 GC。
五、贮藏(stash)
git stash # 贮藏当前改动
git stash -u # 含未跟踪文件
git stash list # 查看贮藏列表
git stash pop # 弹出最近贮藏并删除
git stash apply stash@{0} # 应用指定贮藏(保留)
git stash drop stash@{0} # 删除指定贮藏
git stash clear # 清空所有贮藏
常见场景:改了一半,突然要切分支修 bug。
六、远程
git remote -v # 查看远程仓库
git remote add origin <url> # 添加远程
git remote set-url origin <url> # 修改远程地址
git remote remove origin # 删除远程
git fetch # 拉取远程信息,不合并
git pull # fetch + merge
git pull --rebase # fetch + rebase
git push # 推送
git push -u origin <branch> # 首次推送并建立跟踪
git push --force-with-lease # 安全强推(推荐)
git push --force # 强推(危险)
强推原则:永远用
--force-with-lease,它会检查远程是否有人推过新提交,避免误覆盖别人的工作。
七、标签
git tag # 查看标签
git tag v1.0.0 # 轻量标签
git tag -a v1.0.0 -m "msg" # 附注标签(推荐)
git push origin v1.0.0 # 推送单个标签
git push --tags # 推送所有标签
git tag -d v1.0.0 # 删除本地标签
git push origin :refs/tags/v1.0.0 # 删除远程标签
八、查看历史
git log # 完整日志
git log --oneline # 单行
git log --oneline --graph # 图形化
git log -p <file> # 查看某文件历史
git log --author="name" # 按作者过滤
git log --since="2 weeks ago" # 按时间过滤
git log -S "keyword" # 按内容变化搜索(pickaxe)
git blame <file> # 逐行查看最后修改者
git show <commit> # 查看某次提交详情
九、常见问题
1. 提交信息写错了(未推送)
git commit --amend -m "new message"
如果只是想补充文件:
git add forgotten.txt
git commit --amend --no-edit
2. 提交到了错误的分支
# 假设应该提交到 feature 分支,却提交到了 main
git branch feature # 在当前位置创建分支
git reset --hard HEAD~1 # main 回退
git switch feature # 切到 feature
3. 本地分支落后,想同步远程
git fetch origin
git rebase origin/main
# 或者
git pull --rebase origin main
4. 合并冲突
git status # 查看冲突文件
# 手动编辑冲突文件,删除 <<<<<<< ======= >>>>>>> 标记
git add <resolved-file>
git commit # 或 git rebase --continue
放弃合并:
git merge --abort
git rebase --abort
5. 不小心提交了敏感信息
如果还没推送,git reset --hard 即可。
如果已推送,必须改写历史:
git filter-repo --path secrets.txt --invert-paths
git push --force-with-lease
同时立即更换泄露的密钥,因为 Git 服务器可能仍留有副本。
6. 忽略文件不生效
git rm -r --cached .
git add .
git commit -m "fix gitignore"
.gitignore 只对未跟踪文件生效,已跟踪的文件需要先 rm --cached。
十、提交规范(Conventional Commits)
<type>(<scope>): <subject>
<body>
<footer>
常用 type:
| type | 说明 |
|---|---|
feat | 新功能 |
fix | 修复 bug |
docs | 文档变更 |
style | 格式调整(不影响代码逻辑) |
refactor | 重构 |
perf | 性能优化 |
test | 测试相关 |
chore | 构建 / 工具 / 依赖变更 |
示例:
feat(auth): 支持 OAuth 2.0 登录
- 新增 GitHub / Google 登录入口
- 补充 token 刷新逻辑
Closes #123
十一、实用技巧
# 快速查看某文件的最后一次修改
git log -1 --stat <file>
# 查找是哪次提交引入了某个字符串
git log -S "search-string" --source --all
# 查看两个分支的共同祖先
git merge-base main feature
# 比较工作区和某个提交
git diff <commit>
# 临时切换到某个提交查看,不切分支
git switch --detach <commit>
# 查看暂存区的具体内容
git diff --staged
# 统计每个作者的提交数
git shortlog -sn
# 找出未被合并的分支
git branch --no-merged
十二、配置文件位置
| 级别 | 路径 |
|---|---|
| 系统 | /etc/gitconfig(Linux)/ C:\Program Files\Git\etc\gitconfig(Windows) |
| 全局 | ~/.gitconfig / %USERPROFILE%\.gitconfig |
| 仓库 | <repo>/.git/config |
优先级:仓库 > 全局 > 系统。
参考
- 官方文档:https://git-scm.com/docs
- Pro Git(中文版):https://git-scm.com/book/zh/v2
- Conventional Commits:https://www.conventionalcommits.org/zh-hans/