How to Check If a Git Repository Has Unpushed Commits

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if your local Git repository has commits that have not yet been pushed to a remote repository. We will explore different methods to identify these "unpushed" commits, starting with the fundamental git status command to see if your local branch is ahead of its remote counterpart.

You will then learn how to use git log @{u}..HEAD to specifically list the commits that are present locally but not on the upstream branch. Finally, we will briefly touch upon how to verify the status across all branches to ensure a comprehensive understanding of your repository's state. By the end of this lab, you will be equipped with the essential commands to effectively manage and track your local Git changes before pushing them remotely.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/status("Check Status") git/BasicOperationsGroup -.-> git/commit("Create Commit") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560092{{"How to Check If a Git Repository Has Unpushed Commits"}} git/status -.-> lab-560092{{"How to Check If a Git Repository Has Unpushed Commits"}} git/commit -.-> lab-560092{{"How to Check If a Git Repository Has Unpushed Commits"}} git/log -.-> lab-560092{{"How to Check If a Git Repository Has Unpushed Commits"}} end

Check git status for Ahead Commits

In this step, we will learn how to check if your local branch is "ahead" of the remote branch using git status. This is a common scenario when you've made commits locally but haven't pushed them to a remote repository yet.

First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine directory:

cd ~/project/my-time-machine

Now, let's create a new file and add some content to it. We'll simulate making a change that we will eventually commit.

echo "This is a new line for the future." >> message.txt

This command appends the text "This is a new line for the future." to the message.txt file we created earlier.

Next, let's stage this change using git add:

git add message.txt

Now, let's create a new commit with a message describing the change:

git commit -m "Add another message to the future"

You should see output similar to this, indicating that a new commit has been created:

[master a1b2c3d] Add another message to the future
 1 file changed, 1 insertion(+)

Now that we have a new commit locally, let's check the status of our repository again using git status:

git status

This time, the output will be different. You should see something like this:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

The line "Your branch is ahead of 'origin/master' by 1 commit." tells us that our local master branch has one commit that is not present in the origin/master branch (which represents the remote version of our branch). This is a very common situation when you are working on a project and making changes locally before sharing them with others.

Understanding git status is crucial because it gives you a clear picture of the current state of your repository. It tells you which files have been modified, which changes are staged, and whether your local branch is synchronized with the remote branch. This helps you keep track of your work and prepare for pushing your changes to a remote repository.

Use git log @{u}..HEAD

In the previous step, we saw that git status tells us our local branch is ahead of the remote branch. But how can we see which commits are ahead? This is where the git log @{u}..HEAD command comes in handy.

The @\{u\} (or @{upstream}) syntax refers to the upstream branch that your current branch is tracking. In our case, since we haven't explicitly set an upstream branch, Git defaults to origin/master (assuming origin is the name of the remote and master is the branch name). The HEAD refers to the tip of your current local branch.

So, @{u}..HEAD means "show me the commits that are on my current branch (HEAD) but not on the upstream branch (@{u})".

Let's try it out in our my-time-machine directory:

cd ~/project/my-time-machine
git log @{u}..HEAD

You should see output similar to this:

commit a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9 (HEAD -> master)
Author: Jane Doe <[email protected]>
Date:   Mon Aug 7 10:00:00 2023 +0000

    Add another message to the future

This output shows the commit we just created in the previous step. This confirms that this specific commit is the one that is "ahead" of the remote branch.

Using git log @{u}..HEAD is a powerful way to see exactly what changes you are about to push to a remote repository. It helps you review your work and ensure that you are only pushing the intended commits.

Remember, you can press q to exit the log view.

Verify with All Branches

In the previous steps, we used git status to see that our local branch is ahead and git log @{u}..HEAD to see the specific commits that are ahead. Now, let's use git log --all --decorate --oneline --graph to visualize the commit history of all branches, which can help us understand the relationship between our local and remote branches.

Make sure you are still in the ~/project/my-time-machine directory:

cd ~/project/my-time-machine

Now, run the following command:

git log --all --decorate --oneline --graph

Let's break down the options:

  • --all: Show the history of all branches.
  • --decorate: Show branch and tag names next to the commits they point to.
  • --oneline: Display each commit on a single line.
  • --graph: Draw a text-based graphical representation of the commit history on the left side of the output.

You should see output similar to this:

* a1b2c3d (HEAD -> master) Add another message to the future
* e4f5g6h (origin/master) Send a message to the future

In this output:

  • The top line shows our latest commit (a1b2c3d) with the message "Add another message to the future". (HEAD -> master) indicates that our current HEAD (where we are currently working) and the local master branch are pointing to this commit.
  • The line below shows the previous commit (e4f5g6h) with the message "Send a message to the future". (origin/master) indicates that the origin/master branch (the remote tracking branch) is pointing to this commit.

The graph on the left (the * and the lines) visually shows that our local master branch has moved one commit ahead of the origin/master branch. This confirms what git status told us earlier.

This command is incredibly useful for visualizing the history of your repository, especially when you have multiple branches. It helps you understand how branches diverge and merge, and where your local branches stand in relation to remote branches.

Press q to exit the log view.

Summary

In this lab, we learned how to check if a Git repository has unpushed commits. We started by using git status to identify if our local branch is "ahead" of the remote branch. This involved navigating to the project directory, creating and staging a new file, and committing the changes. After committing, running git status again clearly showed that our local branch was ahead of the remote by one commit, indicating the presence of unpushed changes.

The lab demonstrated the practical application of git status in identifying unpushed commits, highlighting its importance in understanding the synchronization state between local and remote repositories.