In Git, a tag is a way to mark a specific point in the history of a repository. It is typically used to label important milestones or releases, such as a version, release or a major project update. They usually serve three key purposes:
- Release versions: Tags are commonly used to mark specific versions of software releases. An example is a `v1.0` tag which can be used to represent the first official release of your software.
- Stable points: Tags can mark stable points in the development process. This is useful when you want to highlight commits that are known to be stable and reliable, such as after completing a major feature to be released, or fixing critical bugs.
- Documentation: Tags can serve as documentation markers. By tagging significant points in the project’s history you create references that can be used to understand and analyse the evolution of the codebase.
When it comes to applying tags locally, Git provides two types of tags: lightweight tags and annotated tags.
Lightweight tags: These are simple pointers to specific commits. They are created using the `git tag` command followed by a tag name. For example:
git tag v1.0
This creates a lightweight tag name “v1.0” at the current commit. Lightweight tags are easy to create and don’t store any additional information such as the name of the user creating the tag or the current date.
Annotated tags: Annotated tags store extra information such as the tagger’s name, email, date, and a message. They are created using the `-a` or ` — annotate` option with the `git tag` command. For example:
$ git tag -a v1.0 -m "Release version 1.0"
This command created an annotated tag name “v1.0” with the provided message. Annotated tags are often preferred for documenting releases or significant milestones because they provide more context and information.
Both lightweight and annotated tags are created locally and can be used for your personal reference or to share with other developers working on the project.
Tags are not automatically pushed when you push commits to the remote repository. Instead, you can push individual or groups of tags by themselves by specifying their name after the
git push command:
git push origin V1.0 V1.1 V1.2
Pushing all tags that are part of your local repository using the
git push origin --tags
Or attach flags to the relevant commits in the current branch using the
git push --follow-tags
When you push a tag to a remote repository it becomes visible to others who clone or fetch from that repository. However, by default tags are not fetched automatically when someone performs a `git clone` or `git fetch` operation. To retrieve tags from the remote repository, users need to explicitly specify the tags they want to fetch or use the `git fetch — tags` command to fetch all tags.
If you have pushed a tag to a remote repository and later decide to remove it, you can delete the tag both locally and remotely. To delete a local tag, you can use the command:
git tag -d <tag-name>
While to delete a remote tag you can use the command:
git push --delete <remote-name> <tag-name
Tags in git are valuable for marking significant points in your project’s history, documenting releases, and providing a clear reference for important milestones. Understanding how to create, manage and delete tags can enhance your Git workflow and collaboration with other developers.