Git is a free, open-source, distributed version control system that keeps track of the changes made throughout a project's life cycle. Its features to create branches, merges, and tags empower a developing team to work concurrently and efficiently on projects.
Git tags point to specific commits in the repository's commit history. We usually tag commits on releasing a new version or marking a commit as important. Git tags are similar to git branches, but git branches progress and change with each commit, whereas tags are permanent and do not change.
Now that we know what git tags are, let us consider a real-world example to learn more about them.
Let's consider that there is a team of developers working on a web application. The team uses git to manage the source code. After months of rigorous development and testing, they have finally reached the phase to make the first release of the web application. To mark this event, they create a git tag with the name version 1.0.0
.
After another month, the team decides to release a major feature of the application and name its tag as version 2.0.0
. With the help of these tags, the team can easily navigate to the application's code when the releases where made. The diagram below shows a git tag associated with a commit:
In the coming sections, we will explore different git types and their implementations.
There are two types of tags in Git:
Annotated tags
Lightweight tags
These tags store extra information in the tag data, including the tagger's name, email, tag message, and date. We use annotated tags when making a final version release of the project. It allows the developer to write a message in the tag that can be useful in the future.
To create an annotated tag, we use -a
or --annotate
as shown below:
git tag --anotate <tag-name> -m <message>
<tag-name>
: We can give our tag a name in place of <tag-name>
.
<message>
: We can give our tag a message in place of <message>
.
These tags only point to a commit and do not store additional information, including the name, email, message, and creation date. They are used when we want to mark a commit but do not want additional information along the tag.
To create a lightweight tag, we simply use the following command:
git tag <tag-name>
We can give any name to our lightweight tag by replacing it with the <tag-name>
.
Git provides us commands to push an individual tag by its name or push all tags in a go. The commands are given below:
// Push a specific tag to the remote servergit push origin <tag-name>// Push all tagsgit push origin --tags//orgit push --tags
To list all the tags present in the repository, we can run the following command:
git tag
We can run the following command to view the details of a specific tag:
git tag show <tag-name>
We can replace <tag-name>
with the tag name we want to view the details of.
Now that we have learned the basics of git tags, we will explore the step-by-step procedure to create them in our repository.
To create tags, we need to install git first. For that, we can run the following command in the terminal.
apt-get install -y git
Once we install git, we can confirm the installation by running the following command:
git --version
Running the command will display the version of git installed. If not, then git has not been installed correctly.
We can clone the repository in which we want to create the tags. Using git, we can accomplish this task. The command to clone a repository is:
git clone <link to repository>
Running the command clones the repository, and the project folder is created.
Now, we can navigate to the project folder using the following command:
cd <directory name>
Replace <directory-name>
with the cloned project's folder name.
Now, we will be creating a change in the project folder. The change can be created by updating any file or adding functionalities to the project.
Note: Just for replicating the scenario where we make changes in our project, we have created a blank hello.txt
file using the following command
touch hello.txt
We will stage the changes to git using the following command:
git add .
Once the changes have been staged, we can confirm it by checking the current state of the folder by using the following command:
git status
Configure git by providing our email or user name in the following commands.
git config --global user.email "you@example.com"git config --global user.name "Your name"
The email and user name provided should be the ones on which our GitHub account is created.
Now we can commit the changes made in the folder by using the following command. In place of <Your message>
, we can write any message.
git commit -m "<Your message>"
We can now create a tag on the commit using the following command. Any name can be given to the tag by replacing <tag name>
:
git tag <tag name>
Now that our tag has been created, we can view the created tags using the following command:
git tag
Let's do a live demonstration of creating tags on our own repositories. Below, we can see the commands that have been discussed above:
apt-get install -y git # Install gitgit --version # Check git versiongit clone <link to repository> # Clone your repositorycd <directory name>touch hello.txt # " OR Make any changes in the folder so that we can make a commit"git add .git statusgit config --global user.email "youremail@email.com"git commit -m "<Your message>"git tag <tag name> # Create a tag on the commitgit tag
In the terminal below, we can run the commands and create git tags.
Note: Replace
<link to repository>
with you repository link,<directory name>
with the project directory,<Your message>
with your commit message, and<tag name>
with your own tag name.
Some of the advantages of adding tags in the git repository are given below:
They help in marking important milestones throughout the project, for example, version releases.
They help developers, maintainers, and contributors effortlessly navigate through the repository to reach a point in the project's history and work on it.
Unlike branches, tags do not change with time, and they keep on referencing a specific commit.
Free Resources