Introduction
Git tags label specific commits and release versions in a Git project development. The git push
command allows users to export their local commits and tags to a remote repository.
In this tutorial, you will learn to create and push Git tags to a remote repository.
Prerequisites
- Git installed (see how to install Git on Windows, macOS, Ubuntu, CentOS 7, or CentOS 8).
- A local and remote Git repository.
How to Create Tag & Push Tag to Remote
Any changes made in a local repository, including creating tags or deleting them, remain local until pushed to a remote repository. Pushing changes to a remote repository is especially beneficial when collaborating on a project, as everyone can stay in sync with your work.
Warning: In some cases, pushing can overwrite existing remote data. Therefore, take extra caution when pushing changes to remote repositories.
The sections below explain how to create a tag and push it to a remote repository.
Create Tag
There are two types of tags:
- Lightweight. Used internally. Lightweight tags only point to specific commits and contain no extra information other than the tag name.
Create a lightweight tag using the following syntax:
git tag [tag_name]
For example:
- Annotated. Stored as full objects in the Git database. Annotated tags contain metadata, and they are used to describe a release without making a release commit.
Create an annotated tag using the following syntax:
git tag -a [tag_name] -m [message]
For example:
Note: Read our step-by-step tutorial on creating annotated and lightweight tags in Git.
The best practice for naming tags is to follow semantic versioning rules and specify meaningful version numbers that denote backward compatibility. It is also possible to rename a Git tag and address any inconsistencies in the names.
Git Push Tag
Each tag created in the local repository remains local until pushed to a remote repo. Export the tags to notify your collaborators of new program versions, patches, and other changes you made to the project.
Use the following syntax to push an individual Git tag to a remote repository:
git push [remote_name] [tag_name]
For example:
git push origin v2.1.1
The command pushes the v2.1.1
tag to the specified origin
remote repository. If the tag already exists in the remote repository, the command outputs that everything is up to date:
Note: If you are using the same name for a branch and a tag, specify the full refspec to ensure that the tag is pushed to remote and not the branch. The syntax is:
git push origin refs/tags/[tag_name]
To push multiple Git tags, list the tag names after the command. For example:
The command pushes both specified tags to the origin
remote repository.
Push All Git Tags to Remote
After working on a project locally, you may end up with many tags. Instead of pushing tags to a remote repo individually, push all tags at once using the following syntax:
git push [remote_name] --tags
Important: Delete old or incorrect tags in the local repository before pushing them to remote. Review existing tags by running:
git tag -l
For example:
git push origin --tags
The command pushes all local tags to the specified origin
repository.
Conclusion
This tutorial showed how to create and push Git tags to a remote repository, one by one or all at once.
Continue learning about Git with our tutorial for creating a new branch in Git, reverting the last Git commit, or creating a Git tag for a specific commit. Additionally, learn how to use Git fetch, the opposite of the git push
command.