Git 命令速查 适用平台:Windows / macOS / Linux。覆盖安装后常用到的配置 / 新建与克隆 / 提交与撤销 / 分支与合并 / Rebase / 远端 / 标签 / 记录 / 暂存 / 子模块 / 工作树 / 稀疏检出 / 清理 / 排错 等。
1. 基础配置(一次性/全局) 1 2 3 4 5 6 7 8 9 10 11 12 13 git --version git config --global user.name "Your Name" git config --global user.email "you@example.com" git config --global init.defaultBranch main git config --global core.autocrlf input git config --global core.autocrlf true git config --global pull.rebase false git config --global fetch.prune true git config --global rerere.enabled true git config --global core.editor "code --wait" git config --global alias.st "status -sb" git config --global alias.lg "log --oneline --graph --decorate --all" git config --list --show-origin
SSH 与签名(可选)
1 2 3 4 5 ssh-keygen -t ed25519 -C "you@example.com" git config --global commit.gpgsign true git config --global user.signingkey <KEYID>
2. 新建 / 克隆仓库 1 2 3 4 5 git init git init myrepo git clone <url> git clone --depth 1 <url> git clone -b develop <url>
3. 查看状态 / 差异 / 忽略 1 2 3 4 5 git status -sb git diff git diff --staged git add -N <file> git check-ignore -v <path>
.gitignore 快速示例
1 2 3 4 # 忽略所有 .log 和 temp/ 下内容 *.log temp/ !temp/keep.me
4. 暂存与提交 1 2 3 4 5 6 git add <file> git add . git restore --staged <file> git commit -m "feat: message" git commit --amend git commit --amend --no-edit
多行提交信息
5. 撤销与回滚(工作区/暂存区/提交) 1 2 3 4 5 6 7 8 git restore <file> git restore --source =HEAD --staged <file> git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1 git revert <commit> git reflog git restore -S <commit>
删除误追踪的大文件
1 2 git rm --cached <path> echo "<path>" >> .gitignore
6. 分支(创建/切换/删除/重命名) 1 2 3 4 5 6 7 git branch git branch -vv git switch -c feature/x git switch main git branch -m oldname newname git branch -d feature/x git branch -D feature/x
跟踪远端分支
1 2 3 git fetch git switch -c feature/x origin/feature/x git branch -u origin/feature/x
7. 合并与冲突解决 1 2 3 4 5 git merge feature/x git merge --no-ff feature/x git add <conflicted-files> git commit
图形化合并工具(示例:VS Code)
1 2 3 git config --global merge.tool vscode git config --global mergetool.vscode.cmd "code --wait $MERGED " git mergetool
8. Rebase(整理提交历史) 1 2 3 4 5 git rebase main git rebase -i HEAD~5 git rebase --abort git rebase --continue git pull --rebase
注意 :不要对已经公开推送、他人基于其工作的提交做 rebase/改历史。
9. 远端(Remote)/ 拉取与推送 1 2 3 4 5 6 7 8 9 git remote -v git remote add origin <url> git remote set-url origin <new-url> git fetch --all --prune git pull git pull --rebase git push -u origin main git push git push --force-with-lease
10. Stash(临时保存工作现场) 1 2 3 4 5 6 7 git stash push -m "WIP: message" git stash list git stash show -p stash@{0} git stash apply stash@{0} git stash pop git stash drop stash@{0} git stash push -u
11. Tag(标签 / 版本) 1 2 3 4 5 6 git tag git tag v1.0.0 git tag -a v1.0.0 -m "release 1.0.0" git show v1.0.0 git push origin v1.0.0 git push origin --tags
12. 日志与定位(log / blame / grep / shortlog) 1 2 3 4 5 6 git log --oneline --graph --decorate --all git log --stat --patch git blame <file> git grep -n "pattern" git shortlog -sn --all git show <commit>
范围与选择器
1 2 3 git log <since>..<until > git log -- <path> git diff <commit1> <commit2> -- <path>
13. Cherry-pick(挑拣提交) 1 2 3 git cherry-pick <commit> git cherry-pick <c1> <c2> git cherry-pick --abort | --continue
14. Submodule(子模块) 1 2 3 4 5 git submodule add <url> path/ git submodule update --init --recursive git submodule sync --recursive git submodule update --remote --merge
15. Worktree(多工作树并行开发) 1 2 3 git worktree list git worktree add ../wt-feature feature/x git worktree remove ../wt-feature
16. 稀疏检出(仅拉取需要的目录) 1 2 3 4 git sparse-checkout init --cone git sparse-checkout set path/another-path/ git sparse-checkout add extras/ git sparse-checkout list
17. 清理与维护 1 2 3 git clean -fdX git clean -ndX git gc --aggressive --prune=now
18. 重写历史(谨慎) 1 2 3 4 5 6 7 8 git commit --amend --author="New Name <new@example.com>" git filter-repo --email-callback ' return b"new@example.com" if email==b"old@example.com" else email '
如仓库已公开,重写历史需要强推 并通知协作者重新同步。
19. 行结尾 / 大文件 / LFS / 属性 1 2 3 4 5 6 7 8 9 echo "* text=auto" >> .gitattributesgit lfs install git lfs track "*.bin" git add .gitattributes git add <large.bin> git commit -m "track large files via LFS"
20. 排错套路 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 git status -sb git log --oneline -n 10 git reflog -n 20 git remote -v git fetch --all --prune git branch -vv git reflog git reset --hard X git stash push -u -m "safety" git switch -c rescue/$(date +%Y%m%d-%H%M%S) git stash pop
21. 常用别名建议(.gitconfig) 1 2 3 4 5 6 7 8 9 10 11 12 13 [alias] st = status -sb lg = log --on eline --graph --decorate --all co = checkout sw = switch br = branch -vv ci = commit dc = diff --cached rb = rebase cp = cherry-pick aa = add -A undo = reset --soft HEAD~1 last = show -1 --stat
附:常见工作流模板 A. 常规功能分支
1 2 3 4 5 6 git switch -c feature/x git push -u origin feature/x git switch main && git pull --ff-only && git branch -d feature/x
B. Release 切分支
1 2 3 4 git switch -c release/1.2.0 git tag -a v1.2.0 -m "release 1.2.0" git push origin release/1.2.0 v1.2.0
C. 从 main 同步更新到长期分支
1 2 3 git switch longrun git fetch origin git merge origin/main
提示 :
公共分支避免 --force,若必须请用 --force-with-lease;
习惯 reflog,关键时刻能救命;
通过 PR / MR 平台做 Code Review 与发布管理;
与团队约定:分支命名、提交信息规范(如 Conventional Commits)、发布流程。