Search code examples
gitgit-remote

Why does git remote prune origin remove my local tags?


I have several tags that reference commits from local branches and remote tracking branches, or ancestors of those commits.

I want to remove references to branches and tags on origin after having run git fetch:

git remote prune origin --dry-run

But the output indicates it would prune my local tags, even ones that were created manually by me instead of being fetched from any remote:

 * [would prune] origin/git-svn
 * [would prune] origin/ignore/some_branch
 * [would prune] refs/tags/MyLocalTag
 * [would prune] refs/tags/MyLocalTag2

Why is git trying to prune my local tags (even those that point to my local branches with no upstream)?


Solution

  • I was able to reproduce this again and narrowed the problem down to the remote.origin.fetch config. I had added a refspec to ensure that git fetch fetched all tags from the remote:

    [remote "origin"]
        url = https://<path-to-repo>.git
        fetch = +refs/heads/*:refs/remotes/origin/*
        fetch = +refs/tags/*:refs/tags/*
    

    As far as I can tell, this works much like setting tagopt = --tags, in that running git fetch origin will copy all tags into .git/refs/tags. However, including this refspec in the config file has the unexpected side effect of causing git remote prune origin to prune all local tags.

    I submitted a bug report which has been met with a series of patches to better explain this in the documentation, as well as (ironically) new flags to specifically enable this behavior of removing tags without needing the extra refspec in the remote's fetch config.