如何获取单次Push的本地提交数量及上一个Push ID?能否按Push分组?
Hey there! Let's break down how to solve your Git-related questions clearly:
Number of changed files in the last push
To count how many files were modified in your most recent push, run this command:
git diff --name-only HEAD@{1} HEAD | wc -l
Here's what's happening:
HEAD@{1}refers to the state of your branch right before the last push (Git's reflog tracks these state changes)git diff --name-onlylists all files that differ between the pre-push and current branch stateswc -lcounts the number of lines (each line corresponds to one changed file)
Number of local commits included in the last push
To find out how many local commits you bundled into one push, use:
git rev-list --count HEAD@{1}..HEAD
This command counts all commits that exist in your current branch state (HEAD) but weren't present before the last push (HEAD@{1}). As you noted, if this returns 3, the commit ID right before your last push would be HEAD^3 — though using HEAD@{1} directly to reference that pre-push commit is often more reliable, since it doesn't depend on counting manually.
Git doesn't natively track "push" as a first-class object, but you can leverage the reflog (a local log of all branch state changes) to group commits by push actions. Here's how:
Step 1: List all push-related reflog entries
Run this to see every push action recorded locally:
git reflog --grep="push"
You'll get output like this:
abc1234 HEAD@{3}: push origin main: fast-forward
def5678 HEAD@{7}: push origin feature/xyz: forced update
Each entry marks the end state of a push, along with the target branch name.
Step 2: Group commits for a specific push
For any push entry (e.g., HEAD@{3}), you can view all commits included in that push by comparing it to the end state of the previous push. For example, if the prior push was at HEAD@{7}, run:
git log --oneline HEAD@{7}..HEAD@{3}
This will list all commits that were pushed in the action corresponding to HEAD@{3}.
Important note
The reflog is local-only — it only exists in your current Git repository. If you switch to a different clone of the repo, you won't see these push history entries. Also, forced pushes will still show up in the reflog, but you'll want to double-check commit ranges if you've rewritten branch history.
内容的提问来源于stack exchange,提问作者ashal

