Git log - limit list by last push

I frequently use git log -n## to limit the number of commits displayed since I don’t need a full screenful. However, what I would like to do is to limit the list as follows

Start at HEAD
End at Last Push to repo

Something like git log LAST_PUSH..HEAD

Would ORIG_HEAD work? TIA

If I understand correctly, you want to see your local commits that aren’t on the matching remote branch yet? If yes, you could use one of the various ways git log lets you select the revision range for logs. For example, to see commits from the main branch in the remote origin to your current HEAD:

git log origin/main..HEAD

For all the ways to select ranges, see: Git - gitrevisions Documentation

Thank you, but not exactly. If my local branch is equal to the remote, I want to see everything from the last time I pushed to remote.

git push
local commit 1
local commit 2
local commit 3
git push

So I would like my log to show from HEAD back to HEAD~4 when I last pushed.

For that you’ll have to record the revision range of the push from the output of git push, I’m pretty sure that isn’t recorded anywhere in your local repository. If you control the server you’re pushing to, the reflog might answer your question, but as far as I’m aware that isn’t available for repositories on GitHub.

NBD @airtower-luna . I just wanted to make sure I was not missing anything. In the .git directory, there are several *_HEAD files. None applied to my ask. But git has its own magic. I found this in the logs. Is there a command to fetch from this list? This is exactly what I am looking for. The head directory shows amends as well as commits. As you can see I pushed amends (bad) but this was a brand new repo with no participants. Thank you.

$ cat .git/logs/refs/remotes/origin/master
... cropped for brevity
0000000000000000000000000000000000000000 68d1294809313ba402e8e090b4d9dad69d5f6637 Peter  <email> 1616498049 -0500    update by push
5b1b64d0ad9d92596463218e40e6f0a28e1069f3 faef5d5155f1f7f22ee86d11f6c33b1d2b8829a7 Peter  <email> 1627760615 -0500    update by push

And this show what was done locally

cat .git/logs/refs/heads/master
...cropped for brevity
0000000000000000000000000000000000000000 68d1294809313ba402e8e090b4d9dad69d5f6637 Peter  <email> 1616497957 -0500    commit (initial): Initial Commit
68d1294809313ba402e8e090b4d9dad69d5f6637 373c153c439b8c7884e5ae8fff2491427411a119 Peter  <email> 1616498214 -0500    commit (amend): Initial Commit
5b1b64d0ad9d92596463218e40e6f0a28e1069f3 faef5d5155f1f7f22ee86d11f6c33b1d2b8829a7 Peter Hyman <email> 1627760536 -0500    commit: lrzip-next v0.8.x support. Show filter method.
1 Like

Thank you for pointing that out @pete4abw, with that hint I was able to find that git reflog can actually show remote history, e.g.:

git reflog show origin/main

For my repositories I see both updates from my pushes as well as when my local Git saw new commits on fetch or pull.

1 Like

Learn something every day!

1 Like

From the git reflog manpage:

This command manages the information recorded in the reflogs.

The “show” subcommand (which is also the default, in the absence of any subcommands) shows the log of the reference provided in the command-line (or HEAD, by default). The reflog covers all recent actions, and in addition the HEAD reflog records branch switching. git reflog show is an alias for git log -g --abbrev-commit --pretty=oneline; see git-log(1) for more information.

So there is a git log command after all!

$ git log -g --abbrev-commit --pretty=oneline
faef5d5 (HEAD -> master, origin/master) HEAD@{0}: commit: lrzip-next v0.8.x support. Show filter method.
5b1b64d HEAD@{1}: commit (amend): Initial Commit
3e9f73d HEAD@{2}: commit (amend): Initial Commit
373c153 HEAD@{3}: commit (amend): Initial Commit
68d1294 HEAD@{4}: commit (initial): Initial Commit
1 Like

If you use that on a remote branch you only see the reflog updates (e.g. push) though, not the commits in between. But you could certainly use it to find your last push. :slightly_smiling_face: