Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to REALLY remove a tag on git / SourceTree

I know how to delete a tag from SourceTree. Just right-click, delete, and tick "remove tag from all remotes." It then executes the following (and I'm omitting the flags -c diff.mnemonicprefix=false -c core.quotepath=false for readability):

git tag -d my_tag
git push -v origin :refs/tags/my_tag

This works.

Here is the problem.

Some time later, one of the other developers will push their feature branch to origin, and SourceTree will automatically push all local tags to the remote server. This will recreate the tag I just deleted.

I know the idea is that tags are not "supposed" to be deleted, e.g. tagged releases, but sometimes mistakes happen.

Any advice?

like image 725
Peet Brits Avatar asked Feb 11 '16 08:02

Peet Brits


People also ask

Can you remove a tag in git?

To delete the Git tag from the local repo, run the git tag -d tag-name command where tag-name is the name of the Git tag you want to delete. To get a list of Git tag names, run git tag.

How do I delete tags?

Click on the tag in the example page or email. From the pop-up menu that displays after you click the tag, select Clear tag.

How do I remove a tag from bitbucket git?

You can't remove a tag from Bitbucket after you've added it.


1 Answers

The only solution I have found is to simply ask all the other developers to remove their local copies of the tags after you have deleted them from the remote. I found a script help here.

To put it simple, if you are trying to do something like git fetch -p -t, it will not work starting with git version 1.9.4.

However, there is a simple workaround that still works in latest versions:

git tag -l | xargs git tag -d # remove all local tags
git fetch -t                  # fetch remote tags

A one liner can be written as:

git tag -l | xargs git tag -d && git fetch -t

Alternative, you can add a new alias to your ~/.gitconfig file to make things shorter:

in ~/.gitconfig

[alias]

     pt = !git tag -l | xargs git tag -d && git fetch -t 

Now, you can simply call pt alias to prune local stale tags:

git pt
like image 73
DontPanic345 Avatar answered Oct 23 '22 23:10

DontPanic345