0
0
Gitdevops~5 mins

Tagging specific commits in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to mark a specific point in your project's history to remember a release or important change. Tagging lets you label a commit with a friendly name so you can find it easily later.
When you finish a version of your software and want to mark it as a release.
When you want to save a snapshot of your code before making big changes.
When you need to share a specific commit with your team as a reference point.
When you want to quickly switch back to a known good state in your project.
When you want to create a stable point for deployment or rollback.
Commands
Shows the last 5 commits with their short IDs and messages so you can pick the commit to tag.
Terminal
git log --oneline -5
Expected OutputExpected
a1b2c3d Fix login bug f4e5d6c Add user profile page 9a8b7c6 Update README 123abcd Initial commit 789ef01 Setup project structure
--oneline - Shows each commit in one line for easy reading
-5 - Limits output to last 5 commits
Creates a lightweight tag named 'v1.0' on the commit with ID 'a1b2c3d' to mark it as version 1.0.
Terminal
git tag v1.0 a1b2c3d
Expected OutputExpected
No output (command runs silently)
Lists all tags in the repository to confirm the new tag was created.
Terminal
git tag
Expected OutputExpected
v1.0
Shows details about the commit that the tag 'v1.0' points to, including the commit message and changes.
Terminal
git show v1.0
Expected OutputExpected
commit a1b2c3d Author: Jane Doe <jane@example.com> Date: Tue Apr 23 10:00:00 2024 +0000 Fix login bug diff --git a/login.py b/login.py index e69de29..d95f3ad 100644 --- a/login.py +++ b/login.py @@ -1 +1,2 @@ +Fixed authentication error handling
Key Concept

If you remember nothing else from this pattern, remember: tags are simple labels that point to specific commits to mark important points in your project history.

Common Mistakes
Trying to tag a commit ID that does not exist or is mistyped.
Git will not create the tag and may show an error because the commit ID is invalid.
Use 'git log' to find the correct commit ID before tagging.
Creating a tag without specifying the commit ID, expecting it to tag the latest commit.
Git tags the current HEAD by default, but if you want a specific commit, you must specify it explicitly.
Always specify the commit ID if you want to tag a commit other than the latest.
Not pushing tags to the remote repository after creating them locally.
Tags exist only locally until you push them, so others won't see them.
Use 'git push origin v1.0' to share the tag with others.
Summary
Use 'git log --oneline' to find the commit ID you want to tag.
Create a tag with 'git tag <tagname> <commitID>' to mark that commit.
Verify tags with 'git tag' and inspect with 'git show <tagname>'.