Last modified: June 11, 2024
This article is written in: 🇺🇸
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
To create a new tag, use the git tag
command followed by the name of the tag and the commit hash or reference.
- Use the following command to create a new tag pointing to a specific commit:
git tag <tag-name> <commit-hash>
For example:
git tag v2.0 b4d373k8990g2b5de30a37bn843b2f51fks2b40
This will create a new tag called v2.0 that points to the commit with the hash b4d373k8990g2b5de30a37bn843b2f51fks2b40.
You can also specify the commit reference directly, such as the branch name or a commit message:
git tag <tag-name> <branch-name>
git tag <tag-name> "<commit-message>"
For example:
git tag v2.0 master
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:
- Tags are associated with a single commit, whereas branches change from one commit to the next as new code is added.
- Tags are typically used to mark specific points in Git history, such as releases, whereas branches are used to organize code over time.
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.