高级git技巧速查表
1 简介
本文是一份 Git 高级功能速查表,涵盖常用但容易被忽视的高效功能,包含 命令 + 使用场景 + 示例。
需要的可以直接放到文档或 README 里当速查手册。
2 Git 历史与调试
git blame <file> 查看文件每一行是谁、何时改的 git blame maingo
git log -p 查看提交历史和补丁 git log -p -2(最近 2 次提交)
git log --oneline --graph --decorate --all 图形化展示分支和提交历史 git log --oneline --graph
git show <commit> 查看某次提交的改动 git show 123abc
git reflog 找回丢失的提交(后悔药) git reflog → git checkout <commit>
git bisect 二分查找 Bug 引入的提交 git bisect start → git bisect bad → git bisect good <commit>
3 分支与提交管理
git stash 临时保存未提交的改动 git stash → git stash pop
git cherry-pick <commit> 把某次提交应用到当前分支 git cherry-pick a1b2c3
git rebase -i HEAD~N 整理历史(合并/修改/删除提交) git rebase -i HEAD~5
git reset --hard <commit> 回退到指定提交,丢弃改动 git reset --hard 123abc
git revert <commit> 反做某次提交(保留历史) git revert 456def
4 子模块与多工作区
git submodule add <url> <path> 添加子模块 git submodule add git@xxxgit common
git submodule update --init --recursive 拉取子模块内容 git submodule update --init --recursive
git submodule update --remote 更新子模块到远端最新 git submodule update --remote common
git worktree add <dir> <branch> 在多个目录同时签出不同分支 git worktree add /feature-x feature-x
git sparse-checkout set <dirs> 只签出部分目录(monorepo) git sparse-checkout init && git sparse-checkout set apis services
5 日常开发提效
git diff 查看未暂存的改动 git diff
git diff --staged 查看已暂存的改动 git diff --staged
git clean -fd 清理未跟踪的文件/目录 git clean -fd
git tag -a v10 -m “msg” 打标签(版本发布) git tag -a v10 -m “release 10”
git archive 打包某个版本源码 git archive --format=zip HEAD > srczip
6 配置与别名
git config alias<name> “<cmd>” 配置命令别名 git config --global alias co checkout
git config core fsmonitor false 禁用 FSMonitor(Windows 稳定性) git config --global core fsmonitor false
git config core longpaths true 允许长路径(Windows 必备) git config --system core longpaths true
git config pull rebase true 默认用 rebase 拉取 git config --global pull rebase true
7 团队协作 & 安全
git hooks 在 commit/push 前自动跑脚本(代码检查、测试) git/hooks/pre-commit
git commit -S -m “msg” 使用 GPG 签名提交 git commit -S -m “secure commit”
Git LFS 管理大文件(图片/模型/二进制) git lfs track “*zip”
8 小结
调试神器:git bisect, git blame, git reflog
历史管理:git rebase -i, git cherry-pick
多分支协作:git worktree, git sparse-checkout
仓库清理:git stash, git clean, git diff --staged
团队规范:hooks, tag, LFS
- 点赞
- 收藏
- 关注作者
评论(0)