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.