Git Tags and Release Management
# CHAPTER 13
Git Tags and Release Management
1. Introduction
Branches are temporary. Thefeature/login branch is created, merged, and deleted in 3 days. Even the main branch is in a state of constant, fluid motion as new commits are integrated daily. This fluidity is terrible for software releases. When a customer downloads "Version 2.4.1" of your application, you need an absolute mathematical guarantee that the code they receive will perfectly match a specific, immutable point in history. You cannot tell a customer to "download the 45th commit on the main branch." In this chapter, we will introduce Git Tags, the permanent anchors of repository history. We will learn how to create tags, utilize Semantic Versioning, and automate release workflows.
2. Learning Objectives
By the end of this chapter, you will be able to:- Define the operational concept of a Git Tag.
- Differentiate between Lightweight Tags and Annotated Tags.
- Create, list, and delete Annotated Tags.
- Push tags to remote repositories securely.
- Understand and implement Semantic Versioning (SemVer) principles.
3. Beginner-to-Advanced Explanations
The Problem: You finish building your software. The commit hash isa1b2c3d. You package it up, email it to your customers, and call it "Version 1.0".
Six months later, a customer complains about a bug in "Version 1.0". You open your Git repository. It now has 1,000 new commits. How do you find exactly what the code looked like six months ago? You can't remember that the hash was a1b2c3d.
The Solution (Tags):
A Tag is a permanent bookmark attached to a specific commit hash.
Unlike a branch pointer (which constantly moves forward every time you commit), a Tag pointer is frozen in time. It will point to a1b2c3d forever. You simply command Git: git tag v1.0 a1b2c3d. Now, whenever you need to see Version 1.0, you just type git checkout v1.0.
4. Lightweight vs. Annotated Tags
Git supports two types of tags:- 1. Lightweight Tags:
yaml deploytoproduction: script: ./deploy.sh only:
- tags
With this setup, developers can merge code into main 50 times a day, and it will only ever deploy to the testing server. But the moment a Senior Engineer types git tag v2.0.0 and pushes it, the pipeline detects the tag, instantly switches into Production Mode, builds the final artifact, and deploys it to the live internet.
8. Best Practices
-
Never Move a Tag: Once an Annotated Tag is pushed to a public repository and deployed, it is mathematically sacred. If you discover a bug 5 minutes later, DO NOT delete the
v1.0.0 tag, fix the bug, and recreate v1.0.0. This breaks the fundamental law of version control. Fix the bug, commit it, and create v1.0.1.
9. Common Mistakes
-
Checking out a Tag (Detached HEAD): If you type
git checkout v1.0.0 to look at old code, Git warns you that you are in a "Detached HEAD" state. Because a tag is frozen and cannot be modified, any commits you make while standing on a tag will be lost. If you want to fix an old version, you must branch off the tag: git checkout -b hotfix-v1.0 v1.0.0.
10. Exercises
-
1.
Contrast the architectural data stored in a Lightweight Tag versus an Annotated Tag.
-
2.
According to Semantic Versioning (SemVer), what specific type of code change justifies incrementing the MAJOR version number (e.g., from
v1.2 to v2.0)?
11. FAQs
Q: Can I tag a commit I made 3 weeks ago?
A: Yes! If you forgot to tag a release, find the hash of the old commit using git log, and append it to the tag command: git tag -a v1.2.0 9f8a7b6 -m "Late tagging for v1.2"`.