Git 常用命令完整笔记
整理自阮一峰《常用 Git 命令清单》、阿里云开发者社区《Git 系统性使用指南》等资源
涵盖基础配置、日常操作、分支管理、远程协作、撤销回退、标签、高级技巧等全维度内容
目录
1. Git 四区概念
Git 有四个工作区域:
| 区域 | 说明 |
|---|---|
| Workspace | 工作区(本地文件系统) |
| Index / Stage | 暂存区 |
| Repository | 本地仓库 |
| Remote | 远程仓库 |
基本流程:工作区 → git add → 暂存区 → git commit → 本地仓库 → git push → 远程仓库
2. 新建代码库
# 在当前目录新建 Git 代码库
git init
# 新建一个目录,将其初始化为 Git 代码库
git init [project-name]
# 下载一个项目和它的整个代码历史
git clone [url]
# 克隆指定分支
git clone -b [branch] [url]
# 浅克隆(只克隆最新提交,加速大仓库克隆)
git clone --depth=1 [url]
# 递归克隆(含子模块)
git clone --recursive [url]
3. 配置
# 显示当前 Git 配置
git config --list
# 编辑 Git 配置文件
git config -e [--global]
# 设置提交代码时的用户信息
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"
# 设置默认编辑器
git config --global core.editor "vim"
# 设置别名
git config --global alias.st status
git config --global alias.cm commit
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ps push
# 记住账号密码(只需输入一次)
git config --global credential.helper store
# 设置自动建立上游关联(首次推送免 -u)
git config --global push.autoSetupRemote true
# 设置换行符处理
git config --global core.autocrlf input # macOS/Linux
git config --global core.autocrlf true # Windows
4. 增加/删除文件
# 添加指定文件到暂存区
git add [file1] [file2] ...
# 添加指定目录到暂存区(包括子目录)
git add [dir]
# 添加当前目录的所有文件到暂存区
git add .
# 添加所有变化(新增、修改、删除)
git add -A
# 交互式选择添加(每个变化前确认,可分次提交同一文件)
git add -p
# 删除工作区文件,并将此次删除放入暂存区
git rm [file1] [file2] ...
# 停止追踪指定文件,但保留在工作区
git rm --cached [file]
# 清除所有文件跟踪
git rm -r --cached .
# 改名文件,并将此改名放入暂存区
git mv [file-original] [file-renamed]
5. 代码提交
# 提交暂存区到仓库区
git commit -m "message"
# 提交暂存区的指定文件到仓库区
git commit [file1] [file2] ... -m "message"
# 提交工作区自上次 commit 之后的变化(跳过 add,仅限已跟踪文件)
git commit -a
# 提交时显示所有 diff 信息
git commit -v
# 修改最近一次提交信息(如果代码无变化,则改写提交信息)
git commit --amend -m "new message"
# 重做上一次 commit,并包含指定文件的新变化
git commit --amend [file1] [file2] ...
# 创建空提交
git commit --allow-empty -m "init"
6. 分支管理
6.1 查看分支
# 列出所有本地分支
git branch
# 列出所有远程分支
git branch -r
# 列出所有本地和远程分支
git branch -a
# 查看分支与对应远程分支的关联(推荐)
git branch -vv
# 查看已合并的分支
git branch --merged
# 查看未合并的分支
git branch --no-merged
6.2 创建与切换分支
# 新建一个分支(停留在当前分支)
git branch [branch-name]
# 新建一个分支,指向指定 commit
git branch [branch-name] [commit]
# 切换分支
git checkout [branch-name]
# 切换到上一个分支
git checkout -
# 创建并切换分支(推荐)
git checkout -b [branch-name]
# 新版切换命令(Git 2.23+)
git switch [branch-name]
git switch -c [branch-name] # 创建并切换
# 根据远程分支创建本地分支
git checkout -b [branch] origin/[branch]
git checkout --track origin/[branch]
6.3 合并分支
# 合并指定分支到当前分支
git merge [branch]
# 强制创建合并提交(不使用快进模式)
git merge --no-ff [branch]
# 压缩合并为一个提交
git merge --squash [branch]
# 中止合并(解决冲突时放弃)
git merge --abort
6.4 删除分支
# 删除已合并的本地分支
git branch -d [branch-name]
# 强制删除未合并分支
git branch -D [branch-name]
# 删除远程分支
git push origin --delete [branch-name]
git push origin :[branch-name] # 旧语法
git branch -dr [remote/branch]
6.5 分支重命名
# 重命名当前分支
git branch -m [new-name]
# 重命名指定分支
git branch -m [old-name] [new-name]
6.6 Cherry-Pick
# 选择一个 commit 合并到当前分支
git cherry-pick [commit]
# 选择多个 commit
git cherry-pick [commit1] [commit2]
7. 标签管理
# 列出所有标签
git tag
# 查看标签详细信息
git show [tag]
# 在当前 commit 创建轻量标签
git tag [tag]
# 在指定 commit 创建标签
git tag [tag] [commit]
# 创建附注标签(推荐用于版本发布)
git tag -a [tag] -m "description"
# 删除本地标签
git tag -d [tag]
# 删除远程标签
git push origin :refs/tags/[tagName]
git push origin --delete tag [tagName]
# 推送指定标签到远程
git push origin [tag]
# 推送所有未推送的标签到远程
git push origin --tags
# 基于标签创建分支
git checkout -b [branch] [tag]
8. 查看信息与历史
8.1 状态与差异
# 显示有变更的文件
git status
git status -s # 简洁模式
# 显示暂存区与工作区的差异
git diff
# 显示暂存区与上一个 commit 的差异
git diff --staged
git diff --cached
# 显示工作区与当前分支最新 commit 的差异
git diff HEAD
# 显示两次提交之间的差异
git diff [first-branch]...[second-branch]
# 显示今天写了多少行代码
git diff --shortstat "@{0 day ago}"
8.2 日志历史
# 显示当前分支的提交历史
git log
# 单行简洁显示
git log --oneline
# 图形化显示分支合并历史
git log --graph --oneline --all
# 显示 commit 历史及每次变更文件
git log --stat
# 显示每次提交的变更详情(diff)
git log -p
# 搜索提交历史(根据关键词)
git log -S [keyword]
# 根据提交信息搜索(--grep)
git log --grep [keyword]
# 根据作者搜索
git log --author [name]
# 显示某个文件的版本历史(含改名)
git log --follow [file]
# 显示指定文件每次 commit 的 diff
git log -p [file]
# 显示最近 N 次提交
git log -5 --pretty --oneline
# 显示所有提交过的用户,按提交次数排序
git shortlog -sn
# 显示 HEAD 指针移动记录(找回丢失的提交)
git reflog
git reflog --all
8.3 文件追溯
# 显示指定文件每行是谁在什么时间修改的
git blame [file]
# 显示某次提交的元数据和内容变化
git show [commit]
# 显示某次提交发生变化的文件
git show --name-only [commit]
# 显示某次提交时某个文件的内容
git show [commit]:[filename]
# 列出 Git 当前跟踪的文件
git ls-files
9. 远程同步
9.1 远程仓库管理
# 显示所有远程仓库
git remote -v
# 查看某个远程仓库的详细信息
git remote show [remote]
# 增加一个新的远程仓库
git remote add [shortname] [url]
# 修改远程仓库地址
git remote set-url origin [new-url]
# 删除远程仓库
git remote rm [remote]
# 重命名远程仓库
git remote rename [old-name] [new-name]
9.2 拉取与推送
# 下载远程仓库所有变动(不合并)
git fetch [remote]
git fetch [remote] [branch]
# 取回远程仓库变化并与本地分支合并
git pull [remote] [branch]
git pull # 当前分支的默认拉取
git pull --rebase # 拉取后使用 rebase 而非 merge
# 上传本地分支到远程仓库
git push [remote] [branch]
# 首次推送并建立追踪关系
git push -u origin [branch]
# 强行推送(即使有冲突,慎用!)
git push [remote] --force
# 推送所有分支到远程
git push [remote] --all
# 清理远程已删除分支的本地缓存
git fetch -p
git remote prune origin
10. 撤销与回退
10.1 工作区撤销
# 撤销工作区的修改(用暂存区覆盖工作区)
git checkout -- [file]
# 恢复到指定 commit 的文件版本
git checkout [commit] [file]
# 撤销所有未暂存的修改
git checkout .
10.2 暂存区撤销
# 将文件从暂存区移回工作区
git reset HEAD [file]
git restore --staged [file] # 新版命令(Git 2.23+)
10.3 提交撤销
# 回退到上一个 commit(保留工作区修改)
git reset --soft HEAD~1
# 回退到上一个 commit(撤销暂存区,保留工作区修改,默认模式)
git reset HEAD~1
git reset --mixed HEAD~1
# 彻底回退到指定 commit(丢弃所有修改,高危!)
git reset --hard [commit]
git reset --hard HEAD~1
# 回退但保持暂存区和工作区不变
git reset --keep [commit]
10.4 Revert(安全撤销)
# 创建一个新的 commit 来撤销指定的 commit(保留历史记录)
git revert [commit]
# 撤销最近一次提交
git revert HEAD
10.5 Stash(临时保存)
# 暂存当前工作区修改
git stash
git stash save "description"
# 暂存当前修改(包含未跟踪文件)
git stash push -u -m "description"
# 查看所有暂存记录
git stash list
# 恢复最近一次 stash(并删除)
git stash pop
# 恢复指定 stash(不删除)
git stash apply stash@{0}
# 删除指定 stash
git stash drop stash@{0}
# 清空所有 stash
git stash clear
11. 高级操作
11.1 Rebase(变基)
# 将当前分支变基到目标分支顶部
git rebase [base-branch]
# 交互式变基(修改、合并、删除历史提交)
git rebase -i HEAD~3
# 中止变基
git rebase --abort
# 变基冲突解决后继续
git rebase --continue
11.2 Bisect(二分查找 Bug)
# 启动二分查找
git bisect start
# 标记当前提交有 Bug
git bisect bad
# 标记已知正常版本
git bisect good [commit]
# 退出二分查找
git bisect reset
11.3 子模块
# 添加子模块
git submodule add [url] [path]
# 初始化子模块
git submodule init
# 更新子模块
git submodule update
# 递归克隆并更新子模块
git clone --recursive [url]
11.4 清理
# 删除未跟踪的文件
git clean -f
# 预览将要删除的文件
git clean -n
# 删除未跟踪的文件和目录
git clean -fd
12. 常见问题与技巧
12.1 修正已推送的提交
# 1. 修正本地 commit
git commit --amend -m "new message"
# 2. 强制推送(覆盖远程)
git push --force-with-lease # 比 --force 更安全
12.2 合并不相关历史
# 当两个仓库没有共同祖先时
git pull origin main --allow-unrelated-histories
git merge --allow-unrelated-histories
12.3 gitignore 配置示例
# 忽略所有 .log 文件
*.log
# 但保留 tracking.log
!tracking.log
# 忽略 build 目录
build/
# 忽略 node_modules
node_modules/
# 忽略环境文件
.env
.env.local
12.4 恢复误删分支
# 查看 HEAD 历史
git reflog
# 找到丢失分支的最后一个 commit,重建分支
git checkout -b [restored-branch] [commit-hash]
12.5 从所有提交历史中删除文件
git filter-branch --tree-filter "rm -f [file]" --prune-empty -- --all
# 如果已推送需强制推送
git push -f
12.6 清理已合并的本地分支
git branch --merged main | grep -v "main" | xargs -n 1 git branch -d
12.7 查看分支创建时间
# 查看当前分支
git reflog --date=iso
# 查看指定分支
git reflog --date=iso [branch]
12.8 文件操作进阶
# 对比两个分支文件的差异
git diff [branch1] [branch2] -- [file]
# 从所有提交中搜索某个字符串
git grep [keyword] [branch]
Git 常用命令速查(一页通)
| 分类 | 命令 | 说明 |
|---|---|---|
| 初始化 | git init |
初始化仓库 |
| 克隆 | git clone <url> |
克隆远程仓库 |
| 配置 | git config --global user.name/email |
配置用户信息 |
| 添加 | git add . |
添加所有文件到暂存区 |
| 提交 | git commit -m "msg" |
提交暂存区到仓库 |
| 推送 | git push |
推送本地提交到远程 |
| 拉取 | git pull |
拉取并合并远程更新 |
| 获取 | git fetch |
获取远程更新但不合并 |
| 状态 | git status |
查看仓库状态 |
| 日志 | git log --oneline --graph |
查看简洁图形化历史 |
| 分支 | git branch -a |
查看所有分支 |
| 切换 | git checkout <branch> |
切换分支 |
| 创建分支 | git checkout -b <branch> |
创建并切换分支 |
| 合并 | git merge <branch> |
合并分支到当前 |
| 暂存 | git stash |
临时保存工作区 |
| 恢复暂存 | git stash pop |
恢复并删除暂存 |
| 回退 | git reset --hard HEAD~1 |
彻底回退到上一个版本 |
| 安全回退 | git revert HEAD |
通过新提交撤销 |
| 标签 | git tag -a v1.0 -m "msg" |
创建附注标签 |
| 推送标签 | git push origin --tags |
推送所有标签 |
| 远程 | git remote -v |
查看远程仓库 |
参考来源:
- 阮一峰《常用 Git 命令清单》 — https://www.ruanyifeng.com/blog/2015/12/git-cheat-sheet.html
- 阿里云开发者社区《Git 系统性使用指南》 — https://developer.aliyun.com/article/1717378
- 百度云《Git 详细使用教程:从入门到精通的完整指南》