Git 常用命令行开发测试速查
面向日常开发、联调、测试、发布和问题排查场景。每个场景都给出 macOS/Linux 与 Windows PowerShell 命令。大多数 Git 命令跨平台一致,差异主要在路径、环境变量、Shell 语法和文件删除命令。
使用约定
示例分支名:
main、develop、feature/login、bugfix/order-timeout、release/1.2.0。示例远程名:
origin。示例文件:
src/app.js、README.md。示例提交:
abc1234、HEAD~1。Windows 命令默认使用 PowerShell。
高风险命令包括
reset --hard、clean -fd、强制推送、删除分支、删除标签。执行前建议先运行查看类命令确认影响范围。
目录
01. 检查 Git 环境
02. 获取帮助
03. 首次配置用户信息
04. 配置默认分支和常用偏好
05. 配置换行符策略
06. 配置凭证缓存
07. 初始化本地仓库
08. 克隆远程仓库
09. 查看仓库状态
10. 查看当前分支和远程关系
11. 暂存文件
12. 取消暂存
13. 提交代码
14. 修改最近一次提交
15. 查看提交历史
16. 查看文件历史
17. 查看差异
18. 创建和切换分支
19. 重命名和删除分支
20. 拉取远程更新
21. 推送本地分支
22. 同步远程分支列表
23. 合并分支
24. 变基分支
25. 解决冲突
26. 暂存当前工作进度
27. 恢复或丢弃本地修改
28. 回退本地提交
29. 生成反向提交回滚
30. 找回误删提交
31. 拣选提交
32. 打标签和推送标签
33. 删除标签
34. 管理远程仓库地址
35. 使用补丁文件
36. 清理未跟踪文件
37. 忽略文件和检查忽略规则
38. 查看某行代码是谁改的
39. 二分定位问题提交
40. 多工作区并行开发
41. 子模块常用操作
42. 大文件 Git LFS
43. 测试前同步指定分支
44. 只测试本次变更文件
45. 查看两个版本之间的提交
46. 查找包含某段代码的提交
47. 查找引入或删除关键字的提交
48. 临时切到历史版本验证问题
49. 发布分支和版本分支
50. 常见安全命令组合
01. 检查 Git 环境
用途:确认当前机器是否安装 Git,并查看版本、路径和当前目录。
macOS/Linux:
git --version which git pwd
Windows PowerShell:
git --version Get-Command git Get-Location
02. 获取帮助
用途:查看某个 Git 子命令的官方帮助和简短说明。
macOS/Linux:
git help status git status -h git commit --help
Windows PowerShell:
git help status git status -h git commit --help
03. 首次配置用户信息
用途:配置提交记录中的作者姓名和邮箱。
macOS/Linux:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com" git config --global --list
Windows PowerShell:
git config --global user.name "Your Name" git config --global user.email "your.email@example.com" git config --global --list
04. 配置默认分支和常用偏好
用途:设置新仓库默认分支、命令输出颜色、拉取策略和默认编辑器。
macOS/Linux:
git config --global init.defaultBranch main git config --global color.ui auto git config --global pull.rebase false git config --global core.editor "vim"
Windows PowerShell:
git config --global init.defaultBranch main git config --global color.ui auto git config --global pull.rebase false git config --global core.editor "notepad"
05. 配置换行符策略
用途:减少跨平台协作时的 CRLF/LF 换行差异。
macOS/Linux:
git config --global core.autocrlf input git config --global core.eol lf git config --global --get core.autocrlf
Windows PowerShell:
git config --global core.autocrlf true git config --global core.eol native git config --global --get core.autocrlf
06. 配置凭证缓存
用途:减少 HTTPS 远程仓库反复输入账号或令牌。
macOS/Linux:
git config --global credential.helper cache git config --global credential.helper "cache --timeout=3600" git config --global --get credential.helper
Windows PowerShell:
git config --global credential.helper manager git config --global --get credential.helper
07. 初始化本地仓库
用途:把当前目录初始化为 Git 仓库,并创建第一次提交。
macOS/Linux:
mkdir my-project cd my-project git init git status
Windows PowerShell:
New-Item -ItemType Directory -Path my-project Set-Location my-project git init git status
08. 克隆远程仓库
用途:从远程地址下载仓库到本地。
macOS/Linux:
git clone https://example.com/team/project.git cd project git remote -v
Windows PowerShell:
git clone https://example.com/team/project.git Set-Location project git remote -v
09. 查看仓库状态
用途:查看分支、暂存区、未暂存改动和未跟踪文件。
macOS/Linux:
git status git status --short git status --branch --short
Windows PowerShell:
git status git status --short git status --branch --short
10. 查看当前分支和远程关系
用途:确认当前所在分支、所有分支和本地分支追踪的远程分支。
macOS/Linux:
git branch git branch -a git branch -vv
Windows PowerShell:
git branch git branch -a git branch -vv
11. 暂存文件
用途:把修改加入暂存区,准备提交。
macOS/Linux:
git add README.md git add src/app.js git add . git add -p
Windows PowerShell:
git add README.md git add src/app.js git add . git add -p
12. 取消暂存
用途:把文件从暂存区移出,但保留工作区修改。
macOS/Linux:
git restore --staged README.md git restore --staged . git status --short
Windows PowerShell:
git restore --staged README.md git restore --staged . git status --short
13. 提交代码
用途:提交已暂存的改动,形成一个新的提交记录。
macOS/Linux:
git add . git commit -m "feat: add login page" git status
Windows PowerShell:
git add . git commit -m "feat: add login page" git status
14. 修改最近一次提交
用途:补充漏掉的文件或修改最近一次提交说明。已经推送给他人的提交慎用。
macOS/Linux:
git add README.md git commit --amend git commit --amend -m "feat: add login page"
Windows PowerShell:
git add README.md git commit --amend git commit --amend -m "feat: add login page"
15. 查看提交历史
用途:查看提交列表、图形化分支关系和最近提交。
macOS/Linux:
git log git log --oneline --decorate --graph --all git log -5 --oneline
Windows PowerShell:
git log git log --oneline --decorate --graph --all git log -5 --oneline
16. 查看文件历史
用途:查看某个文件的提交演进。
macOS/Linux:
git log -- README.md git log --oneline -- README.md git log -p -- README.md
Windows PowerShell:
git log -- README.md git log --oneline -- README.md git log -p -- README.md
17. 查看差异
用途:比较工作区、暂存区、分支、提交之间的变化。
macOS/Linux:
git diff git diff --staged git diff main...feature/login git diff abc1234..HEAD
Windows PowerShell:
git diff git diff --staged git diff main...feature/login git diff abc1234..HEAD
18. 创建和切换分支
用途:为新需求、缺陷修复或测试验证创建独立分支。
macOS/Linux:
git switch main git pull git switch -c feature/login git branch --show-current
Windows PowerShell:
git switch main git pull git switch -c feature/login git branch --show-current
19. 重命名和删除分支
用途:整理本地分支,删除已合并或废弃分支。
macOS/Linux:
git branch -m old-name new-name git branch -d feature/login git branch -D feature/login git push origin --delete feature/login
Windows PowerShell:
git branch -m old-name new-name git branch -d feature/login git branch -D feature/login git push origin --delete feature/login
20. 拉取远程更新
用途:同步远程分支内容到本地。
macOS/Linux:
git fetch origin git pull origin main git pull --rebase origin main
Windows PowerShell:
git fetch origin git pull origin main git pull --rebase origin main
21. 推送本地分支
用途:把本地提交推送到远程,供他人评审、测试或部署。
macOS/Linux:
git push origin feature/login git push -u origin feature/login git push
Windows PowerShell:
git push origin feature/login git push -u origin feature/login git push
22. 同步远程分支列表
用途:删除本地已经失效的远程追踪分支引用。
macOS/Linux:
git fetch --prune git remote prune origin git branch -r
Windows PowerShell:
git fetch --prune git remote prune origin git branch -r
23. 合并分支
用途:把一个分支的提交合并到当前分支。
macOS/Linux:
git switch develop git pull git merge feature/login git status
Windows PowerShell:
git switch develop git pull git merge feature/login git status
24. 变基分支
用途:把当前分支的提交移动到目标分支最新提交之后,让历史更线性。多人共享分支慎用。
macOS/Linux:
git switch feature/login git fetch origin git rebase origin/main git status
Windows PowerShell:
git switch feature/login git fetch origin git rebase origin/main git status
25. 解决冲突
用途:合并或变基遇到冲突时,查看冲突文件、编辑、标记解决并继续。
macOS/Linux:
git status git diff git add src/app.js git merge --continue git rebase --continue
Windows PowerShell:
git status git diff git add src/app.js git merge --continue git rebase --continue
26. 暂存当前工作进度
用途:临时保存未提交修改,方便切分支、拉代码或处理紧急任务。
macOS/Linux:
git stash push -m "wip: login form" git stash list git stash pop git stash apply stash@{0}Windows PowerShell:
git stash push -m "wip: login form" git stash list git stash pop git stash apply "stash@{0}"27. 恢复或丢弃本地修改
用途:撤销工作区文件修改,或恢复误删文件。执行前确认不再需要这些改动。
macOS/Linux:
git restore README.md git restore . git restore --source=HEAD~1 -- README.md
Windows PowerShell:
git restore README.md git restore . git restore --source=HEAD~1 -- README.md
28. 回退本地提交
用途:回退尚未共享的本地提交。--hard会丢弃工作区和暂存区改动。
macOS/Linux:
git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1
Windows PowerShell:
git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1
29. 生成反向提交回滚
用途:回滚已经推送或已经进入共享分支的提交,适合测试环境和生产回滚。
macOS/Linux:
git revert abc1234 git revert HEAD git revert --no-commit abc1234..HEAD
Windows PowerShell:
git revert abc1234 git revert HEAD git revert --no-commit abc1234..HEAD
30. 找回误删提交
用途:通过引用日志找回 reset、rebase 或删除分支前的提交。
macOS/Linux:
git reflog git switch -c recover/old-work abc1234 git log --oneline -5
Windows PowerShell:
git reflog git switch -c recover/old-work abc1234 git log --oneline -5
31. 拣选提交
用途:把某个分支上的指定提交复制到当前分支,常用于把修复同步到测试分支或发布分支。
macOS/Linux:
git switch release/1.2.0 git cherry-pick abc1234 git cherry-pick --continue git cherry-pick --abort
Windows PowerShell:
git switch release/1.2.0 git cherry-pick abc1234 git cherry-pick --continue git cherry-pick --abort
32. 打标签和推送标签
用途:标记发布版本、测试基线或里程碑。
macOS/Linux:
git tag git tag -a v1.2.0 -m "release v1.2.0" git push origin v1.2.0 git push origin --tags
Windows PowerShell:
git tag git tag -a v1.2.0 -m "release v1.2.0" git push origin v1.2.0 git push origin --tags
33. 删除标签
用途:删除本地或远程错误标签。共享标签删除前应与团队确认。
macOS/Linux:
git tag -d v1.2.0 git push origin --delete v1.2.0 git ls-remote --tags origin
Windows PowerShell:
git tag -d v1.2.0 git push origin --delete v1.2.0 git ls-remote --tags origin
34. 管理远程仓库地址
用途:查看、添加、修改或删除远程仓库地址。
macOS/Linux:
git remote -v git remote add origin https://example.com/team/project.git git remote set-url origin git@example.com:team/project.git git remote remove origin
Windows PowerShell:
git remote -v git remote add origin https://example.com/team/project.git git remote set-url origin git@example.com:team/project.git git remote remove origin
35. 使用补丁文件
用途:在无法直接推送或需要离线传递变更时生成和应用补丁。
macOS/Linux:
git diff > change.patch git apply --check change.patch git apply change.patch git format-patch -1 abc1234
Windows PowerShell:
git diff > change.patch git apply --check change.patch git apply change.patch git format-patch -1 abc1234
36. 清理未跟踪文件
用途:清理编译产物、临时文件和未跟踪目录。先用-n预览,再执行删除。
macOS/Linux:
git clean -n git clean -fd git clean -fdx
Windows PowerShell:
git clean -n git clean -fd git clean -fdx
37. 忽略文件和检查忽略规则
用途:维护.gitignore,检查某个文件为什么被忽略。
macOS/Linux:
printf "node_modules/\ndist/\n.env\n" >> .gitignore git check-ignore -v .env git status --ignored
Windows PowerShell:
@("node_modules/","dist/",".env") | Add-Content .gitignore git check-ignore -v .env git status --ignored38. 查看某行代码是谁改的
用途:定位责任提交,辅助排查缺陷来源。
macOS/Linux:
git blame src/app.js git blame -L 10,40 src/app.js git show abc1234
Windows PowerShell:
git blame src/app.js git blame -L 10,40 src/app.js git show abc1234
39. 二分定位问题提交
用途:在大量提交中快速定位引入缺陷的提交。
macOS/Linux:
git bisect start git bisect bad git bisect good v1.1.0 git bisect reset
Windows PowerShell:
git bisect start git bisect bad git bisect good v1.1.0 git bisect reset
40. 多工作区并行开发
用途:在同一仓库同时检出多个分支,适合一边修缺陷一边跑测试。
macOS/Linux:
git worktree list git worktree add ../project-release release/1.2.0 git worktree remove ../project-release
Windows PowerShell:
git worktree list git worktree add ..\project-release release/1.2.0 git worktree remove ..\project-release
41. 子模块常用操作
用途:管理依赖仓库或嵌套仓库。
macOS/Linux:
git submodule status git submodule update --init --recursive git submodule foreach git pull
Windows PowerShell:
git submodule status git submodule update --init --recursive git submodule foreach git pull
42. 大文件 Git LFS
用途:管理模型、图片、安装包等大文件。需要先安装 Git LFS。
macOS/Linux:
git lfs install git lfs track "*.zip" git add .gitattributes git add assets/demo.zip git commit -m "chore: track zip files with lfs"
Windows PowerShell:
git lfs install git lfs track "*.zip" git add .gitattributes git add assets/demo.zip git commit -m "chore: track zip files with lfs"
43. 测试前同步指定分支
用途:测试人员在执行回归、冒烟或验收前同步目标分支到最新状态。
macOS/Linux:
git fetch origin git switch test git pull --ff-only origin test git log -1 --oneline
Windows PowerShell:
git fetch origin git switch test git pull --ff-only origin test git log -1 --oneline
44. 只测试本次变更文件
用途:列出相对目标分支发生变化的文件,辅助选择测试范围。
macOS/Linux:
git fetch origin git diff --name-only origin/main...HEAD git diff --name-status origin/main...HEAD
Windows PowerShell:
git fetch origin git diff --name-only origin/main...HEAD git diff --name-status origin/main...HEAD
45. 查看两个版本之间的提交
用途:生成测试范围、发布说明或变更清单。
macOS/Linux:
git log --oneline v1.1.0..v1.2.0 git log --pretty=format:"%h %an %s" v1.1.0..v1.2.0 git diff --stat v1.1.0..v1.2.0
Windows PowerShell:
git log --oneline v1.1.0..v1.2.0 git log --pretty=format:"%h %an %s" v1.1.0..v1.2.0 git diff --stat v1.1.0..v1.2.0
46. 查找包含某段代码的提交
用途:根据文件内容或函数名定位相关提交。
macOS/Linux:
git grep "login" git log -S "login" --oneline git log -G "function login" --oneline
Windows PowerShell:
git grep "login" git log -S "login" --oneline git log -G "function login" --oneline
47. 查找引入或删除关键字的提交
用途:定位某个配置、接口、字段、错误码从什么时候开始变化。
macOS/Linux:
git log -S "ERROR_CODE_1001" --all --oneline git log -p -S "ERROR_CODE_1001" --all
Windows PowerShell:
git log -S "ERROR_CODE_1001" --all --oneline git log -p -S "ERROR_CODE_1001" --all
48. 临时切到历史版本验证问题
用途:复现历史问题或确认某个版本是否存在缺陷。
macOS/Linux:
git switch --detach v1.1.0 git log -1 --oneline git switch main
Windows PowerShell:
git switch --detach v1.1.0 git log -1 --oneline git switch main
49. 发布分支和版本分支
用途:从主干创建发布分支,推送给测试、预发或生产流程。
macOS/Linux:
git switch main git pull --ff-only origin main git switch -c release/1.2.0 git push -u origin release/1.2.0
Windows PowerShell:
git switch main git pull --ff-only origin main git switch -c release/1.2.0 git push -u origin release/1.2.0
50. 常见安全命令组合
用途:在执行高风险操作前先备份、查看状态和确认差异。
macOS/Linux:
git status --short git branch backup/$(date +%Y%m%d-%H%M%S) git log --oneline -5 git diff --stat
Windows PowerShell:
git status --short $stamp = Get-Date -Format "yyyyMMdd-HHmmss" git branch "backup/$stamp" git log --oneline -5 git diff --stat
高频组合流程
新需求开发流程
macOS/Linux:
git switch main git pull --ff-only origin main git switch -c feature/new-feature git add . git commit -m "feat: add new feature" git push -u origin feature/new-feature
Windows PowerShell:
git switch main git pull --ff-only origin main git switch -c feature/new-feature git add . git commit -m "feat: add new feature" git push -u origin feature/new-feature
缺陷修复流程
macOS/Linux:
git switch develop git pull --ff-only origin develop git switch -c bugfix/order-timeout git add . git commit -m "fix: handle order timeout" git push -u origin bugfix/order-timeout
Windows PowerShell:
git switch develop git pull --ff-only origin develop git switch -c bugfix/order-timeout git add . git commit -m "fix: handle order timeout" git push -u origin bugfix/order-timeout
测试环境更新流程
macOS/Linux:
git fetch origin git switch test git pull --ff-only origin test git log -1 --oneline
Windows PowerShell:
git fetch origin git switch test git pull --ff-only origin test git log -1 --oneline
合并前自检流程
macOS/Linux:
git status --short git fetch origin git diff --stat origin/main...HEAD git log --oneline origin/main..HEAD
Windows PowerShell:
git status --short git fetch origin git diff --stat origin/main...HEAD git log --oneline origin/main..HEAD
发布打标流程
macOS/Linux:
git switch main git pull --ff-only origin main git tag -a v1.2.0 -m "release v1.2.0" git push origin v1.2.0
Windows PowerShell:
git switch main git pull --ff-only origin main git tag -a v1.2.0 -m "release v1.2.0" git push origin v1.2.0
命令选择建议
优先使用
git switch切换分支,使用git restore恢复文件;它们比旧式git checkout语义更清晰。共享分支优先使用
git revert回滚,避免改写别人已经基于它开发的提交历史。本地私有分支可以使用
git reset、git commit --amend、git rebase整理历史。拉取共享分支时,追求严格线性可使用
git pull --ff-only;需要整理本地提交可使用git pull --rebase。执行删除或清理前,先运行
git status、git log --oneline -5、git clean -n等预览命令。
常见问题速查
| 问题 | 推荐命令 |
|---|---|
| 想知道当前改了什么 | git status --short、git diff |
| 想知道暂存区里有什么 | git diff --staged |
| 提交后发现漏文件 | git add <file>、git commit --amend |
| 切分支前手头工作没做完 | git stash push -m "wip" |
| 想撤销已经推送的提交 | git revert <commit> |
| 想撤销本地最后一次提交但保留代码 | git reset --soft HEAD~1 |
| 想彻底丢弃本地修改 | git restore .、git clean -fd |
| 想找回误删分支或提交 | git reflog |
| 想定位哪个提交引入缺陷 | git bisect |
| 想看某个文件谁改过 | git log -- <file>、git blame <file> |
