Run git tag --contains Commit
In this step, we will learn how to use the git tag --contains
command to find which tags contain a specific commit. This is useful when you want to know which releases or versions of your project include a particular change.
First, let's make sure we are in our project directory. Open your terminal and navigate to the my-time-machine
directory:
cd ~/project/my-time-machine
Now, let's create a few commits and tags to work with. We'll add a new file and make a commit:
echo "Another message for the future" > message2.txt
git add message2.txt
git commit -m "Add a second message"
You should see output similar to this:
[master <commit-hash>] Add a second message
1 file changed, 1 insertion(+)
create mode 100644 message2.txt
Now, let's add a tag to this commit. We'll call it v1.0
:
git tag v1.0
This command doesn't produce any output, but it has created a tag pointing to the latest commit.
Let's make another commit without a tag:
echo "A third message" > message3.txt
git add message3.txt
git commit -m "Add a third message"
You should see output similar to this:
[master <commit-hash>] Add a third message
1 file changed, 1 insertion(+)
create mode 100644 message3.txt
Now we have two commits and one tag. Let's use git log --oneline
to see our commit history:
git log --oneline
You should see something like this (commit hashes will be different):
<commit-hash> (HEAD -> master) Add a third message
<commit-hash> (tag: v1.0) Add a second message
<commit-hash> Send a message to the future
Notice that the v1.0
tag is associated with the "Add a second message" commit.
Now, let's find which tags contain the commit "Add a second message". We need the commit hash for this. From the git log --oneline
output, copy the commit hash next to (tag: v1.0)
.
Replace <commit-hash>
with the actual hash you copied and run the following command:
git tag --contains <commit-hash>
You should see v1.0
in the output, because this tag points directly to that commit.
Now, let's try to find which tags contain the first commit ("Send a message to the future"). Copy the commit hash for that commit from git log --oneline
.
Replace <first-commit-hash>
with the actual hash and run:
git tag --contains <first-commit-hash>
You should still see v1.0
in the output. This is because v1.0
is on a commit that is a descendant of the first commit. The --contains
flag checks if the specified commit is an ancestor of the commit that the tag points to.
This command is very helpful when you need to determine which versions of your software include a specific bug fix or feature.