Last modified: July 10, 2024

This article is written in: 🇺🇸

Creating and Managing Git Tags

Tags are references to specific points in Git history. They can be used to mark important milestones, such as releases, and provide a way to refer to specific commits in a repository.

Creating Tags

Tags in Git are used to mark specific points in your repository’s history. Typically, tags are used to mark release points (e.g., v1.0, v2.0).

To create a new tag, use the git tag command followed by the name of the tag and the commit hash or reference.

I. Create a New Tag Pointing to a Specific Commit:

To tag a specific commit, provide the tag name and the commit hash. This creates a lightweight tag pointing to the specified commit.

git tag <tag-name> <commit-hash>

Example: If you want to create a tag named v2.0 for a commit with the hash b4d373k8990g2b5de30a37bn843b2f51fks2b40, you would run:

git tag v2.0 b4d373k8990g2b5de30a37bn843b2f51fks2b40

II. Create a Tag Using a Commit Reference:

You can also specify a commit reference directly, such as a branch name or a commit message. This is useful when you want to tag the latest commit on a branch or a commit identified by its message.

git tag <tag-name> <branch-name>
git tag <tag-name> "<commit-message>"

Examples:

  1. To create a tag named v2.0 that points to the latest commit on the master branch, use:

git tag v2.0 master

  1. To tag a commit with the message "Fix bug in login form" with v2.0, you would use:

git tag v2.0 "Fix bug in login form"

Annotated Tags

Annotated tags contain additional information such as a message and the tagger's name and email address.

To create an annotated tag, use the -a flag followed by the tag name and a message:

git tag -a <tag-name> -m "<tag-message>"

Viewing Tags

To see a list of all the tags in your repository, you can use the git tag command without any arguments:

$ git tag
v1.0
v1.1
v2.0

This will display a list of all tags in your repository.

To see the details of a specific tag, you can use the git show command followed by the tag name:

git show <tag-name>

For example:

git show v2.0

This will show the commit the tag points to, as well as any additional metadata such as the tag message and tagger information.

Pushing Tags to a Remote Repository

By default, tags are not pushed to a remote repository when you use the git push command. To push tags to a remote repository, you can use the git push command followed by the --tags flag:

git push --tags

This will push all the tags in your local repository to the remote repository. You can also push a specific tag to a remote repository by using the following command:

git push origin <tag-name>

Tags vs Branches

Tags and branches are both references to specific commits, but they behave differently:

When working on a feature, you use branches to organize code over time. Tagging, in general, refers to metadata linked with a build or deployment that is automatically or manually updated.

Table of Contents

  1. Creating Tags
  2. Annotated Tags
  3. Viewing Tags
  4. Pushing Tags to a Remote Repository
  5. Tags vs Branches