Git
Version Control
DevOps
Programming
Git Tips

How to list unpushed Git commits local but not on origin

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Git is a powerful version control system that allows developers to track changes in their projects, collaborate with others, and maintain a history of modifications. One common scenario when working with Git is having local commits that have not yet been pushed to a remote repository. This article provides a detailed explanation of how to list these unpushed Git commits using both command line techniques and various helpful Git commands. Understanding how to identify unpushed commits is crucial for maintaining an organized workflow and ensuring that your work is up-to-date with the remote repository.

Understanding Commits in Git

Before diving into the command-line techniques for identifying unpushed commits, it's essential to understand how Git tracks changes through commits. Each commit in Git represents a snapshot of the project's state at a given time. Commits are identified by a unique SHA hash, which can be used to reference any specific commit. When you make changes to your files and execute the git commit command, a new commit is created in your local repository.

Local and Remote Repositories

In Git, the common workflow involves working with both local and remote repositories. A local repository resides on your machine, while a remote repository is usually hosted on a platform like GitHub, GitLab, or Bitbucket. You can push your local commits to the remote repository to share your work with others and ensure your changes are available online.

Listing Unpushed Git Commits

To list commits that are present in your local branch but have not been pushed to the remote branch, you can use several techniques. The most common methods involve comparing your local branch with the corresponding remote branch.

Using Git Log with origin

The simplest way to see unpushed commits is by using the git log command alongside the .. range syntax. Suppose you are on the branch main, and you've made some local commits you haven't pushed yet. Here is how you can list them:

bash
git log origin/main..main

This command will show all the commits from main that are not in origin/main.

Using Git Log with Branch Comparison

Alternatively, you can compare the local and remote branches by omitting the second branch name since you are currently on the branch you want to compare:

bash
git log origin/main..

This will provide the same results as the previous example, as it defaults to comparing the state of your current branch.

Utilizing Git Rev-List

Another powerful method to list unpushed commits is using git rev-list. This command is highly flexible and can handle advanced scenarios:

bash
git rev-list --left-only --count origin/main...main

The --left-only option will give you a count of unpushed commits, while you can omit it to get the complete list of SHA hashes for those commits.

Git Cherry

A less known but handy command is git cherry. This command shows the commits in your current branch that are not in the remote branch, along with a + or - symbol indicating if the commit is unique to the local branch or a duplicate in the remote branch:

bash
git cherry -v

This will output a list of commit SHA hashes and their corresponding commit message.

Summary Table

Below is a summary table of the Git commands discussed for listing unpushed commits:

CommandDescriptionExample Output
git log origin/main..Lists unpushed commits with full details.Commit messages, authors, and SHA hashes.
git rev-list origin/main..Lists SHA hashes of unpushed commits.List of SHA hashes.
git cherry -vShows a concise list of unique local commits.List with + or - markers and messages.

Conclusion

Listing unpushed commits in Git is an essential skill for effective version control, allowing you to review and understand your local changes before pushing them to a remote repository. Whether through git log, git rev-list, or git cherry, each method offers unique insights and options for handling your Git workflow effectively. By mastering these commands, you can ensure that your work is tracked correctly and consistently, aligning your local progress with your team's shared codebase.


Course illustration
Course illustration

All Rights Reserved.