Git Tags

1 min

Table of contents

  1. What Is a Tag?
  2. Types of Tags
  3. Practical Use
  4. Version Management

1. What Is a Tag?

Git tag = Permanent label on a specific commit.

Main use: Mark versions (v1.0, v2.0, etc.)

Difference from branches:

  • Branch = Mobile, evolves with new commits
  • Tag = Fixed, always points to the same commit

Analogy: Tag = a "Version 1.0" sticker on a notebook page.

Back to table of contents

2. Types of Tags

Lightweight Tag (simple)

bash
git tag v1.0                # Simple tag
  • Just a name
  • Pointer to a commit
  • Fast to create

Annotated Tag (complete)

bash
git tag -a v1.0 -m "Version 1.0 - First stable release"
  • Contains a message
  • Date and author
  • Can be signed (GPG)
  • Recommended for official releases

Back to table of contents

3. Practical Use

Project setup:

bash
mkdir demo-tags && cd demo-tags
git init

echo "print('app v0.1')" > app.py
git add . && git commit -m "Initial version"

echo "print('app v0.2')" > app.py
git add . && git commit -m "Bug fixes"

echo "print('app v1.0')" > app.py
git add . && git commit -m "First stable version"

Create tags:

bash
# Simple tag
git tag v1.0

# Tag with a message
git tag -a v1.0-stable -m "Version 1.0 - Production ready"

# Tag on a previous commit
git log --oneline           # See the hashes
git tag v0.2-beta <hash>    # Tag on a specific commit

List tags:

bash
git tag                     # All tags
git tag -l "v1.*"          # Tags matching a pattern
git show v1.0              # Tag details
bash
git checkout v1.0          # Go to the tagged commit
git checkout main          # Go back to main

Push tags:

bash
git push origin v1.0       # Push a specific tag
git push origin --tags     # Push all tags

Back to table of contents

4. Version Management

Semantic Versioning convention:

vMAJOR.MINOR.PATCH
- v1.0.0 → First stable release
- v1.0.1 → Bug fix
- v1.1.0 → New feature (backward compatible)
- v2.0.0 → Breaking changes

Typical release workflow:

bash
# Development finished
git checkout main
git pull origin main

# Release tag
git tag -a v1.2.0 -m "Release v1.2.0 - Add user authentication"

# Push code + tags
git push origin main
git push origin v1.2.0

# Or push everything at once:
git push origin main --tags

Managing tags:

bash
# Delete a local tag
git tag -d v1.0

# Delete a remote tag
git push origin --delete v1.0

# Modify a tag (recreate)
git tag -d v1.0                           # Delete locally
git push origin --delete v1.0             # Delete remotely
git tag -a v1.0 -m "Corrected message"    # Recreate
git push origin v1.0                      # Re-push

Use cases:

1. Production release:

bash
git tag -a v2.1.0 -m "Production release v2.1.0"
git push origin v2.1.0
# Deploy version v2.1.0 to production

2. Go back to a previous version:

bash
git checkout v2.0.0        # Go back to a stable version
git checkout -b hotfix-v2.0.0    # Create a hotfix branch

3. Compare versions:

bash
git diff v1.0.0 v2.0.0     # See all the changes

Tags = Permanent milestones of your project. Use them for all your releases!

Back to table of contents