How to update local tags to match remote?

GitTags

Git Problem Overview


I fixed it for my repo by deleting the local tag and then doing a git fetch. This brought the updated tag.

Is there are "right" way to update tags that may have changed on the remote? This is a simple tag, not signed or anything, created with "git tag "

Git Solutions


Solution 1 - Git

Make sure you fetch all the tags (through git fetch --tags), to get all the tags and not just ones referencing commits reachable from the branch heads.

Those (fetched) tags are annotated ones (and usually not lightweight), and if you add deleted one on the local repo, they will just pop back after the fetch.

However, if you have deleted a lightweight one, then you need to recreate it locally: a lightweight tag isn't usually pushed or fetched to/from a remote repo.

Note that starting git 1.9/2.0 (Q1 2014), git fetch --tags will fetch everything (like git fetch), plus the tags. See "Does “git fetch --tags” include “git fetch”?".

Again, fetch "everything" means annotated and lightweight (if those lightweight tags were previously pushed).


As noted below in biocyberman's answer, if you want to fetch tags from all remotes (not just the default remote named 'origin'), you need to add the --all option.

git fetch --tags --all

Solution 2 - Git

Previous to git 2.30, the right way seemed to be:

  git fetch origin --tags --force

You should avoid to have a branch with the same tag name, because the checkout prioritizes the branch and you can feel like the tag was not updated. Maybe git should have a warning in this case, something like:

> You have updated a tag that differs now from a branch of > the same name. The reference to "tagname" became ambiguous.

Solution 3 - Git

What you have said is the right way and that is what the git tag manual recommends ( actually, it says, don't change the tags on the remote repo unless the world is coming to an end):

git tag -d X
git fetch origin tag X

Solution 4 - Git

In case one has multiple upstreams:

git --version
git version 2.11.1 
git fetch --tags --all

without the --all option, I could not fetch the tags from the upstream whose name is not "upstream".

Solution 5 - Git

In fact git fetch --tags is enough to let git overwrite lightweight and annotated tags by remote tags of either kind. You can consider it a documentation bug for not mentioning that.

Local tags with names which have no equivalent on the remote will be left alone with this command.

Tested with git version 2.7.4.

Solution 6 - Git

I don't think it's a bug. Though you shouldn't change tags, if one changes upstream, this will update the tag on your repo:

git fetch origin "+refs/tags/*:refs/tags/*"

Solution 7 - Git

Short cheat sheet with example below for tag XTAGX and commit ID 339f42b to whom, who are using tags only to mark specific commits for yourself (it might be okay to do re-tagging) and not for an opened to wide audience releases. Otherwise it is not good idea and @dannysauer already put a link with great explanation and use case.

remove locally: git tag -d XTAGX
remove on remote: git push -d origin XTAGX
create locally: git tag -a XTAGX -m "My XTAGX git tag" 339f42b
push to remote: git push origin --tags
list locally: git tags --list
list on remote: git ls-remote --tags origin
additional check to see tags in all history: git log --oneline

Good reference with explanations: https://stackoverflow.com/a/5480292/2957102

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondonatelloView Question on Stackoverflow
Solution 1 - GitVonCView Answer on Stackoverflow
Solution 2 - GityucerView Answer on Stackoverflow
Solution 3 - GitmanojldsView Answer on Stackoverflow
Solution 4 - GitbiocybermanView Answer on Stackoverflow
Solution 5 - GitRobert SiemerView Answer on Stackoverflow
Solution 6 - GitdbkaplunView Answer on Stackoverflow
Solution 7 - GitSysaninView Answer on Stackoverflow