BudiBadu Logo
Samplebadu

Git by Example: Tag Deletion

2.43.0

Learn how to remove tags from both your local repository and the remote server. This is useful for fixing mistakes or cleaning up old release candidates.

Code

# 1. Delete a tag locally
git tag -d v1.0

# Output:
# Deleted tag 'v1.0' (was 1a2b3c)

# 2. Delete a tag from the remote repository
git push origin --delete v1.0

# Output:
# To https://github.com/user/repo.git
#  - [deleted]         v1.0

# 3. Alternative syntax for remote deletion (older Git versions)
git push origin :refs/tags/v1.0

Explanation

Sometimes you might create a tag by mistake, perhaps naming it incorrectly or tagging the wrong commit. In these cases, you need to delete the tag. Deleting a tag in Git is a two-step process if you have already pushed the tag to a remote server: you must delete it locally and then delete it remotely.

Deleting a local tag is straightforward using the -d or --delete flag. This removes the reference from your local machine. However, this does not affect the remote repository. If you pull from the remote again, the tag might come back.

To permanently remove the tag from the shared remote repository, you must execute a specific push command. The modern syntax git push origin --delete tagname is intuitive. The older syntax git push origin :refs/tags/tagname is more cryptic but works on very old Git versions; it effectively says "push nothing into the remote tag", overwriting it with null.

  • Deleting locally does not delete remotely
  • Use git tag -d for local deletion
  • Use git push --delete for remote deletion
  • Essential for correcting release mistakes

Code Breakdown

2
Removes the tag 'v1.0' from your local repository. The commit it pointed to remains untouched.
8
Tells the remote server 'origin' to delete its version of the tag. This ensures other developers won't fetch the bad tag.
15
The classic way to delete a remote tag. It's good to recognize this syntax if you encounter it in older scripts or documentation.