How to List Unpushed Git Commits (local but not on origin)?

Tracking changes efficiently and ensuring that your local commits are pushed to the remote repository is important. However, there are times when you may have local commits that haven’t yet been pushed to the origin. Identifying these commits can be important for synchronizing your work with your team. This article will guide you through the process of listing unpushed Git commits in detail.

What are Git Commits?

Before diving into the commands, it’s important to understand a few concepts:

  • Commit: A commit in Git represents a snapshot of your repository at a specific point in time. Each commit has a unique hash.
  • Remote Repository (Origin): This is the version of your project that is hosted on a remote server, such as GitHub or GitLab. The default remote repository is usually called origin.
  • Branch: A branch is a pointer to a specific commit. The default branch is often named main or master.

Note: Ensure you have Git installed on your system. You can check this by running:

git --version

Step-by-Step Guide to Listing Unpushed Commits

Step 1: Open Your Terminal

Open your terminal (Command Prompt, PowerShell, Git Bash, or any terminal emulator you prefer).

Step 2: Navigate to Your Local Repository

Use the cd command to navigate to the root directory of your local Git repository. For example:

cd path/to/your/repository

Step 3: Fetch Updates from the Remote Repository

Before listing the unpushed commits, it’s a good practice to fetch the latest changes from the remote repository. This ensures that you have the most recent information. Use the following command:

git fetch origin

Step 4: Compare Local and Remote Branches

Now, you need to compare your local branch with the corresponding branch on the remote repository. Suppose you are on the main branch. You can list unpushed commits using the following command:

git log origin/main..HEAD
  • origin/main: Refers to the latest commit on the main branch in the remote repository.
  • HEAD: Refers to the latest commit on the current branch in your local repository.
  • .. : Specifies the range of commits to compare.

This command will list all the commits that are present in your local main branch but not in the remote main branch.

How to list unpushed Git commits (local but not on origin)?

Step 5: Interpreting the Output

The output of the git log origin/main..HEAD command will display a list of commits with details such as commit hashes, author names, dates, and commit messages. Here’s an example output:

commit 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d3e2f1a0b
Author: Your Name <your.email@example.com>
Date: Mon May 10 12:34:56 2023 +0000

Commit message describing the changes

commit 8e7f6d5c4b3a2f1e0d9c8b7a6e5d4c3b2a1f0e1d
Author: Your Name <your.email@example.com>
Date: Sun May 9 11:22:33 2023 +0000

Another commit message describing other changes

Each block represents a commit that hasn’t been pushed to the remote repository.

Conclusion

Tracking unpushed commits is a fundamental task in Git that helps ensure your local work is synchronized with the remote repository. By following the steps in this article, you can efficiently list unpushed commits and maintain a smooth workflow.


Contact Us