How to Check If a Git Branch Has Unpushed Commits

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to check if your local Git branch 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 git status command to see if your branch is ahead of the remote.

You will then learn how to use the git log command with specific arguments to list the commits that are present locally but not on the remote tracking branch. Finally, we will discuss how to verify this information by comparing your local branch with the remote repository. By the end of this lab, you will be equipped with the skills to easily determine if your local changes need to be pushed.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/CollaborationandSharingGroup(["Collaboration and Sharing"]) 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") git/CollaborationandSharingGroup -.-> git/push("Update Remote") subgraph Lab Skills git/add -.-> lab-560040{{"How to Check If a Git Branch Has Unpushed Commits"}} git/status -.-> lab-560040{{"How to Check If a Git Branch Has Unpushed Commits"}} git/commit -.-> lab-560040{{"How to Check If a Git Branch Has Unpushed Commits"}} git/log -.-> lab-560040{{"How to Check If a Git Branch Has Unpushed Commits"}} git/push -.-> lab-560040{{"How to Check If a Git Branch Has Unpushed Commits"}} end

Use git status to Check Ahead Commits

In this step, we'll learn how to use the git status command to see if our local branch has commits that are ahead of the remote branch. This is a common scenario when you've made changes locally and 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 call this file future_plans.txt:

echo "Plan for world domination." > future_plans.txt

Next, we need to stage this new file to prepare it for a commit:

git add future_plans.txt

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

git commit -m "Add future plans"

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

[master abcdefg] Add future plans
 1 file changed, 1 insertion(+)
 create mode 100644 future_plans.txt

We have now created a new commit on our local master branch. However, this commit only exists locally and has not been sent to any remote repository.

Let's use git status to see the current state of our repository:

git status

The output should now show that your local branch is ahead of the remote branch (if you had one configured, which we don't in this basic example, but Git still gives you the hint):

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

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   future_plans.txt

nothing to commit, working tree clean

The important line here is Your branch is ahead of 'origin/master' by 1 commit.. This tells us that our local master branch has one commit that is not present on the origin/master branch (which is the default name for the remote branch Git expects).

This is a very useful piece of information. It lets you know that you have local changes that haven't been shared with others yet. In a real-world scenario with a remote repository, this would indicate that you need to git push your changes.

Run git log --oneline @{u}..HEAD

In the previous step, we saw how git status can tell us if our local branch is ahead of the remote branch. Now, let's use the git log command with a special syntax to see exactly which commits are ahead.

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

The command we will use is git log --oneline @{u}..HEAD. Let's break down this command:

  • git log: This is the command to view the commit history.
  • --oneline: This option displays each commit on a single line, making the output concise and easy to read.
  • @{u}: This is a special syntax that refers to the upstream branch. 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).
  • ..HEAD: This is a range notation. HEAD refers to the tip of the current branch (our local master). The @{u}..HEAD range means "show me the commits that are reachable from HEAD but not reachable from @{u}". In simpler terms, it shows the commits that are on our local branch but not on the upstream remote branch.

Now, let's run the command:

git log --oneline @{u}..HEAD

You should see output similar to this:

abcdefg (HEAD -> master) Add future plans

This output shows the commit we created in the previous step (Add future plans). The unique identifier (abcdefg) will be different in your terminal, but the commit message should match.

This command is incredibly useful for seeing exactly which commits you are about to push to a remote repository. It gives you a clear list of the changes you've made locally that haven't been shared yet.

Comparing this output with the git status output from the previous step, you can see that git status tells you how many commits you are ahead, while git log @{u}..HEAD tells you which specific commits you are ahead by. Both commands provide valuable information about the state of your local repository relative to its remote counterpart.

Verify with Remote Comparison

In a real-world scenario, you would typically have a remote repository (like on GitHub, GitLab, or Bitbucket) that your local repository is connected to. In this lab environment, we don't have a live remote repository to push to. However, we can simulate the concept of comparing our local branch to a remote branch using the commands we've learned.

We've already used git status and git log --oneline @{u}..HEAD to see the commits that are on our local branch but not on the simulated upstream branch (origin/master).

Let's review the output of git status again:

git status

You should still see the line indicating that your branch is ahead:

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

... (rest of the output)

And the output of git log --oneline @{u}..HEAD:

git log --oneline @{u}..HEAD

This should show the single commit we made:

abcdefg (HEAD -> master) Add future plans

These two commands work together to give you a clear picture of the difference between your local branch and its configured upstream branch. git status provides a summary, while git log @{u}..HEAD provides the details of the specific commits.

In a real project, after seeing this output, you would typically run git push to send your local commits to the remote repository, making them available to others and updating the remote branch.

Since we don't have a remote to push to in this lab, we have successfully completed the goal of understanding how to identify commits that are ahead of the remote branch using git status and git log.

This skill is fundamental for collaborating with others and keeping your local and remote repositories synchronized. By regularly checking the status and log, you can stay aware of your pending changes and ensure you push them when ready.

Summary

In this lab, we learned how to check if a Git branch has unpushed commits. We started by using the git status command after making a local commit. The output of git status clearly indicates if the local branch is "ahead" of its remote counterpart and by how many commits, providing a direct way to see if there are unpushed changes.

We then explored using git log --oneline @{u}..HEAD to get a more detailed view of the specific commits that are on the local branch but not on the remote tracking branch. This command lists the commit hashes and messages of the unpushed commits, offering a precise way to identify what changes are pending. Finally, we discussed the concept of verifying this by comparing the local and remote branches, reinforcing the understanding of how to confirm the presence of unpushed commits.