Git - Tag Operation
Git - Working with Tags
Section titled “Git - Working with Tags”Tags in Git are used to mark specific points in your project’s history as being important, typically for releases (e.g., v1.0.0, v2.0-beta). Once a tag is created, it acts like an unmovable reference to a particular commit.
There are two main types of tags in Git:
- Lightweight tags: These are like branches that don’t change – just a pointer to a specific commit. They don’t store any extra information.
- Annotated tags: These are stored as full objects in the Git database. They are checksummed; contain the tagger name, email, and date; have a tagging message; and can be GPG signed. It’s generally recommended to use annotated tags for releases or important public markers.
Creating Tags
Section titled “Creating Tags”Creating an Annotated Tag
Section titled “Creating an Annotated Tag”To create an annotated tag, use the git tag -a <tagname> -m <message> command. The -a flag signifies an annotated tag, and -m provides the tagging message.
$ git log --oneline -1 # Get the latest commit hash (optional, HEAD is default)# a1b2c3d (HEAD -> main) Add super feature
# Tag the current commit (HEAD) with an annotated tag$ git tag -a v1.0 -m "Version 1.0 release"
# You can also tag a specific commit by providing its hash:$ git tag -a v0.9 -m "Pre-release version 0.9" a1b2c3dCreating a Lightweight Tag
Section titled “Creating a Lightweight Tag”To create a lightweight tag, simply provide the tag name without the -a, -s, or -m options:
$ git tag v1.0-lw # Creates a lightweight tag v1.0-lw pointing to HEADViewing Tags
Section titled “Viewing Tags”To list all available tags in your repository, use the git tag command (or git tag -l):
$ git tag# v1.0# v1.0-lwYou can also search for tags that match a particular pattern:
$ git tag -l "v1.*" # Lists tags starting with v1.# v1.0-lwTo view the data associated with a tag (especially an annotated tag), use git show <tagname>:
$ git show v1.0The output for an annotated tag will look something like this:
tag v1.0Tagger: Your Name <your.email@example.com>Date: Mon Jan 1 10:00:00 2024 +0000
Version 1.0 release
commit a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0 (HEAD -> main, tag: v1.0-lw, tag: v1.0, tag: v0.9)Author: Committer Name <committer.email@example.com>Date: Mon Jan 1 09:50:00 2024 +0000
Add super feature
diff --git a/feature.txt b/feature.txtnew file mode 100644index 0000000..e69de29For a lightweight tag, git show will display information about the commit the tag points to, without the tag-specific metadata.
Sharing Tags (Pushing to Remote)
Section titled “Sharing Tags (Pushing to Remote)”By default, git push does not transfer tags to remote servers. You need to explicitly push tags.
To push a single specific tag:
$ git push origin v1.0To push all your local tags that are not yet on the remote:
$ git push origin --tagsAfter pushing, other collaborators can fetch these tags into their local repositories.
Deleting Tags
Section titled “Deleting Tags”Deleting a Local Tag
Section titled “Deleting a Local Tag”To delete a tag from your local repository, use git tag -d <tagname>:
$ git tag -d v1.0-lw# Deleted tag 'v1.0-lw' (was fedcba9)Deleting a Remote Tag
Section titled “Deleting a Remote Tag”If you’ve pushed a tag to a remote repository and need to delete it from there, you need to push a special ‘delete’ reference. The modern syntax is git push origin --delete <tagname>:
$ git push origin --delete v0.9# To https://github.com/user/project.git# - [deleted] v0.9An older, but still functional, syntax is git push origin :refs/tags/<tagname>:
$ git push origin :refs/tags/old-tag-to-deleteNote: Deleting a tag from a remote repository should be done with caution, especially if others might already be using it.
Checking Out Tags
Section titled “Checking Out Tags”If you want to view the state of your project at a particular tag, you can check it out. This will put you in a ‘detached HEAD’ state, meaning you’re not on any branch. It’s fine for inspection or creating a new branch from that point.
$ git checkout v1.0# Note: switching to 'v1.0'.# You are in 'detached HEAD' state...If you want to work on files from that tagged version, it’s usually best to create a new branch from the tag:
$ git checkout -b branch_from_v1.0 v1.0# Switched to a new branch 'branch_from_v1.0'Practical Applications
Section titled “Practical Applications”Tags are crucial for:
- Software Releases: Marking stable versions like
v1.0.0,v1.0.1,v2.0.0-rc1. - Deployment Triggers: CI/CD systems often use tags to trigger build and deployment pipelines for specific versions.
- Historical Markers: Identifying significant milestones or stable points in a project’s history for easy reference.
For more details on tagging, consult the official Git documentation on Tagging.