How to List Commits Not Pushed to the Remote Repository in Git

John Wachira Feb 02, 2024
How to List Commits Not Pushed to the Remote Repository in Git

This article illustrates how we can list all the commits we have not yet pushed to the remote repository.

Git is a lifesaver. It alerts you when your branch is ahead of the remote.

However, the console only shows you how many commits you need to push. It does not list the commits, so how can we view these commits?

List Commits Not Pushed to the Remote Repository in Git

In this section, we will use an example to illustrate how you can list the commits you have not yet published.

In the example below, we will make some changes in our repository and commit them such that our branch is ahead of the remote branch by two commits.

Branch Ahead of the Remote by Two Commits

We can see that our branch is ahead of the remote by two commits. We are interested in the commits themselves.

Technically, we want to get all the commits between origin/main and HEAD. We will run the git log command, as illustrated below:

$ git log origin/main..HEAD

Get All Commits Between origin/main and HEAD

We can output a diff with the commands below:

$ git difftool origin/main..HEAD

You can use git diff origin/main, but you will use Git’s default diff interface. We use git difftool to open Meld, our default difftool, and mergetool.

Alternatively, you can run the git log command, as shown below.

$ git log --branches --not --remotes

This will list all the commits in all the branches that have not been pushed to the remote.

Another handy command is:

$ git log @{u}..

In a nutshell, you can view the unpublished commits in your local repository. The git log command, combined with several flags discussed, can give you a full list of the commits you need to push.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Log