适用环境:Windows 10/11、PowerShell 5/7、Git for Windows、GitHub / Gitee / GitLab / Azure DevOps 等 Git 平台。
目标:把 PowerShell 里使用 Git 做版本管理的常见场景全部整理成可复制、可自查、可排错的命令清单。
快速索引
按场景查命令
| 我想做什么 |
常用命令 |
| 检查 Git 是否安装 |
git --version、where.exe git |
| 设置用户名和邮箱 |
git config --global user.name、git config --global user.email |
| 查看 Git 配置 |
git config --list --show-origin |
| 初始化仓库 |
git init |
| 克隆远程仓库 |
git clone <url> |
| 查看工作区状态 |
git status、git status -sb |
| 查看文件改动 |
git diff、git diff --staged |
| 添加暂存 |
git add .、git add <file>、git add -p |
| 提交版本 |
git commit -m "message" |
| 查看提交历史 |
git log --oneline --graph --decorate --all |
| 创建和切换分支 |
git switch -c <branch>、git switch <branch> |
| 合并分支 |
git merge <branch> |
| 变基整理提交 |
git rebase <branch>、git rebase -i HEAD~3 |
| 拉取远程更新 |
git pull、git fetch |
| 推送到远程 |
git push、git push -u origin <branch> |
| 解决冲突 |
git status、编辑冲突文件、git add、git commit |
| 临时保存工作区 |
git stash push -m "message"、git stash pop |
| 撤销未提交修改 |
git restore <file>、git restore . |
| 取消暂存 |
git restore --staged <file> |
| 回退提交 |
git revert <hash>、git reset --soft/--mixed/--hard |
| 找回误删提交 |
git reflog、git reset --hard <hash> |
| 删除本地分支 |
git branch -d <branch>、git branch -D <branch> |
| 删除远程分支 |
git push origin --delete <branch> |
| 管理标签版本 |
git tag、git tag -a v1.0.0 -m "release" |
| 忽略文件 |
.gitignore、git rm --cached <file> |
| 大文件管理 |
git lfs track、git lfs install |
| 子模块管理 |
git submodule add、git submodule update --init --recursive |
| 清理未跟踪文件 |
git clean -fd、git clean -fdx |
| 排查谁改了某行 |
git blame <file> |
| 二分定位问题提交 |
git bisect start |
分类目录
0. PowerShell 使用约定
0.1 命令提示符
文中命令默认在 PowerShell 执行,不需要输入前面的说明文字。
1
| PS C:\Project> git status
|
0.2 占位符说明
1 2 3 4 5
| <repo-url> <branch> <file> <hash> <tag>
|
0.3 PowerShell 路径写法
1 2 3 4 5
| cd C:\Projects cd .\MyProject cd .. pwd ls
|
0.4 带空格路径必须加引号
1 2
| cd "C:\Users\YourName\Desktop\My Project" git add "Assets/My Script.cs"
|
0.5 PowerShell 常用辅助命令
1 2 3 4 5
| Get-Location Set-Location C:\Projects Get-ChildItem Get-Command git where.exe git
|
0.6 不建议在 PowerShell 里直接照抄 Linux 写法
1 2
| rm -rf .\folder Remove-Item .\folder -Recurse -Force
|
1. 安装与环境检查
1.1 检查 Git 版本
1 2 3
| git --version where.exe git Get-Command git
|
1.2 使用 winget 安装 Git
1 2 3
| winget search Git.Git winget install --id Git.Git -e git --version
|
1.3 升级 Git
1 2
| winget upgrade --id Git.Git -e git update-git-for-windows
|
1.4 检查当前目录是否是 Git 仓库
1 2
| git rev-parse --is-inside-work-tree git rev-parse --show-toplevel
|
1.5 进入仓库根目录
1
| cd (git rev-parse --show-toplevel)
|
2. Git 基础配置
2.1 配置用户名和邮箱
1 2 3 4
| git config --global user.name "YourName" git config --global user.email "you@example.com" git config user.name git config user.email
|
2.2 只给当前仓库设置用户名和邮箱
1 2
| git config user.name "CompanyName" git config user.email "company@example.com"
|
2.3 查看所有配置来源
1 2 3 4
| git config --list git config --list --show-origin git config --global --edit git config --local --edit
|
2.4 设置默认分支名
1
| git config --global init.defaultBranch main
|
2.5 设置默认编辑器
1 2
| git config --global core.editor "notepad" git config --global core.editor "code --wait"
|
2.6 设置换行符策略
1 2 3
| git config --global core.autocrlf true git config --global core.safecrlf warn git config --global core.eol lf
|
建议项目根目录配 .gitattributes,比单纯依赖个人配置更稳定:
1 2 3 4 5 6
| * text=auto # 自动规范文本文件换行 *.cs text eol=lf # C# 文件提交时统一 LF *.shader text eol=lf # Shader 文件提交时统一 LF *.png binary # 图片按二进制处理 *.jpg binary # 图片按二进制处理 *.zip binary # 压缩包按二进制处理
|
2.7 设置彩色输出和命令别名
1 2 3 4 5 6
| git config --global color.ui auto git config --global alias.st "status -sb" git config --global alias.co "checkout" git config --global alias.sw "switch" git config --global alias.br "branch -vv" git config --global alias.lg "log --oneline --graph --decorate --all"
|
2.8 设置默认拉取策略
1 2 3
| git config --global pull.rebase false git config --global pull.rebase true git config --global pull.ff only
|
只想简单稳定,建议先用:
1
| git config --global pull.rebase false
|
2.9 设置推送策略
1 2
| git config --global push.default simple git config --global push.autoSetupRemote true
|
3. 新建仓库与克隆仓库
3.1 初始化一个新仓库
1 2 3 4
| mkdir MyProject cd MyProject git init git status
|
3.2 第一次提交
1 2 3
| New-Item README.md git add README.md git commit -m "初始化项目"
|
3.3 克隆远程仓库
1 2 3
| git clone https://github.com/user/repo.git git clone git@github.com:user/repo.git git clone <repo-url> MyRepo
|
3.4 克隆指定分支
1 2
| git clone -b dev <repo-url> git clone --single-branch -b dev <repo-url>
|
3.5 浅克隆
1 2
| git clone --depth 1 <repo-url> git fetch --unshallow
|
3.6 克隆后第一件事
1 2 3 4
| cd .\repo git status -sb git remote -v git branch -vv
|
4. 工作区、暂存区与提交
4.1 Git 三个常用区域
| 区域 |
含义 |
常用命令 |
| 工作区 |
你正在编辑的文件 |
git status、git diff |
| 暂存区 |
准备提交的改动 |
git add、git diff --staged |
| 本地仓库 |
已经生成的提交记录 |
git commit、git log |
4.2 查看当前状态
1 2
| git status git status -sb
|
常见状态含义:
1 2 3 4 5 6
| ?? file.txt # 未跟踪的新文件 M file.txt # 工作区有修改,但未暂存 M file.txt # 已暂存,等待提交 MM file.txt # 暂存后又继续修改 A file.txt # 新文件已暂存 D file.txt # 文件删除已暂存
|
4.3 添加文件到暂存区
1 2 3 4 5 6
| git add . git add -A git add <file> git add .\src\ git add *.cs git add -p
|
4.4 提交改动
1 2 3 4
| git commit -m "修复登录按钮点击失效" git commit git commit --amend git commit --amend --no-edit
|
4.5 修改上一次提交
1 2
| git add <file> git commit --amend --no-edit
|
如果上一次提交已经推送到公共分支,不建议随便 amend;必须改时需要强推,见后文。
4.6 空提交
1
| git commit --allow-empty -m "触发 CI 构建"
|
4.7 提交信息建议
1 2 3 4 5 6 7
| feat: 新增登录页面 fix: 修复角色移动卡顿 docs: 更新使用文档 style: 调整格式,不改变逻辑 refactor: 重构代码 test: 增加测试 chore: 调整构建脚本或配置
|
5. 查看历史、差异与文件状态
5.1 查看提交历史
1 2 3 4 5 6
| git log git log --oneline git log --oneline --graph --decorate --all git log -5 --oneline git log --author="YourName" git log --since="2026-01-01"
|
5.2 查看某个文件历史
1 2 3
| git log -- <file> git log --follow -- <file> git log -p -- <file>
|
5.3 查看工作区差异
1 2 3 4 5
| git diff git diff --staged git diff HEAD git diff -- <file> git diff --stat
|
5.4 比较两个提交
1 2 3 4
| git diff <hash1> <hash2> git diff <branch1> <branch2> git diff main..dev git diff --name-only main..dev
|
5.5 查看某次提交内容
1 2 3
| git show <hash> git show --stat <hash> git show --name-only <hash>
|
5.6 查看某个文件在某次提交的内容
1 2
| git show <hash>:path/to/file.cs git show HEAD~1:path/to/file.cs
|
5.7 查询某行是谁改的
1 2
| git blame <file> git blame -L 20,40 <file>
|
6. 分支管理
6.1 查看分支
1 2 3 4
| git branch git branch -a git branch -r git branch -vv
|
6.2 创建分支
1 2
| git switch -c feature/login git branch feature/login
|
6.3 切换分支
1 2 3
| git switch main git switch dev git checkout main
|
6.4 从指定分支创建新分支
1 2 3
| git switch main git pull git switch -c feature/login
|
6.5 重命名分支
1 2
| git branch -m old-name new-name git branch -m new-name
|
6.6 删除本地分支
1 2
| git branch -d feature/login git branch -D feature/login
|
6.7 删除远程分支
1
| git push origin --delete feature/login
|
6.8 清理本地失效远程分支记录
1 2
| git fetch --prune git remote prune origin
|
6.9 查看分支从哪里分出来
1 2
| git merge-base main feature/login git log --oneline --graph --decorate --all
|
7. 合并、变基与提交整理
7.1 merge 合并
1 2 3
| git switch main git pull git merge feature/login
|
适合场景:
- 团队公共分支
- 不想改写提交历史
- 希望保留真实分支合并记录
7.2 fast-forward 快进合并
1
| git merge --ff-only feature/login
|
7.3 no-ff 合并
1
| git merge --no-ff feature/login -m "合并登录功能"
|
适合功能分支合并,希望历史里保留“一个功能整体”。
7.4 rebase 变基
1 2 3
| git switch feature/login git fetch origin git rebase origin/main
|
适合场景:
- 自己的功能分支
- 还没推送给别人共同使用
- 想让提交历史更直
7.5 rebase 过程中继续、跳过、终止
1 2 3 4 5
| git status git add <file> git rebase --continue git rebase --skip git rebase --abort
|
7.6 交互式 rebase 整理最近几次提交
常见操作:
1 2 3 4 5
| pick # 保留提交 reword # 修改提交信息 squash # 合并到上一个提交,并编辑提交信息 fixup # 合并到上一个提交,丢弃当前提交信息 drop # 删除提交
|
7.7 合并和变基怎么选
| 场景 |
推荐 |
| 公共主分支合并功能 |
merge --no-ff 或平台 PR 合并 |
| 自己分支同步 main |
rebase origin/main |
| 新手日常 pull |
git pull 默认 merge |
| 历史必须保持线性 |
rebase 或 pull --rebase |
| 不确定是否有人基于你的分支开发 |
不要 rebase 已推送历史 |
8. 远程仓库、拉取与推送
8.1 查看远程仓库
1 2 3
| git remote git remote -v git remote show origin
|
8.2 添加远程仓库
1 2
| git remote add origin git@github.com:user/repo.git git remote add origin https://github.com/user/repo.git
|
8.3 修改远程地址
1 2
| git remote set-url origin git@github.com:user/repo.git git remote -v
|
8.4 拉取远程更新
1 2 3
| git fetch origin git pull git pull --rebase
|
8.5 第一次推送新分支
1 2
| git push -u origin feature/login git branch -vv
|
设置过 upstream 后,以后只需要:
8.6 推送所有分支和标签
1 2
| git push --all origin git push --tags
|
8.7 强制推送
1 2
| git push --force-with-lease git push --force
|
强推适合:
- 自己的功能分支 rebase 后
- amend 已推送提交后
- 清理个人分支提交历史
不适合:
main、master、dev 这种公共分支
- 不确定别人是否基于该分支继续开发
8.8 查看本地和远程差多少
1 2 3
| git status -sb git log --oneline origin/main..HEAD git log --oneline HEAD..origin/main
|
8.9 本地分支关联远程分支
1 2
| git branch --set-upstream-to=origin/main main git push -u origin <branch>
|
9. 冲突处理
9.1 冲突通常什么时候出现
git merge <branch> 时
git rebase <branch> 时
git pull 自动合并时
git stash pop 恢复改动时
- 多人修改同一个文件同一段内容时
9.2 查看冲突文件
9.3 冲突标记长这样
1 2 3 4 5
| <<<<<<< HEAD 当前分支的内容 ======= 要合入分支的内容 >>>>>>> feature/login
|
处理方式:
1 2 3
| 保留当前分支内容 # 删除另一边和冲突标记 保留对方分支内容 # 删除当前边和冲突标记 两边内容合并 # 手工整理成最终想要的结果
|
9.4 merge 冲突解决流程
1 2 3 4
| git status notepad .\path\to\file.cs git add .\path\to\file.cs git commit
|
9.5 rebase 冲突解决流程
1 2 3 4
| git status notepad .\path\to\file.cs git add .\path\to\file.cs git rebase --continue
|
9.6 放弃本次合并或变基
1 2
| git merge --abort git rebase --abort
|
9.7 冲突时直接使用某一边
1 2 3
| git checkout --ours <file> git checkout --theirs <file> git add <file>
|
注意:rebase 时 ours/theirs 的直觉可能反过来,执行前用 git status 和 git diff 确认。
9.8 使用 VS Code 处理冲突
1 2 3 4 5
| code . git status git add . git commit git rebase --continue
|
10. 撤销、回退与恢复
10.1 取消暂存,但保留文件修改
1 2
| git restore --staged <file> git restore --staged .
|
10.2 丢弃未暂存修改
1 2
| git restore <file> git restore .
|
10.3 恢复已删除文件
1 2
| git restore <file> git checkout HEAD -- <file>
|
10.4 从历史提交恢复某个文件
1 2
| git restore --source=<hash> -- <file> git checkout <hash> -- <file>
|
10.5 撤销某次提交,保留历史
1 2 3
| git revert <hash> git revert HEAD git revert --no-commit <hash>
|
适合公共分支,因为不会改写历史。
10.6 reset 三种模式
1 2 3
| git reset --soft HEAD~1 git reset --mixed HEAD~1 git reset --hard HEAD~1
|
10.7 回到某个提交
高危:--hard 会丢弃未保存改动。执行前建议:
1 2 3
| git status git stash push -m "reset 前备份" git log --oneline -5
|
10.8 找回误删提交
1 2
| git reflog git reset --hard <hash>
|
10.9 删除未跟踪文件
1 2 3
| git clean -n git clean -fd git clean -fdx
|
11. Stash 临时保存
11.1 临时保存当前改动
1 2 3 4
| git stash git stash push -m "临时保存登录页面修改" git stash -u git stash -a
|
11.2 查看 stash
1 2 3
| git stash list git stash show git stash show -p stash@{0}
|
PowerShell 里 stash@{0} 建议加引号:
1
| git stash show -p "stash@{0}"
|
11.3 恢复 stash
1 2 3
| git stash pop git stash apply git stash apply "stash@{1}"
|
11.4 删除 stash
1 2
| git stash drop "stash@{0}" git stash clear
|
11.5 从 stash 创建分支
1
| git stash branch fix/login "stash@{0}"
|
12. 标签与版本发布
12.1 查看标签
1 2 3
| git tag git tag -l "v1.*" git show v1.0.0
|
12.2 创建标签
1 2 3
| git tag v1.0.0 git tag -a v1.0.0 -m "发布 v1.0.0" git tag -a v1.0.0 <hash> -m "补打标签"
|
12.3 推送标签
1 2
| git push origin v1.0.0 git push --tags
|
12.4 删除标签
1 2
| git tag -d v1.0.0 git push origin --delete v1.0.0
|
12.5 常见版本号
1 2 3 4
| v1.0.0 # 正式版本 v1.1.0 # 新功能版本 v1.1.1 # 修复版本 v2.0.0-beta.1 # 测试版本
|
13. .gitignore 与文件跟踪
13.1 创建 .gitignore
1 2 3 4
| New-Item .gitignore notepad .gitignore git add .gitignore git commit -m "添加忽略规则"
|
13.2 Windows 常见忽略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| # Windows Thumbs.db Desktop.ini
# VS Code .vscode/
# Rider / JetBrains .idea/
# Build bin/ obj/ dist/ build/
# Logs *.log
|
13.3 Unity 常见忽略
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| [Ll]ibrary/ [Tt]emp/ [Oo]bj/ [Bb]uild/ [Bb]uilds/ [Ll]ogs/ [Uu]ser[Ss]ettings/ MemoryCaptures/
*.csproj *.sln *.user *.pidb *.booproj *.svd *.pdb *.mdb
|
13.4 已经被 Git 跟踪的文件,加入 .gitignore 不会自动取消跟踪
1 2 3 4
| git rm --cached <file> git rm -r --cached .\Library git add .gitignore git commit -m "停止跟踪无关文件"
|
13.5 检查某文件为什么被忽略
1
| git check-ignore -v <file>
|
13.6 强制添加被忽略文件
14. SSH、HTTPS 与凭证管理
14.1 查看远程地址使用的是 SSH 还是 HTTPS
14.2 HTTPS 地址
1 2
| git remote set-url origin https://github.com/user/repo.git git push
|
14.3 SSH 地址
1 2 3
| git remote set-url origin git@github.com:user/repo.git ssh -T git@github.com git push
|
SSH 的详细配置可以看:Git Push&Pull 使用 SSH(含 443 端口)配置。
14.4 生成 SSH 密钥
1 2
| ssh-keygen -t ed25519 -C "you@example.com" Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub"
|
把 .pub 公钥添加到 GitHub / Gitee / GitLab 的 SSH Keys 页面。
14.5 启动 ssh-agent
1 2 3 4 5
| Get-Service ssh-agent Set-Service -Name ssh-agent -StartupType Automatic Start-Service ssh-agent ssh-add "$env:USERPROFILE\.ssh\id_ed25519" ssh-add -l
|
14.6 端口 22 不通时走 GitHub 443
编辑 SSH 配置:
1
| notepad "$env:USERPROFILE\.ssh\config"
|
写入:
1 2 3 4 5
| Host github.com HostName ssh.github.com Port 443 User git IdentityFile ~/.ssh/id_ed25519
|
测试:
14.7 清理 Git 代理
1 2 3 4 5
| git config --global --unset http.proxy git config --global --unset https.proxy git config --unset http.proxy git config --unset https.proxy git config --list --show-origin | Select-String proxy
|
14.8 配置 Git 代理
1 2
| git config --global http.proxy http://127.0.0.1:7890 git config --global https.proxy http://127.0.0.1:7890
|
只给 GitHub 设置代理:
1
| git config --global http.https://github.com.proxy http://127.0.0.1:7890
|
14.9 清理 Windows 凭据
1 2
| cmdkey /list cmdkey /delete:git:https://github.com
|
也可以在 Windows 凭据管理器里删除:
1
| 控制面板 -> 凭据管理器 -> Windows 凭据 -> 删除 github.com / git 相关凭据
|
15. 多账号、多远程与换仓库地址
15.1 多远程仓库
1 2 3 4 5
| git remote add origin git@github.com:user/repo.git git remote add gitee git@gitee.com:user/repo.git git remote -v git push origin main git push gitee main
|
15.2 一个本地仓库推送到两个平台
1 2 3 4
| git remote set-url --add --push origin git@github.com:user/repo.git git remote set-url --add --push origin git@gitee.com:user/repo.git git remote -v git push origin main
|
15.3 修改仓库地址
1 2
| git remote set-url origin <new-repo-url> git remote -v
|
15.4 删除远程
15.5 多账号 SSH 配置示例
1 2 3 4 5 6 7 8 9
| Host github-personal HostName github.com User git IdentityFile ~/.ssh/id_ed25519_personal
Host github-work HostName github.com User git IdentityFile ~/.ssh/id_ed25519_work
|
远程地址对应改成:
1 2
| git remote set-url origin git@github-personal:user/repo.git git remote set-url origin git@github-work:company/repo.git
|
15.6 当前仓库使用单独用户名邮箱
1 2 3
| git config user.name "WorkName" git config user.email "work@example.com" git config --local --list
|
16. 团队协作常用流程
16.1 最常见功能分支流程
1 2 3 4 5 6 7 8 9 10
| git switch main git pull git switch -c feature/login
git status -sb git add . git commit -m "新增登录功能" git push -u origin feature/login
|
然后在 GitHub / GitLab / Gitee 上创建 Pull Request / Merge Request。
16.2 功能分支同步主分支更新
1 2 3 4
| git switch feature/login git fetch origin git rebase origin/main git push --force-with-lease
|
如果不想 rebase:
1 2 3
| git switch feature/login git pull origin main git push
|
16.3 合并功能分支到 main
1 2 3 4
| git switch main git pull git merge --no-ff feature/login -m "合并登录功能" git push
|
16.4 修 bug 流程
1 2 3 4 5 6 7 8 9
| git switch main git pull git switch -c fix/login-null
git add . git commit -m "修复登录空引用" git push -u origin fix/login-null
|
16.5 紧急热修复流程
1 2 3 4 5 6 7 8 9 10 11
| git switch main git pull git switch -c hotfix/crash-on-start
git add . git commit -m "修复启动崩溃" git tag -a v1.0.1 -m "发布 v1.0.1" git push -u origin hotfix/crash-on-start git push origin v1.0.1
|
16.6 每天开始写代码前
1 2 3 4 5
| git status -sb git switch main git pull git switch feature/your-task git rebase main
|
16.7 每天下班前
1 2 3 4
| git status -sb git add . git commit -m "保存今日开发进度" git push
|
17. Git LFS 大文件管理
17.1 安装和启用 LFS
1 2
| git lfs version git lfs install
|
17.2 跟踪大文件类型
1 2 3 4 5 6
| git lfs track "*.psd" git lfs track "*.fbx" git lfs track "*.zip" git lfs track "*.mp4" git add .gitattributes git commit -m "配置 Git LFS"
|
17.3 查看 LFS 文件
1 2
| git lfs ls-files git lfs status
|
17.4 拉取 LFS 文件
1 2
| git lfs pull git lfs fetch --all
|
17.5 取消某类型 LFS 跟踪
1 2 3
| git lfs untrack "*.zip" git add .gitattributes git commit -m "取消 zip LFS 跟踪"
|
注意:取消跟踪规则不等于自动把历史 LFS 文件转回普通 Git 文件。
18. 子模块 Submodule
18.1 添加子模块
1 2
| git submodule add <repo-url> libs/MyLib git commit -m "添加 MyLib 子模块"
|
18.2 克隆带子模块的仓库
1
| git clone --recurse-submodules <repo-url>
|
如果已经普通克隆:
1
| git submodule update --init --recursive
|
18.3 更新子模块
1 2 3 4
| git submodule update --remote --recursive git status git add libs/MyLib git commit -m "更新 MyLib 子模块"
|
18.4 进入子模块单独操作
1 2 3 4 5 6
| cd .\libs\MyLib git status git switch main git pull cd ..\.. git add libs/MyLib
|
18.5 删除子模块
1 2 3 4
| git submodule deinit -f libs/MyLib git rm -f libs/MyLib Remove-Item -Recurse -Force .git\modules\libs\MyLib git commit -m "删除 MyLib 子模块"
|
19. Worktree 多工作区
19.1 什么时候用 worktree
- 想同时打开两个分支
- 不想来回 stash
- 一个分支在跑构建,另一个分支继续改 bug
- 同一个仓库多个任务并行处理
19.2 新建一个工作区
1 2
| git worktree add ..\MyProject-dev dev git worktree add -b fix/login ..\MyProject-fix main
|
19.3 查看工作区
19.4 删除工作区
1 2
| git worktree remove ..\MyProject-dev git worktree prune
|
20. Cherry-pick、Patch 与临时复制提交
20.1 cherry-pick 一个提交
1 2
| git switch dev git cherry-pick <hash>
|
20.2 cherry-pick 多个提交
1 2
| git cherry-pick <hash1> <hash2> git cherry-pick <old>^..<new>
|
20.3 cherry-pick 冲突处理
1 2 3 4 5
| git status notepad <file> git add <file> git cherry-pick --continue git cherry-pick --abort
|
20.4 生成 patch
1 2
| git format-patch -1 <hash> git format-patch main..feature/login
|
20.5 应用 patch
1 2
| git apply .\0001-some-change.patch git am .\0001-some-change.patch
|
20.6 导出未提交改动为 patch
1 2
| git diff > changes.patch git diff --staged > staged.patch
|
20.7 PowerShell 里应用补丁时注意编码
1
| git apply .\changes.patch
|
如果 patch 由 PowerShell 重定向生成后编码异常,建议用 Git Bash 或 VS Code 重新保存为 UTF-8。
21. Bisect 定位问题提交
21.1 开始二分
1 2 3
| git bisect start git bisect bad git bisect good <hash>
|
Git 会自动切到中间提交,让你测试。
21.2 标记好坏
1 2
| git bisect good git bisect bad
|
重复测试,直到 Git 找出第一个坏提交。
21.3 结束二分
21.4 用脚本自动二分
1 2 3 4
| git bisect start git bisect bad git bisect good <hash> git bisect run .\test.ps1
|
22. PowerShell 常用脚本与别名
22.1 查看 PowerShell 配置文件路径
1 2 3 4
| $PROFILE Test-Path $PROFILE New-Item -ItemType File -Path $PROFILE -Force notepad $PROFILE
|
22.2 常用 Git 函数
把下面内容写入 $PROFILE:
1 2 3 4 5 6 7 8 9
| function gs { git status -sb } function gl { git log --oneline --graph --decorate --all } function ga { git add @args } function gcmsg { git commit -m $args[0] } function gp { git push } function gpl { git pull } function gb { git branch -vv } function gsw { git switch @args } function gnew { git switch -c $args[0] }
|
重新加载:
22.3 一键提交并推送脚本
1 2 3 4 5 6 7 8
| param( [string]$Message = "更新内容" )
git status -sb git add -A git commit -m $Message git push
|
保存为 git-save.ps1 后执行:
1
| .\git-save.ps1 -Message "更新 Git 文档"
|
22.4 批量拉取多个仓库
1 2 3 4 5 6 7
| Get-ChildItem C:\Projects -Directory | ForEach-Object { if (Test-Path "$($_.FullName)\.git") { Set-Location $_.FullName Write-Host "Pull $($_.Name)" -ForegroundColor Cyan git pull } }
|
22.5 显示当前分支名
1
| git branch --show-current
|
23. Windows 常见问题排查
23.1 git 不是内部或外部命令
1 2
| git --version where.exe git
|
处理:
1 2
| 重新安装 Git for Windows,并勾选 Git from the command line 或把 Git 安装目录里的 cmd 路径加入 PATH
|
常见路径:
1
| C:\Program Files\Git\cmd
|
23.2 中文路径或文件名乱码
1 2
| git config --global core.quotepath false [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
PowerShell 7 对 UTF-8 支持更好,中文项目建议优先使用 PowerShell 7。
23.3 文件名大小写问题
1 2 3 4
| git config core.ignorecase git mv oldname.cs temp.cs git mv temp.cs NewName.cs git commit -m "修正文件名大小写"
|
23.4 换行符反复变化
1 2 3 4
| git config --global core.autocrlf true git add --renormalize . git status -sb git commit -m "规范化换行符"
|
23.5 Permission denied (publickey)
1 2 3 4
| ssh -T git@github.com ssh-add -l Get-Content "$env:USERPROFILE\.ssh\id_ed25519.pub" git remote -v
|
检查项:
- 公钥是否添加到平台账号
- 当前远程地址是否是
git@github.com:user/repo.git
- 私钥是否加载到
ssh-agent
- 多账号时是否命中了正确
Host
23.6 fatal: remote origin already exists
1 2
| git remote -v git remote set-url origin <repo-url>
|
1
| git pull origin main --allow-unrelated-histories
|
常见原因:本地 git init 后提交过,远程也有 README 初始化提交。
23.8 Your branch is ahead of origin/main
1 2 3
| git status -sb git log --oneline origin/main..HEAD git push
|
23.9 Your branch is behind origin/main
1 2
| git status -sb git pull
|
23.10 Your branch and origin/main have diverged
1 2
| git status -sb git pull
|
或者:
23.11 推送被拒绝
如果你的分支 rebase 过:
1
| git push --force-with-lease
|
23.12 LF will be replaced by CRLF
1
| git config --global core.autocrlf true
|
如果项目要求统一 LF,使用 .gitattributes 控制。
23.13 detached HEAD
1 2
| git status git switch main
|
如果在 detached HEAD 里改了代码并想保留:
1 2 3
| git switch -c save-detached-work git add . git commit -m "保存 detached HEAD 修改"
|
23.14 文件太大无法推送
1 2 3 4 5 6
| git lfs install git lfs track "*.zip" git add .gitattributes git add <big-file> git commit -m "使用 LFS 管理大文件" git push
|
如果大文件已经进入历史,需要用历史清理工具处理,不能只靠 .gitignore 解决。
23.15 index.lock 锁文件
1
| Remove-Item .git\index.lock
|
先确认没有正在运行的 Git 命令、编辑器提交窗口或 IDE Git 操作。
23.16 当前目录不是 Git 仓库
1 2 3 4
| git status pwd ls cd C:\Projects\YourRepo
|
23.17 误提交敏感信息
如果已经推送了密码、Token、私钥:
- 立刻去平台后台作废泄露的 Token / 密钥
- 从历史中清理敏感文件
- 强推清理后的历史
- 通知团队重新拉取或重克隆
24. 高频工作流速查
24.1 第一次把本地项目推到 GitHub
1 2 3 4 5 6 7
| cd C:\Projects\MyProject git init git add . git commit -m "初始化项目" git branch -M main git remote add origin git@github.com:user/repo.git git push -u origin main
|
24.2 每次正常提交
1 2 3 4 5 6
| git status -sb git diff git add . git diff --staged git commit -m "说明这次改了什么" git push
|
24.3 拉别人最新代码
1 2
| git status -sb git pull
|
如果本地有未提交改动:
1 2 3
| git stash push -m "pull 前临时保存" git pull git stash pop
|
24.4 新开功能分支
1 2 3
| git switch main git pull git switch -c feature/new-ui
|
24.5 分支写完推上去
1 2 3 4
| git status -sb git add . git commit -m "新增 UI 面板" git push -u origin feature/new-ui
|
24.6 合并 main 到自己的分支
1 2 3
| git switch feature/new-ui git fetch origin git merge origin/main
|
24.7 rebase main 到自己的分支
1 2 3 4
| git switch feature/new-ui git fetch origin git rebase origin/main git push --force-with-lease
|
24.8 撤销刚刚的 commit,但保留改动
24.9 撤销刚刚的 commit,改动回到工作区
24.10 完全丢弃刚刚的 commit 和改动
24.11 只想撤销某个文件
1 2
| git restore <file> git restore --staged <file>
|
24.12 从远程强制同步本地 main
1 2 3 4
| git fetch origin git switch main git reset --hard origin/main git clean -fd
|
高危:会丢弃本地 main 上未推送提交和未跟踪文件,执行前先 git status -sb。
24.13 修改最后一次提交信息
1 2
| git commit --amend -m "新的提交信息" git push --force-with-lease
|
24.14 找回误删分支
1 2
| git reflog git switch -c recovered-branch <hash>
|
24.15 查看最近改过哪些文件
1 2 3
| git diff --name-only git diff --staged --name-only git show --name-only --oneline HEAD
|
24.16 查看某个文件两版差异
1 2
| git diff HEAD~1 HEAD -- <file> git diff main feature/new-ui -- <file>
|
24.17 临时切分支但当前有改动
1 2 3
| git stash push -m "切分支前保存" git switch other-branch git stash pop
|
24.18 查看远程是否可推送
1 2 3
| git remote -v ssh -T git@github.com git ls-remote origin
|
24.19 提交前自查清单
1 2 3 4
| git status -sb git diff git diff --staged git log --oneline -5
|
提交前确认:
- 没有误提交密码、Token、私钥、账号配置
- 没有误提交
bin/、obj/、Library/、日志、缓存
- 提交信息能看懂本次改了什么
- 分支名和目标远程正确
- 团队公共分支不要随意
reset --hard 后强推
常用命令总表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| git status -sb git add . git commit -m "提交说明" git pull git push git log --oneline --graph --decorate --all git switch -c feature/name git switch main git merge feature/name git rebase origin/main git stash push -m "说明" git stash pop git restore <file> git restore --staged <file> git revert <hash> git reset --soft HEAD~1 git reset --hard HEAD~1 git reflog git clean -n git clean -fd
|
最后建议
- 新手优先掌握:
status、add、commit、pull、push、switch、merge、restore。
- 回滚公共分支优先用
git revert,少用 reset --hard。
- 强推优先用
git push --force-with-lease,不要直接 --force。
- 每次执行删除、清理、强制回退前,先用
git status -sb 和 git log --oneline -5 看一眼。
- 团队协作时,一个任务一个分支,提交信息写清楚,解决冲突后再推送。