How to Create Tag in Git: Simple Commands and Examples
To create a tag in Git, use the
git tag command followed by the tag name. For example, git tag v1.0 creates a lightweight tag named v1.0 at the current commit.Syntax
The basic syntax to create a tag in Git is:
git tag <tagname>: Creates a lightweight tag at the current commit.git tag -a <tagname> -m "message": Creates an annotated tag with a message.git tag -d <tagname>: Deletes a tag.
bash
git tag <tagname>
git tag -a <tagname> -m "message"Example
This example shows how to create a lightweight tag and an annotated tag in Git.
bash
git tag v1.0 git tag -a v1.1 -m "Release version 1.1"
Output
Created tag 'v1.0'
Created annotated tag 'v1.1'
Common Pitfalls
Common mistakes when creating tags include:
- Not pushing tags to the remote repository, so others can't see them.
- Confusing lightweight tags with annotated tags; annotated tags store extra information like author and date.
- Trying to create a tag with a name that already exists.
Remember to push tags with git push origin <tagname> or git push origin --tags to share them.
bash
git tag v1.0 # Trying to create the same tag again # Wrong way (will fail): git tag v1.0 # Right way (delete old tag first): git tag -d v1.0 git tag v1.0
Output
fatal: tag 'v1.0' already exists.
Deleted tag 'v1.0'
Created tag 'v1.0'
Quick Reference
| Command | Description |
|---|---|
| git tag | Create a lightweight tag at current commit |
| git tag -a | Create an annotated tag with a message |
| git tag -d | Delete a tag locally |
| git push origin | Push a specific tag to remote |
| git push origin --tags | Push all tags to remote |
Key Takeaways
Use
git tag <tagname> to create a simple tag at the current commit.Use
git tag -a <tagname> -m "message" to create an annotated tag with extra info.Tags are local until you push them with
git push origin <tagname> or git push origin --tags.Avoid duplicate tag names; delete old tags before recreating them.
Annotated tags are better for releases because they store author, date, and messages.