1. 查看所有分支(本地 + 远程)
git branch -a
- 输出示例:
* main # 当前本地分支(带 * 号)feature/login # 其他本地分支remotes/origin/main # 远程分支(格式:remotes//)remotes/origin/dev
2. 查看本地分支 + 最后一次提交信息
git branch -v
- 输出示例:
main e1f2a3d [ahead 2] 修复登录页样式dev 4b5c6d9 新增用户权限模块
3. 查看远程分支(仅远程仓库的分支)
git branch -r
- 输出示例:
origin/HEAD -> origin/mainorigin/mainorigin/devorigin/feature/api
4. 查看本地分支与远程分支的关联关系
git branch -vv
- 输出示例:
* main e1f2a3d [origin/main: ahead 2] 修复登录页样式dev 4b5c6d9 [origin/dev] 新增用户权限模块
[origin/main: ahead 2]
表示本地分支 main
比远程分支 origin/main
多 2 个提交。
5. 查看远程仓库详细信息(含分支状态)
git remote show origin
- 输出示例:
* remote origin Fetch URL: git@github.com:user/repo.git Push URL: git@github.com:user/repo.git HEAD branch: main Remote branches: main tracked dev tracked feature stale (use \'git remote prune\' to remove) Local branches configured for \'git pull\': main merges with remote main dev merges with remote dev Local refs configured for \'git push\': main pushes to main (up to date) dev pushes to dev (local out of date)
6. 查看其他远程仓库的分支(非默认 origin
)
git remote show another-remote-name
高频场景解决方案
场景 1:确认当前分支是否关联远程分支
git rev-parse --abbrev-ref --symbolic-full-name @{u}
- 输出示例:
origin/main
(表示当前分支追踪 origin/main
)
- 若无输出,说明当前分支未关联远程分支。
场景 2:查看远程分支最新提交
git ls-remote --heads origin
场景 3:同步远程分支信息
git fetch --all --prune # 强制更新所有远程分支信息,并删除本地已失效的远程分支记录
总结命令速查表
命令 |
用途 |
关键场景 |
git branch -a |
查看所有分支 |
快速浏览本地 + 远程分支列表 |
git branch -avv |
查看分支详情 |
分析本地分支与远程的差异(提交领先/落后) |
git remote show origin |
查看远程仓库状态 |
协作时确认分支跟踪关系和同步状态 |
git fetch --prune |
清理失效远程分支 |
保持本地记录的远程分支与实际仓库一致 |