一、初始化与配置

操作命令
初始化仓库git init
克隆仓库git clone <url>
设置用户名git config user.name "…"
设置邮箱git config user.email "…"
查看所有配置git config --list

二、工作区与暂存区

操作命令
查看状态(详细)git status
查看状态(简洁)git status -s
添加指定文件git add <file>
添加所有变动git add .
交互式分块添加git add -p
撤销暂存(新版)git restore --staged <file>
撤销暂存(旧版)git reset HEAD <file>
丢弃工作区修改(新版)git restore <file>
丢弃工作区修改(旧版)git checkout -- <file>
丢弃所有未提交改动git reset --hard HEAD

三、提交

操作命令
提交暂存区git commit -m "message"
跳过 add 直接提交已跟踪文件git commit -a -m "message"
修正上一次提交(不改 message)git commit --amend --no-edit
修正上一次提交并改 messagegit commit --amend -m "new msg"
追加修改到上一次提交(需先 add)git commit --amend --no-edit(先 add)

四、分支

操作命令
列出本地分支git branch
列出所有分支(含远程跟踪)git branch -a
查看分支最后提交git branch -v
查看已合并到当前分支的分支git branch --merged
创建分支git branch <name>
基于指定提交创建分支git branch <name> <commit>
创建并切换到新分支git switch -c <name>git checkout -b <name>
切换分支git switch <name>git checkout <name>
安全删除分支(已合并)git branch -d <name>
强制删除分支git branch -D <name>
重命名分支git branch -m <old> <new>

五、合并与变基

操作命令
合并指定分支到当前git merge <branch>
禁止快进合并(保留分支记录)git merge --no-ff <branch>
压缩合并且不自动提交git merge --squash <branch>(还需手动 commit)
取消正在进行的合并git merge --abort
变基当前分支到目标git rebase <base>
交互式变基最近 N 个提交git rebase -i HEAD~N
解决冲突后继续变基git rebase --continue
跳过正在变基的当前提交git rebase --skip
取消正在进行的变基git rebase --abort

六、远程仓库

操作命令
查看远程仓库列表git remote -v
添加远程仓库git remote add <name> <url>
移除远程仓库git remote remove <name>
重命名远程仓库git remote rename <old> <new>
获取所有远程更新(不合并)git fetch <remote>
获取并清理已删除的远程分支引用git fetch -p
拉取并合并远程分支git pull
拉取并通过变基整合git pull --rebase
推送分支到远程git push <remote> <branch>
推送并设置上游跟踪git push -u <remote> <branch>
安全强制推送(检查冲突)git push --force-with-lease
强制推送(覆盖远程历史,危险)git push --force

七、查看历史

操作命令
完整提交历史git log
单行简洁历史git log --oneline
可视化所有分支历史git log --graph --oneline --decorate --all
查看某文件的提交历史及差异git log -p <file>
按作者过滤git log --author="name"
按时间范围过滤git log --since="2024-01-01"
搜索内容变更(某字符串增删)git log -S"keyword"
文件逐行归属git blame <file>
查看某次提交详情git show <commit>
比较两次提交差异git diff <commit1> <commit2>
查看暂存区与上次提交的差异git diff --staged

八、撤销与回退

操作命令
安全撤销某次提交(新建反向提交)git revert <commit>
回退到指定提交,保留暂存区和工作区git reset --soft <commit>
回退并清空暂存区,保留工作区git reset --mixed <commit>(默认)
彻底回退并丢弃所有修改git reset --hard <commit>
查看 HEAD 变动记录(找回丢失提交)git reflog
恢复单个文件到指定版本git checkout <commit> -- <file>

九、暂存(Stash)

操作命令
暂存当前修改git stash
暂存并添加说明git stash -m "message"
查看暂存列表git stash list
恢复最近一次暂存并删除记录git stash pop
恢复指定暂存git stash pop stash@{n}
删除某次暂存git stash drop stash@{n}
清空所有暂存git stash clear

十、标签

操作命令
列出所有标签git tag
创建轻量标签(指向当前提交)git tag <name>
创建附注标签git tag -a <name> -m "message"
给历史提交打标签git tag -a <name> <commit>
推送所有标签git push --tags
推送指定标签git push <remote> <tag>
删除本地标签git tag -d <name>
删除远程标签git push --delete <remote> <tag>

十一、清理与维护

操作命令
垃圾回收git gc
删除不可达对象git prune
列出未跟踪文件(预览)git clean -n
删除未跟踪文件git clean -f
删除未跟踪文件和目录git clean -fd

十二、常用组合技巧

场景命令
取消最近一次提交但保留修改git reset --soft HEAD~1
修改最近一次提交的作者git commit --amend --author="Name <email>"
将当前分支强制对齐到远程主干git reset --hard origin/main
查看两分支的差异文件列表git diff --name-only branch1..branch2
将某个提交应用到当前分支git cherry-pick <commit-hash>
连续 cherry-pick 一个范围(不含 A)git cherry-pick A..B