DEV Community

Zanets
Zanets

Posted on

How to find the commits from last pull

I came out with this question recently: how to find those commits from last git pull?

Of course you can check git log first, record the last commit and check again after git pull.

Obviously not an ideal approach for a lazy developer like me.

Here is an example.
Alt Text
As you can see, this local repository is on ed102ca, but remote is on 7b44e48.
So, how do I find those commits from 3dea674 to 7b44e48 after git pull ?

git reflog + git show

What is reflog ?

Well, it's a log which records the modified when HEAD is changed. Such as git rebase, git commit and git pull will change the HEAD.

After you did a git pull, your last reflog will like this.



7b44e48 HEAD@{0}: pull: Fast-forward
952bd4d HEAD@{1}: ...


Enter fullscreen mode Exit fullscreen mode

We can use git show to see the difference between HEAD@{1} and HEAD@{0}.



git show @@{1}..@@{0}


Enter fullscreen mode Exit fullscreen mode

The @@{n} is an alias of HEAD@{n}, so this command shows the diff between HEAD@{1} and HEAD@{0} which contains commits from 3dea674 to 7b44e48.

git show shows commit message and file diff in pager by default, use this command instead when you just want an overview list.



git -c pager.show=false show --format="%h %an %s" -q @@{1}..@@{0}


Enter fullscreen mode Exit fullscreen mode

This command shows commit hash, author and subject, without diff result and doesn't use pager.



$ git -c pager.show=false show --format="%h %an %s" -q @@{1}..@@{0}
7b44e48 Zanets [refactor][git] don't need bash
f322bee Zanets [feat][vim/fzf] enhance color
c30740f Zanets [feat][bash] ls colors
4985b83 Zanets [feat][bash] get support multiple url
952bd4d Zanets [feat][tmux] better output color
7239d0f Zanets [feat][vim] enhance Help
650ed49 Zanets [refactor][vim] use a/b/c/d
1e092fd Zanets [refactor][vim] move comment to backup
3dea674 Zanets [config][vim] use origin fzf.vim
$


Enter fullscreen mode Exit fullscreen mode

Which is exactly what I want.

more

Sometimes @@{1}..@@{0} shows nothing, it's usually because of rebase



7b44e48 HEAD@{0}: rebase finished: returning to refs/heads/master
7b44e48 HEAD@{1}: pull: checkout 7b44e48646057a89e8f90ff6953b2c696d06eeda
ed102ca HEAD@{2}: ...


Enter fullscreen mode Exit fullscreen mode

If this happen, use @@{2}..@@{0} instead.

Top comments (0)