Live streamJoin us for "State of code review 2024" on July 18thRegister today

How to add a tag to a git commit

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Adding tags to commits in Git is a crucial part of version control practices, especially when you want to mark specific points in your repository's history as important. Tags are commonly used for marking release points (v1.0, v2.0, etc.). This guide will walk you through the process of adding tags to your commits in Git, including how to add tags to specific commits, retrieve the hash for tags, and edit tags after they have been set.

What is a tag? A tag in Git is a pointer to a specific commit, used to capture a snapshot of the project at a particular point in time. Unlike branches, tags, after being created, do not change as new commits are made.

1. Adding a lightweight tag: A lightweight tag is like a bookmark to a specific commit. It’s a simple pointer to a commit and doesn’t include additional tag information.

Terminal
git tag <tagname>

If you execute this command without specifying a commit, Git automatically assigns the tag to the current commit.

2. Adding an annotated tag: Annotated tags are stored as full objects in the Git database. They include the tagger name, email, date, and have a tagging message, and can be GPG signed.

Terminal
git tag -a <tagname> -m "your message"

Like lightweight tags, if no commit is specified, it defaults to the current commit.

If you need to tag a commit that isn't the most recent, you first need to find the commit hash associated with the commit to which you want to add a tag.

Terminal
git log --pretty=oneline

This command will list all commits with their hashes. Once you have identified the correct commit hash, you can add a tag to it.

Terminal
git tag <tagname> <commit-hash>

Here, replace <commit-hash> with the actual hash of the commit you wish to tag.

To find the hash associated with a specific tag, you can use the following command:

Terminal
git rev-list -n 1 <tagname>

This command returns the commit hash pointed to by the tag.

Moving a tag to a different commit: To move a tag to a different commit, you might need to delete the old tag and then add it to another commit.

Terminal
git tag -d <tagname>
git tag <tagname> <new-commit-hash>

Deleting a tag: If you simply need to delete a tag, use:

Terminal
git tag -d <tagname>

Tags are not automatically pushed to your remote repository when you push your commits. You need to push them explicitly.

Terminal
git push origin <tagname>

To push all tags at once, use:

Terminal
git push origin --tags

For further reading on Git tags, see the official Git documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Get started

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2