🔖 What Happens to Tags and Commits When You Delete a Git Branch?

RAJRAJ
3 min read

In my previous blog, I covered Git release management using branches and tags. A common follow-up question I’ve received is:

“If I create a tag from a branch and then delete that branch, what happens to the tag and the associated commit?”

Let’s break it down clearly.


🏷️ Tags Are Independent of Branches

When you create a tag in Git, it doesn’t get attached to the branch — it points directly to a commit hash.

This means:

  • If you create a tag (say, v1.0.0) from a branch (e.g., release/1.0.0) and then delete the branch,

  • The tag still exists and remains valid.

  • You can still checkout the tag, reference it in CI/CD, or create a release from it.

✔️ TL;DR: Deleting the branch has no effect on the tag.


🔗 But What About the Commit Itself?

Here’s where it gets interesting. Git is efficient and tries to clean up unreferenced commits eventually via garbage collection.

So what happens to the commit that the tag was pointing to?

If the commit is still referenced by a tag:

✅ The commit stays.
Because tags are also Git references (just like branches), Git sees the commit as “reachable” and won’t delete it.

If the commit has no references at all:

⚠️ The commit will eventually be garbage collected — typically after 30 days — if no tag or branch points to it.

In short:

SituationWill the commit be deleted?
Tagged and branch deleted❌ No
Not tagged and branch deleted✅ Eventually (via GC)
Still referenced by another branch❌ No

🧪 Real-World Example

Let’s say you:

# Create a branch and make changes
git checkout -b release/1.0.0
# Commit your release
git commit -am "Release version 1.0.0"
# Tag it
git tag v1.0.0
# Push the tag and branch to remote
git push origin v1.0.0 release/1.0.0

Then later:

# Delete the branch
git push origin --delete release/1.0.0

The tag v1.0.0 still exists and points to the same commit. That commit is safe.


🔐 Best Practices

  • ✅ Always push your tags to remote (GitLab, GitHub, etc.) to preserve them long-term.

  • 🔁 Use tags for immutable release references; branches are great for ongoing development.

  • 🧹 Avoid creating and deleting branches without tagging if the commits are important.


💬 Final Thoughts

This distinction between branches, tags, and commits is essential for solid Git hygiene. Tags are your reliable bookmarks, and as long as they exist, your commits are safe — even if the branches disappear.

Want to automate this in CI/CD or handle Git versioning in large teams? Feel free to connect or drop a comment!

0
Subscribe to my newsletter

Read articles from RAJ directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RAJ
RAJ

I am a Senior Software Engineer from India.