Git 常用命令速查手册
一、仓库管理
命令说明
| git init |
在当前目录初始化一个新的 Git 仓库 |
| git init [project-name] |
新建目录并初始化为 Git 仓库 |
| git clone [url] |
克隆远程仓库到本地(含完整历史) |
二、配置
命令说明
| git config –list |
显示当前所有配置 |
| git config -e [–global] |
编辑配置文件(系统/全局/本地) |
| git config [–global] user.name "[name]" |
设置用户名 |
| git config [–global] user.email "[email]" |
设置用户邮箱 |
| git config [–global] core.editor "[editor]" |
设置默认编辑器(如 vim) |
| git config [–global] alias.[alias-name] "[command]" |
创建命令别名(如 git st = git status) |
三、基本快照操作
3.1 添加文件到暂存区(git add)
命令说明
| git add [file] |
添加指定文件到暂存区 |
| git add [dir] |
添加整个目录(递归) |
| git add . |
添加当前目录所有修改(含新增)到暂存区 |
| git add -p |
交互式分块暂存(适合精细控制) |
| git add -u |
只添加已跟踪文件的修改(不包含新文件) |
3.2 提交(git commit)
命令说明
| git commit -m "[message]" |
提交暂存区内容 |
| git commit [file1] [file2] -m "[msg]" |
提交暂存区中的指定文件 |
| git commit -a -m "[msg]" |
跳过暂存,直接提交所有已跟踪文件的修改 |
| git commit –amend -m "[new msg]" |
修正上一次提交(可修改信息或补充文件) |
| git commit –amend –no-edit |
补充文件到上一次提交,不修改信息 |
3.3 删除与重命名
命令说明
| git rm [file] |
删除工作区文件并放入暂存区(相当于删除+add) |
| git rm –cached [file] |
停止追踪文件,但保留在工作区(常用于 .gitignore) |
| git mv [old] [new] |
重命名文件,并自动暂存 |
四、分支操作
4.1 查看与创建分支
命令说明
| git branch |
列出所有本地分支(当前分支标 *) |
| git branch -r |
列出所有远程分支 |
| git branch -a |
列出本地 + 远程所有分支 |
| git branch [branch-name] |
创建新分支(但不切换) |
| git branch [branch] [commit] |
基于指定提交创建分支 |
| git branch –track [branch] [remote-branch] |
创建分支并建立追踪关系 |
| git branch -m [old] [new] |
重命名分支(若已在远程需重推) |
4.2 切换分支
命令说明
| git checkout [branch] |
切换分支(同时更新工作区) |
| git checkout -b [new-branch] |
创建并切换到新分支 |
| git checkout – |
切换到上一个分支 |
| git switch [branch] |
(Git 2.23+)切换分支,更安全的替代 checkout |
| git switch -c [new-branch] |
创建并切换(替代 checkout -b) |
4.3 合并与摘取
命令说明
| git merge [branch] |
将指定分支合并到当前分支 |
| git merge –no-ff [branch] |
强制产生合并提交(即使可以快进) |
| git cherry-pick [commit] |
将某个提交的改动应用到当前分支(选摘) |
| git cherry-pick [commit1]..[commit2] |
选摘一个区间内的所有提交 |
4.4 删除分支
命令说明
| git branch -d [branch] |
删除已合并的分支(安全) |
| git branch -D [branch] |
强制删除分支(无论是否合并) |
| git push origin –delete [branch] |
删除远程分支 |
| git branch -dr [remote/branch] |
删除本地追踪的远程分支引用 |
4.5 分支移动(高级)
命令说明
| git branch -f [branch] [target] |
强制将分支指针移动到指定提交(target 可为 commit hash 或 HEAD~n) |
| git reset –hard [target] |
将当前分支指针移动到 target,并同步重置暂存区和工作区(危险) |
| git reset –soft [target] |
移动分支指针,保留暂存区和工作区不变(修改仍在暂存区) |
| git reset –mixed [target] |
移动分支指针,重置暂存区但保留工作区(默认行为) |
| git reset –keep [target] |
移动分支指针,若工作区修改与目标不冲突则保留,否则中止 |
分支移动的本质:改变分支引用指向的 commit。常用于撤销、回滚、整理历史。
五、标签(Tag)
命令说明
| git tag |
列出所有标签 |
| git tag [tag] |
创建轻量标签(当前 HEAD) |
| git tag [tag] [commit] |
给指定提交打轻量标签 |
| git tag -a [tag] -m "[msg]" |
创建附注标签(推荐) |
| git tag -d [tag] |
删除本地标签 |
| git push origin [tag] |
推送单个标签到远程 |
| git push origin –tags |
推送所有本地标签 |
| git push origin :refs/tags/[tag] |
删除远程标签 |
| git checkout -b [branch] [tag] |
基于标签创建新分支 |
六、查看信息(日志、差异、状态)
命令说明
| git status |
显示工作区和暂存区的状态 |
| git log |
显示当前分支的提交历史 |
| git log –oneline |
单行简洁日志 |
| git log –graph –all –decorate |
图形化显示所有分支历史 |
| git log -p [file] |
显示指定文件的每次修改 diff |
| git log –follow [file] |
跟踪文件重命名历史 |
| git log -S [keyword] |
按关键词搜索提交 |
| git log –grep "[pattern]" |
按提交信息搜索 |
| git diff |
工作区 vs 暂存区差异 |
| git diff –cached |
暂存区 vs 最近提交差异 |
| git diff HEAD |
工作区 vs 最近提交差异 |
| git diff [commit1] [commit2] |
两次提交之间的差异 |
| git diff [branch1] [branch2] |
两个分支最新提交的差异 |
| git show [commit] |
显示某次提交的详细变更 |
| git show [commit]:[file] |
查看某次提交中指定文件的内容 |
| git blame [file] |
逐行显示文件的修改者及提交信息 |
| git reflog |
显示所有 HEAD 变动记录(可用于找回丢失提交) |
| git shortlog -sn |
按提交数量排序作者 |
七、撤销与恢复
7.1 工作区撤销
命令说明
| git checkout — [file] |
丢弃工作区中指定文件的修改(恢复至最近 commit) |
| git checkout — . |
丢弃所有工作区修改 |
| git restore [file] |
(Git 2.23+)丢弃工作区修改 |
7.2 暂存区撤销
命令说明
| git reset HEAD [file] |
取消暂存(从暂存区移除,保留工作区修改) |
| git restore –staged [file] |
同上(新版命令) |
7.3 提交撤销
命令说明
| git reset –soft HEAD~n |
撤销最近 n 次提交,修改保留在暂存区 |
| git reset –mixed HEAD~n |
撤销提交,修改保留在工作区(未暂存) |
| git reset –hard HEAD~n |
彻底丢弃最近 n 次提交的所有修改(危险!) |
| git revert [commit] |
创建一个新提交来撤销指定提交(安全,不改变历史) |
| git commit –amend |
修改上一次提交(可调整内容和信息) |
7.4 临时保存(Stash)
命令说明
| git stash |
将当前未提交的修改暂存起来 |
| git stash save "[msg]" |
暂存并加备注 |
| git stash list |
查看所有 stash 列表 |
| git stash pop |
恢复最近一次 stash 并删除该记录 |
| git stash apply |
恢复最近一次 stash 但不删除 |
| git stash apply stash@{n} |
恢复指定的 stash |
| git stash drop stash@{n} |
删除指定 stash |
| git stash clear |
删除所有 stash |
| git stash branch [branch] |
从 stash 创建新分支 |
7.5 找回丢失的提交
命令说明
| git reflog |
查看所有 HEAD 移动历史,找到丢失的 commit hash |
| git checkout [hash] |
检出该提交 |
| git branch [new-branch] [hash] |
基于该提交创建新分支以恢复 |
八、远程同步(仅作参考,本地仓库可忽略)
命令说明
| git remote -v |
查看远程仓库地址 |
| git remote add [name] [url] |
添加远程仓库 |
| git fetch [remote] |
下载远程所有变动(不自动合并) |
| git pull [remote] [branch] |
拉取并合并远程分支 |
| git push [remote] [branch] |
推送本地分支到远程 |
| git push [remote] –force |
强制推送(危险,会覆盖远程历史) |
| git push [remote] –all |
推送所有分支 |
| git push [remote] –tags |
推送所有标签 |
九、清理与维护
命令说明
| git clean -n |
预览将被删除的未跟踪文件 |
| git clean -fd |
删除所有未跟踪的文件和目录 |
| git clean -fdx |
同时删除被 .gitignore 忽略的文件(慎用) |
| git gc –aggressive –prune=now |
垃圾回收,压缩历史,清理无用对象 |
十、历史改写(Rebase 等)
命令说明
| git rebase [base] |
将当前分支的提交变基到 base 之上 |
| git rebase -i [commit] |
交互式变基(可合并、修改、删除提交) |
| git rebase –abort |
取消正在进行的 rebase |
| git rebase –continue |
解决冲突后继续 rebase |
| git cherry-pick [commit] |
将指定提交应用到当前分支(同上) |
| git filter-branch |
批量修改历史(如删除大文件) |
十一、其他实用命令
命令说明
| git archive –format=tar.gz HEAD > project.tar.gz |
从当前 HEAD 打包生成发布压缩包 |
| git help [command] |
查看指定命令的帮助文档 |
| git –version |
查看 Git 版本 |
十二、补充:分支移动的典型场景
场景 1:回退到上一版本
git reset –hard HEAD~1
场景 2:移动分支到另一个提交(不切换)
git branch -f feature develop
场景 3:撤销已推送的提交(但保留远程历史)
git reset –hard HEAD~1
git push –force-with-lease
场景 4:将某个功能分支的多个提交合并为一个(交互式 rebase)
git rebase -i HEAD~3
场景 5:从历史中永久删除敏感文件
bash
git filter-branch –force –index-filter \\
"git rm –cached –ignore-unmatch secret.txt" \\
–prune-empty –tag-name-filter cat — –all
附录:常用别名配置
bash
git config –global alias.co checkout
git config –global alias.br branch
git config –global alias.ci commit
git config –global alias.st status
git config –global alias.lg "log –graph –pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' –abbrev-commit"
git config –global alias.unstage "reset HEAD –"
使用示例:git lg 显示漂亮的分支图,git unstage file 取消暂存。
1. 提交(git commit)补充
命令说明
| git commit -v |
提交时在编辑器中显示详细的 diff 变化(便于审查) |
2. 分支(git branch)补充
命令说明
| git branch –set-upstream [branch] [remote-branch] |
手动为本地分支建立与远程分支的追踪关系(注意:新版 Git 推荐使用 –set-upstream-to) |
| git branch –contains [commit] |
查看哪些分支包含了指定的提交(常用于排查标签或修复是否已合并) |
| git merge –abort |
合并产生冲突时,放弃本次合并,恢复到合并前的状态 |
3. 查看信息(git log / git diff)补充
命令说明
| git log –stat |
显示每次提交的简略文件变更统计(修改了多少文件,增删行数) |
| git log -5 –pretty –oneline |
显示最近 5 次提交(-5 等同于 -n 5,整理版仅提了 -n) |
| git diff –shortstat "@{0 day ago}" |
显示从今天零点到现在的代码改动统计(写了多少行) |
| git whatchanged [file] |
显示文件的版本历史(与 git log –follow 功能重叠,属于旧式命令) |
4. 撤销(git reset)补充
命令说明
| git reset –keep [commit] |
重置 HEAD 到指定提交,但保留工作区的本地修改(如果本地修改与重置目标冲突则报错中止) |
| git checkout [commit] [file] |
从指定提交中恢复特定文件到工作区(整理版写的是 git restore –source,但原始命令为 checkout,需补充别名) |
5. 暂存(git stash)补充
命令说明
| git stash show -p stash@{0} |
查看指定暂存条目与创建时的详细代码差异(-p 即 –patch) |
6. 标签与合并联动补充
命令说明
| git merge [tag] |
直接合并标签指向的提交到当前分支(整理版仅写了合并分支,未明确写出合并标签) |
| git tag –merged / git tag –no-merged |
查看哪些标签已/未合并到当前分支 |
补充说明(避免操作误解)
🔹 补充 1:分支切换时的文件变化机制
- 原理:执行 git checkout [branch] 或 git switch [branch] 时,Git 会删除旧分支有而新分支没有的文件,并创建新分支有而旧分支没有的文件。
- 切换失败条件:如果工作区存在未提交的修改,且该文件在目标分支中已被修改,Git 会拒绝切换(防止覆盖丢失),必须先用 git stash 或 git commit 处理。
- 补充命令:强制切换 git checkout –force [branch](危险,会丢失未提交修改,应避免使用)。
🔹 补充 2:合并的目标分支没有限制
- 原则:git merge [branch] 是将其他分支(或标签)合并到当前所在的分支。当前分支可以是 main、develop、甚至是另一个功能分支,没有必须合并到主分支的限制。
- 典型流程:git checkout develop → git merge feature-login(将功能分支合入开发分支)。
🔹 补充 3:“放弃本次所有修改”的完整命令矩阵(整合版)
虽然整理版有涉及,但为了快速查阅,特将原始专题中的完整决策表补充如下:
你的目标命令组合(推荐)说明
| 仅丢弃工作区改动(未 add) |
git restore . 或 git checkout — . |
保留暂存区不变 |
| 仅取消暂存(已 add,保留改动) |
git restore –staged . |
修改回到工作区(红色状态) |
| 彻底放弃所有未提交的改动(工作区+暂存区) |
git reset –hard HEAD |
回到最新提交的干净状态 |
| 彻底放弃 + 删除新建文件(未跟踪文件) |
git reset –hard HEAD && git clean -fd |
最强重置,回到刚 clone 或 init 后的状态 |
| 预览将被删除的未跟踪文件 |
git clean -n |
执行 clean 前的安全检查 |
完整命令速查增补表
– `git commit -v`
– `git log –stat`
– `git diff –shortstat "@{0 day ago}"`
– `git merge –abort`
– `git merge [tag-name]`
– `git branch –contains [commit]`
– `git branch –set-upstream [branch] [remote/branch]`
– `git reset –keep [commit]`
– `git checkout [commit] — [file]`
– `git stash show -p stash@{0}`
– `git tag –merged`
– `git tag –no-merged`