When managing projects with Git, we often need to “mark” code at different stages, such as “the project has completed its first version” or “a critical bug has been fixed.” These marks are Git tags, which help us clearly locate important milestones in a project, especially crucial during version releases.

What are Git Tags and Version Releases?

In simple terms, a tag is a tool in Git used to take a “snapshot” of a specific commit. Whenever we complete a feature, fix a major bug, or reach a milestone (e.g., “Version 1.0”), we can tag the corresponding commit for easy future reference and use.

Version release is the “delivery” process based on these tags—when the project stabilizes to a certain stage, we package the code corresponding to a tag and release it to users or the team.

Why Use Git Tags?

  1. Mark Important Milestones: For example, “v1.0.0” indicates the project’s official launch, and “v2.1.0” indicates the addition of a new feature. Tags make historical versions immediately clear.
  2. Facilitate Version Rollback: If issues are found in a new version, tags allow quick access to previously stable versions.
  3. Clarify Version Meaning: Through tags (e.g., “v1.0.0”), team members can quickly understand the current version’s status (e.g., whether it is stable or fully functional).
  4. Distinguish from Branches: Tags are “static snapshots,” while branches are continuous development lines. For example, the “master” branch is constantly updated, whereas the “v1.0.0” tag always points to the code from version 1.0 of the project.

How to Use Git Tags?

1. Creating Tags

Tags are categorized into two types: lightweight tags and annotated tags. Beginners often prefer annotated tags because they can include version descriptions.

# Create an annotated tag (-a for "annotated", -m for the tag description)
git tag -a v1.0.0 -m "Project first official release"

After execution, Git will tag the latest commit (pointed to by the current HEAD) with v1.0.0 and record the description “Project first official release.”

Lightweight Tags (Quick Marks, No Description)

# Lightweight tag, no description, only records the tag name and commit point
git tag v0.1.0

2. Viewing Tags

After creation, use the following commands to view all tags:

# List all tags
git tag

# Filter tags by specific pattern (e.g., tags starting with v1)
git tag -l "v1.*"

For a large number of tags, you can also view the commit information corresponding to a tag:

git show v1.0.0  # Display detailed commit information for the v1.0.0 tag

3. Deleting Tags

If a tag is miscreated or outdated, it can be deleted:

Delete Local Tags

git tag -d v1.0.0  # Delete the local tag named v1.0.0

Delete Remote Tags

git push origin --delete v1.0.0  # Delete the v1.0.0 tag in the remote repository

4. Pushing Tags to the Remote Repository

Local tags are not automatically synchronized to the remote repository and need to be manually pushed:

# Push a single tag to the remote
git push origin v1.0.0

# Push all unpushed local tags
git push origin --tags

Best Practices for Version Releases

1. Version Numbering: Semantic Versioning

Version numbers typically follow the format MAJOR.MINOR.PATCH (e.g., v1.2.3), with the following meanings:
- MAJOR: Major version update (e.g., 1.0 → 2.0, may include incompatible changes).
- MINOR: Minor feature additions or optimizations (e.g., 1.1 → 1.2, backward compatible).
- PATCH: Bug fixes (e.g., 1.0.1 → 1.0.2, only fixes issues).

For example: v2.0.0 indicates a major update, and v1.5.3 indicates the third bug fix in version 1.5.

2. When to Tag?

  • Stable Versions: After a project’s features are complete and testing passes, tag the official version (e.g., v1.0.0).
  • Milestone Nodes: When core features are completed (e.g., “Login module development finished”), tag with v0.5.0.
  • Emergency Fixes: After fixing critical production bugs, tag with v1.0.1.

3. Using Tags for Version Rollback

To revert to a historical version (e.g., v1.0.0), use the tag to locate it:

# Switch to the code corresponding to the v1.0.0 tag
git checkout v1.0.0

# If you need to preserve the rolled-back code, create a new branch
git checkout -b rollback-v1.0.0 v1.0.0

Common Issues and Considerations

  1. Can Tags Be Duplicated?
    Duplication is not recommended; each tag should uniquely correspond to one version. If an old tag needs to be overwritten, delete the old tag (both locally and remotely) first, then recreate it.

  2. Can Tags Only Be Applied to the Latest Commit?
    No! You can tag using a commit hash: git tag v1.0.0 a1b2c3d (where a1b2c3d is the hash of a historical commit; the first 7 characters suffice).

  3. What’s the Difference Between Branches and Tags?
    - Branches are “dynamic development lines” (e.g., the master branch is continuously updated), while tags are “static snapshots” (e.g., v1.0.0 always points to version 1.0 code and does not update with branch changes).

Conclusion

Git tags are the “anchors” of project version management, helping us mark key milestones, clarify version meanings, and quickly backtrack when needed. Mastering the creation, viewing, deletion, and pushing of tags—combined with semantic versioning standards—can make project version management clearer and collaboration more efficient. Start tagging your first version today!

Xiaoye