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.