How to Check If a Git Branch Has Specific Commits

GitGitBeginner
Practice Now

Introduction

In this lab, we will learn how to check if a Git branch contains specific commits. We will begin by exploring the git log command and its filtering options to search for commits based on message content or author. Next, we will utilize the git branch --contains command to efficiently determine which branches include a particular commit. Finally, we will verify our findings by directly checking for the presence of a commit using its hash.


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/commit("Create Commit") git/BranchManagementGroup -.-> git/branch("Handle Branches") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560039{{"How to Check If a Git Branch Has Specific Commits"}} git/commit -.-> lab-560039{{"How to Check If a Git Branch Has Specific Commits"}} git/branch -.-> lab-560039{{"How to Check If a Git Branch Has Specific Commits"}} git/log -.-> lab-560039{{"How to Check If a Git Branch Has Specific Commits"}} end

In this step, we will explore how to use the git log command to search for specific commits in your project's history. As your project grows and you make more commits, the git log output can become very long. Git provides powerful options to filter and search this history.

First, let's make sure we are in our project directory:

cd ~/project/my-time-machine

Now, let's add a few more commits so we have a history to search through. We'll add a new file and make a change to the existing one.

Create a new file:

echo "This is my second file." > second_file.txt

Add and commit the new file:

git add second_file.txt
git commit -m "Add a second file"

You should see output similar to this:

[master a1b2c3d] Add a second file
 1 file changed, 1 insertion(+)
 create mode 100644 second_file.txt

Now, let's modify the message.txt file:

echo "Adding another line." >> message.txt

Add and commit the change:

git add message.txt
git commit -m "Update message.txt"

You should see output similar to this:

[master e4f5g6h] Update message.txt
 1 file changed, 1 insertion(+)

Now that we have multiple commits, let's use git log to see the history:

git log

You will see all three commits listed. Press q to exit the log view.

To search for commits that contain a specific word in the commit message, we can use the --grep option. Let's search for commits with the word "message":

git log --grep "message"

This command will only show the commits whose messages contain the word "message". You should see the first and the third commit.

You can also search for commits by author using the --author option. Let's search for commits made by "Jane Doe" (the name we configured in the setup):

git log --author "Jane Doe"

This will show all commits made by the author "Jane Doe". In this case, it should show all three commits.

Using git log with options like --grep and --author is essential for navigating larger projects and finding specific changes or contributions. It's like having a powerful search engine for your project's history!

Use git branch --contains for Commit Check

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 useful when you have multiple branches and want to know where a particular change exists.

First, ensure you are in your project directory:

cd ~/project/my-time-machine

Let's create a new branch to demonstrate this command. We'll call it feature-branch:

git branch feature-branch

This command creates a new branch, but it doesn't switch to it. You can see your branches using git branch:

git branch

You should see something like this, with master highlighted (indicating it's the current branch):

* master
  feature-branch

Now, let's get the hash of our first commit. We can use git log --oneline to see a condensed log with commit hashes:

git log --oneline

You will see output similar to this (your hashes will be different):

e4f5g6h (HEAD -> master) Update message.txt
a1b2c3d Add a second file
abcdef0 Send a message to the future

Copy the hash of the first commit ("Send a message to the future"). In the example above, it's abcdef0.

Now, let's use git branch --contains with the hash of the first commit:

git branch --contains abcdef0

Replace abcdef0 with the actual hash of your first commit.

The output should show both master and feature-branch:

* master
  feature-branch

This tells us that the commit "Send a message to the future" exists on both the master and feature-branch branches. This makes sense because we created feature-branch while on the master branch, and the first commit was already part of master.

Now, let's get the hash of the last commit ("Update message.txt"). In the example above, it's e4f5g6h.

Use git branch --contains with the hash of the last commit:

git branch --contains e4f5g6h

Replace e4f5g6h with the actual hash of your last commit.

The output should only show master:

* master

This is because the last commit was made on the master branch after we created feature-branch. Therefore, feature-branch does not contain this commit.

The git branch --contains command is a powerful tool for understanding the relationship between commits and branches, helping you track where specific changes have been integrated.

Verify with Commit Hash

In this step, we will practice using commit hashes to refer to specific points in our project's history. Each commit in Git has a unique hash, which acts like its fingerprint. You can use this hash to view the details of a specific commit or even go back to that point in time.

First, make sure you are in your project directory:

cd ~/project/my-time-machine

Let's view the commit log again to get the hashes:

git log --oneline

You will see output similar to this (your hashes will be different):

e4f5g6h (HEAD -> master) Update message.txt
a1b2c3d Add a second file
abcdef0 Send a message to the future

Now, let's view the details of the first commit using its hash. Copy the hash of the first commit ("Send a message to the future"). In the example above, it's abcdef0.

Use the git show command followed by the commit hash:

git show abcdef0

Replace abcdef0 with the actual hash of your first commit.

This command will display detailed information about that specific commit, including the author, date, commit message, and the changes introduced in that commit. You will see that the message.txt file was created in this commit.

Press q to exit the git show view.

Now, let's view the details of the second commit ("Add a second file"). Copy its hash from the git log --oneline output. In the example above, it's a1b2c3d.

Use git show with the second commit's hash:

git show a1b2c3d

Replace a1b2c3d with the actual hash of your second commit.

This will show the details of the second commit, where the second_file.txt was created.

Finally, let's view the details of the last commit ("Update message.txt"). Copy its hash. In the example above, it's e4f5g6h.

Use git show with the last commit's hash:

git show e4f5g6h

Replace e4f5g6h with the actual hash of your last commit.

This will show the details of the last commit, where a line was added to message.txt.

Using git show with commit hashes allows you to inspect the contents and changes of any specific commit in your project's history. This is incredibly useful for debugging, understanding how a feature was implemented, or simply reviewing past work.

Summary

In this lab, we learned how to check if a Git branch contains specific commits. We began by exploring the git log command and its filtering options, such as --grep for searching commit messages and --author for searching by author, to locate commits within the project history.

Next, we will learn how to use the git branch --contains command to directly check if a branch includes a particular commit, and finally, we will verify this by using the commit hash.