In git, how do I sync my tags against a remote server?

Is there a way to keep my local git tags in lockstep with a remote's tags? That is -- not only get new tags when created (as usual, when fetch-ing/pull-ing), but also prune tags no longer on a remote and also delete existing tags when someone else git push -f's a tag. I know I can git fetch remotename followed by git remote prune remotename to achieve similar behaviour for branches.

50964 次浏览

git push --tags will push your local tags up to the server. By default, git fetch (the first half of git pull or git pull --rebase) will pull tags, but you can specify -t or --tags to pull all of them.

I'm not sure how to prune remotely deleted tags, but the fetch should pull down any force-updated tags.

A bit of research has shown that git has no way to tell the difference between local or foreign tags (all tags go to .git/refs/tags/). Therefore, it is not possible to determine the difference between locally created tags and prune-able remote tags. The options, are then reduced to: having an ever growing set of tags, or only the tags that are on the server.

git push --tags origin && \
git tag | xargs -n1 git tag -d && \
git fetch --tags

Drop the first line for the latter behaviour, and could be potentially git alias'd for frequent usage.

An alternative would be to create a branch (as they can be identified as local/remote) at a tag point and are never write to it again. Then using remotename/branchname as a tag to checkout would keep tags in sync (in addition to git fetch and git remote prune remotename).

Either way is a hack, and the "right" answer is to stop changing tags all the time.

The following worked for me:

git fetch --prune --tags

disclaimer this uses git internals (some may argue that the filesystem is a git interface, but that's for another day :D)

# Blow away all local tags, this will remove any that are tagged locally
# but are not on the remote
rm .git/refs/tags/*


# Download all the tags from the remote
git fetch --tags

Here's an alternative solution:

git fetch -p +refs/tags/*:refs/tags/*

From the git fetch doc:

-p --prune

Before fetching, remove any remote-tracking references that no longer exist on the remote. Tags are not subject to pruning if they are fetched only because of the default tag auto-following or due to a --tags option. However, if tags are fetched due to an explicit refspec (either on the command line or in the remote configuration, for example if the remote was cloned with the --mirror option), then they are also subject to pruning.

Another solution which actually works for me:

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

...also prune tags no longer on a remote

git fetch gets with Git 2.17 (Q2 2018) an handy short-hand for getting rid of stale tags that are locally held.

See commit 6317972, commit 97716d2, commit e249ce0, commit 627a129, commit d0e0747, commit 2c72ed7, commit e1790f9, commit 59caf52, commit 82f34e0, commit 63179720, commit 63179721, commit 63179722, commit 63179723, commit 63179724, commit 63179725, commit 63179726, commit 63179727 (09 Feb 2018) by commit 63179728.
(Merged by Junio C Hamano -- gitster -- in commit c1a7902, 06 Mar 2018)

fetch: add a --prune-tags option and fetch.pruneTags config

Add a --prune-tags option to git-fetch, along with fetch.pruneTags config option and a -P shorthand (-p is --prune).
This allows for doing any of:

git fetch -p -P
git fetch --prune --prune-tags
git fetch -p -P origin
git fetch --prune --prune-tags origin

Or simply:

git config fetch.prune true &&
git config fetch.pruneTags true &&
git fetch

Instead of the much more verbose:

git fetch --prune origin 'refs/tags/*:refs/tags/*' '+refs/heads/*:refs/remotes/origin/*'

Before this feature it was painful to support the use-case of pulling from a repo which is having both its branches and tags deleted regularly, and have our local references to reflect upstream.

At work we create deployment tags in the repo for each rollout, and there's lots of those, so they're archived within weeks for performance reasons.

Without this change it's hard to centrally configure such repos in /etc/gitconfig (on servers that are only used for working with them). You need to set fetch.prune=true globally, and then for each repo:

git -C {} config --replace-all remote.origin.fetch "refs/tags/*:refs/tags/*" "^\+*refs/tags/\*:refs/tags/\*$"

Now I can simply set fetch.pruneTags=true in /etc/gitconfig as well, and users running "git pull" will automatically get the pruning semantics I want.


Update Apr. 2021, Git for Windows 2.30.1 and GitHub Desktop 2.8

It works if you have set prune and pruneTags options:

cd C:\path\to\local\repo
git config fetch.prune true
git config fetch.pruneTags true

Then click on Fetch origin in GitHub Deskop: the logs will show:

2021-04-28T20:25:21.244Z - info: [ui] Executing fetch:
git -c credential.helper= -c protocol.version=2 fetch --progress --prune origin (took 2.986s)

... and any local tag not present in the remote will be gone!

use these command to sync tags(delete all local then fetch all remote)

git tag -d $(git tag) # delete all local tags
git fetch --all # fetch all remote to local