How to Check If a Git Commit Is in a Specific Branch

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a specific Git commit exists within a particular branch. We will explore the git branch --contains command to identify all branches that include a given commit, providing insight into the commit's history and integration points.

You will also learn how to use git log with a branch name to verify the presence of a commit within that branch's history and how to efficiently check multiple branches for a commit. This lab will equip you with essential skills for understanding and navigating your Git project's history.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("Git")) -.-> git/BranchManagementGroup(["Branch Management"]) git(("Git")) -.-> git/BasicOperationsGroup(["Basic Operations"]) git/BasicOperationsGroup -.-> git/add("Stage Files") git/BasicOperationsGroup -.-> git/commit("Create Commit") git/BranchManagementGroup -.-> git/branch("Handle Branches") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560059{{"How to Check If a Git Commit Is in a Specific Branch"}} git/commit -.-> lab-560059{{"How to Check If a Git Commit Is in a Specific Branch"}} git/branch -.-> lab-560059{{"How to Check If a Git Commit Is in a Specific Branch"}} git/log -.-> lab-560059{{"How to Check If a Git Commit Is in a Specific Branch"}} end

Run git branch --contains Commit

In this step, we will learn how to use the git branch --contains command to find out which branches contain a specific commit. This is a very useful command when you want to understand the history of your project and see where a particular change has been integrated.

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

cd ~/project/my-time-machine

Now, let's create a new file and make a commit. This will give us a new commit to work with.

echo "Another message for the future" > message2.txt
git add message2.txt
git commit -m "Add another message"

You should see output similar to this after the commit:

[master <commit-id>] Add another message
 1 file changed, 1 insertion(+)
 create mode 100644 message2.txt

Now we have a new commit. Let's find the commit ID of this new commit. We can use git log --oneline to see a condensed log:

git log --oneline

The output will look something like this:

<commit-id-2> (HEAD -> master) Add another message
<commit-id-1> Send a message to the future

The first commit ID listed is the one for "Add another message". Copy this commit ID.

Now, let's use git branch --contains with this commit ID. Replace <commit-id-2> with the actual commit ID you copied:

git branch --contains <commit-id-2>

The output should show the master branch, indicating that this commit is on the master branch:

* master

The git branch --contains <commit> command is powerful because it helps you trace the lineage of a commit. If you have multiple branches, this command will list all the branches that include that specific commit. This is essential for understanding how changes flow through your project's different development lines.

Use git log Branch to Verify

In the previous step, we used git branch --contains to see which branches contain a specific commit. Now, let's use the git log command with a branch name to verify the history of that branch. This will show us all the commits that are part of that branch's history.

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

cd ~/project/my-time-machine

We know from the previous step that our latest commit is on the master branch. Let's view the log specifically for the master branch:

git log master --oneline

You should see output similar to this, showing the commits on the master branch:

<commit-id-2> (HEAD -> master) Add another message
<commit-id-1> Send a message to the future

This output confirms that both of our commits are indeed on the master branch. Using git log <branch-name> is a great way to inspect the history of a specific branch and see all the commits that are reachable from that branch's tip.

Comparing the output of git branch --contains <commit> and git log <branch-name> helps you understand the relationship between commits and branches. git branch --contains tells you which branches include a commit anywhere in their history, while git log <branch-name> shows the linear history leading up to the tip of that specific branch.

Press q to exit the log view.

Check Multiple Branches

In this step, we will create a new branch and see how git branch --contains behaves when a commit is present on multiple branches. This will further illustrate the power of this command in understanding your project's branching structure.

First, make sure you are in the ~/project/my-time-machine directory:

cd ~/project/my-time-machine

Now, let's create a new branch called feature-branch:

git branch feature-branch

This command creates a new branch, but it doesn't switch you to it. You are still on the master branch.

Let's check our branches:

git branch

You should see both branches listed, with master highlighted (indicating it's the current branch):

* master
  feature-branch

Now, let's get the commit ID of our latest commit again using git log --oneline:

git log --oneline

The output will be similar to before:

<commit-id-2> (HEAD -> master, feature-branch) Add another message
<commit-id-1> Send a message to the future

Notice that the latest commit now shows both HEAD -> master and feature-branch. This means the feature-branch was created at this commit.

Now, let's use git branch --contains with this commit ID again. Replace <commit-id-2> with the actual commit ID:

git branch --contains <commit-id-2>

This time, the output should show both branches, because the commit exists in the history of both master and feature-branch:

* master
  feature-branch

This demonstrates how git branch --contains can show you all the branches that have a particular commit in their history. This is incredibly useful for tracking where changes have been merged or integrated across different development lines.

Summary

In this lab, we learned how to check if a Git commit is present in a specific branch. We started by using the git branch --contains <commit-id> command, which lists all branches that include the specified commit. This is a fundamental tool for understanding the history and integration of changes within a Git repository.

We then explored how to verify the presence of a commit in a branch using git log <branch-name>. This command displays the commit history of a specific branch, allowing us to visually confirm if the target commit is part of that branch's lineage. Finally, we saw how to extend these techniques to check for a commit across multiple branches, providing a comprehensive view of its distribution within the project's branching structure.