How to Check If a Git Commit Is in the Current Branch

GitGitBeginner
Practice Now

Introduction

In this lab, you will learn how to determine if a specific Git commit exists within the current branch. We will begin by exploring the git log command and its various options for efficiently searching and filtering commit history, including using --grep to search commit messages and --author to filter by author.

Following that, you will learn how to use the git branch --contains command to identify which branches in your repository include a given commit. This hands-on exercise will equip you with the skills to effectively navigate and understand your project's commit history across different branches.


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/checkout("Switch Branches") git/BranchManagementGroup -.-> git/log("Show Commits") subgraph Lab Skills git/add -.-> lab-560060{{"How to Check If a Git Commit Is in the Current Branch"}} git/commit -.-> lab-560060{{"How to Check If a Git Commit Is in the Current Branch"}} git/branch -.-> lab-560060{{"How to Check If a Git Commit Is in the Current Branch"}} git/checkout -.-> lab-560060{{"How to Check If a Git Commit Is in the Current Branch"}} git/log -.-> lab-560060{{"How to Check If a Git Commit Is in the Current Branch"}} end

Use git log to Find Commit

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

First, let's make a few more commits so we have a history to work with. Make sure you are in the ~/project/my-time-machine directory.

cd ~/project/my-time-machine
echo "Adding a second message" >> message.txt
git add message.txt
git commit -m "Add a second message"
echo "Adding a third message" >> message.txt
git add message.txt
git commit -m "Add a third message"

Now, let's view the full log again:

git log

You will see three commits listed, with the most recent one at the top.

Sometimes, you might be looking for a commit that contains a specific word in its message. You can use the -grep option for this. Let's find the commit with "second message":

git log --grep "second message"

This command will only show the commit whose message contains the phrase "second message".

Another useful option is -author to find commits by a specific author. Since we configured our author name in the setup, let's try finding commits by "Jane Doe":

git log --author "Jane Doe"

This will show all commits made by "Jane Doe".

Finally, you can limit the number of commits shown using the -n option. For example, to see only the last two commits:

git log -n 2

Using these options with git log helps you navigate your project's history efficiently, especially in larger projects with many commits.

Run git branch --contains for Commit

In this step, we will explore how to use the git branch --contains command. This command is useful for finding out which branches contain a specific commit. This is particularly helpful when you have multiple branches and need to track where a certain change has been included.

First, let's get the commit hash of one of our previous commits. We can use git log --oneline to see a condensed view of the log and easily copy a commit hash. Make sure you are in the ~/project/my-time-machine directory.

cd ~/project/my-time-machine
git log --oneline

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

abcdefg (HEAD -> master) Add a third message
hijklmn Add a second message
opqrstu Send a message to the future

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

Now, let's use git branch --contains with this commit hash. Replace [commit_hash] with the actual hash you copied:

git branch --contains [commit_hash]

For example, using the hash from the example output:

git branch --contains opqrstu

The output should show * master. The asterisk * indicates the currently checked-out branch. This tells us that the commit opqrstu is present on the master branch.

Right now, we only have one branch (master), so the output is simple. In the next step, we will create another branch and see how the output of git branch --contains changes.

Understanding which branches contain a specific commit is crucial for managing your project's history and coordinating work across different development lines.

Test with Other Branches

In this step, we will create a new branch and make a commit on it to see how git branch --contains behaves when a commit is only on one branch. This will help solidify your understanding of how Git tracks commits across different lines of development.

First, let's create a new branch called feature-branch. Make sure you are in the ~/project/my-time-machine directory.

cd ~/project/my-time-machine
git branch feature-branch

Now, let's switch to our new branch:

git checkout feature-branch

You should see output indicating that you have switched to the new branch:

Switched to branch 'feature-branch'

Let's make a new commit on this feature-branch.

echo "Adding a feature" >> feature.txt
git add feature.txt
git commit -m "Add a new feature file"

Now, let's get the commit hash of this new commit using git log --oneline:

git log --oneline

The output will show your new commit at the top, followed by the previous commits. Copy the hash of the "Add a new feature file" commit.

Now, let's use git branch --contains with the hash of the "Add a new feature file" commit. Replace [feature_commit_hash] with the actual hash you copied:

git branch --contains [feature_commit_hash]

The output should show only * feature-branch. This is because this specific commit was made after we created and switched to feature-branch, so it only exists on this branch's history.

Now, let's try git branch --contains with the hash of the very first commit ("Send a message to the future") again. Replace [first_commit_hash] with the hash you used in the previous step:

git branch --contains [first_commit_hash]

This time, the output should show both feature-branch and * master. This is because the feature-branch was created from the master branch, so it contains all the commits that were on master at that time, including the first commit.

This demonstrates how git branch --contains can help you understand the relationship between branches and the commits they include.

Summary

In this lab, we learned how to check if a Git commit is present in the current branch. We began by exploring the git log command and its various options such as --grep, --author, and -n to efficiently search and filter commit history. This is crucial for navigating projects with numerous commits.

Following that, we learned how to use the git branch --contains command to identify which branches include a specific commit. This command is valuable for tracking the propagation of changes across different branches within a Git repository.