gitgit-branch

git delete tags associated with removed branches


On GIT After I removed a branch there are tags still exists. I used this command to delete local and then remote branch "brnch"

git branch -D "brnch"
git push origin --delete "brnch"

But the tags associated with the branch still exists. I am able to successfully check out the tag TAG from the deleted branch. It shows no branches but exists

$ git checkout TAG
$ git branch -a --contains TAG
* (HEAD detached at TAG)

How can I delete all the tags on the deleted branch locally and remotely ? including any commit ids on the branch. Complete clean up. Appreciate any help on this. I am bit confused here


Solution

  • I did this to cleanup the tags and commits linked to the deleted branches

    first find out the tags those are not linked with any branches

    git tag $(git branch --all --list --format="--no-merged %(refname:short)")
    

    then deleted then like all other tags locally and remotely

    git push origin -d "tagname"
    git tag -d "tagname"
    

    After deleting forcefully clean the cache else the commit hash can be referenced

    git stash clear
    git reflog expire --expire-unreachable=now --expire=now  --all
    git fsck --unreachable
    git gc --prune=now --aggressive
    

    You cannot control the cleanup in remote so what I did is deleted the remote and created an empty repo with the same name and then pushed the cleaned local repo. Using the command something similar to this.

    git push --set-upstream -f origin --all
    git push --set-upstream -f origin --tags
    

    This is to make sure the commits and tags are immediately removed