如何列出所有未推送的本地提交
列出未推送提交:git log origin/main..HEAD(当前分支相对上游);全仓库范围用 git log --branches --not --remotes --oneline。git status 的 ahead 提示和 git cherry -v 也能快速确认。
当前分支的未推送提交:git log @{u}..HEAD --oneline(或写全 origin/main..HEAD)。 想知道所有本地分支上还没推送的提交(包括那些忘了推的功能分支):git log --branches --not --remotes --oneline。
常用命令
git log @{u}..HEAD --oneline # 当前分支未推送(@{u}=上游分支)
git log origin/main..HEAD --oneline # 显式写法
git log --branches --not --remotes --oneline # 全仓库未推送的提交
git cherry -v # 简表:+ 未推送 - 已在远程
只看数量
git rev-list --count @{u}..HEAD # 输出数字,如 3
git status # "ahead of origin/main by 3 commits"
推送前的最后检查
git log @{u}..HEAD --stat # 每个提交动了哪些文件
git diff @{u}...HEAD # 未推送内容的总差异
养成 git push 前过一眼的习惯,避免把调试提交、临时注释推上去。
常见问题(FAQ)
Q:@{u} 报 "no upstream configured"?
当前分支没设上游。git push -u origin 分支名 建立跟踪,或直接用 origin/分支名..HEAD 显式对比。
Q:多远程(origin + upstream)怎么算"未推送"?
--not --remotes 排除所有远程的提交。只相对某一个远程:git log upstream/main..HEAD。
Q:为什么 cherry 显示的结果和 log 不一样?
git cherry 按补丁内容等价性判断(cherry-pick 过去的不同哈希提交也算已在远程),log 按提交图可达性判断。查"实质没推送的改动",cherry 更准。