82

I have some tags on my "origin" repository. Then I realized I needed to add some changes on one of the tags, and push them back on my repository. Is there a way I can push an existing tag to the repository in one time, or should I delete the tag before ?

azmeuk
  • 4,026
  • 3
  • 37
  • 64
  • 2
    Possible duplicate of [How can I move a tag on a git branch to a different commit?](http://stackoverflow.com/questions/8044583/how-can-i-move-a-tag-on-a-git-branch-to-a-different-commit) – dskrvk Apr 14 '16 at 22:22

5 Answers5

85

This should not be the practice, though you can delete the tag and push the change to the remote repo.

git tag -d tag1
git push origin :refs/tags/tag1
Bijendra
  • 9,467
  • 8
  • 39
  • 66
66

So if you need to move a tag (eg: "v0.5") on a git branch (eg: "master") to a different commit, probably a newer one, then you can use the -f option to git tag:

-f
--force

Replace an existing tag with the given name (instead of failing)

You probably want to use -f in conjunction with -a to force-create an annotated tag instead of a non-annotated one.

Example

  1. Delete the tag on any remote before you push

    git push origin :refs/tags/<tagname>
    

    or for our example:

    $ git push origin master :refs/tags/v0.5
    To git@github.com:org_name/repo_name.git
    - [deleted]         v0.5
    
  2. Replace the tag to reference the most recent commit (using -f will save as the git tag -d <tagname> local tag deletion step).

    git tag -fa <tagname>
    

    or for our example:

    $ git tag -fa "v0.5" -m "version 0.5"
    Updated tag 'v0.5' (was f55c93f)
    
  3. Push the tag to the remote origin

    git push origin --tags
    

    or for our example:

    $ git push origin master --tags
    Counting objects: 1, done.
    Writing objects: 100% (1/1), 196 bytes | 0 bytes/s, done.
    Total 1 (delta 0), reused 0 (delta 0)
    To git@github.com:org_name/repo_name.git
    * [new tag]         v0.5 -> v0.5
    
Exequiel Barrirero
  • 4,998
  • 1
  • 36
  • 27
11

Assuming newtag is the new tag and oldtag is the old tag. Simply do:

# Create new tag that points to the same of old tag
git tag newtag oldtag

# Remove oldtag
git tag -d oldtag

# Remove oldtag in remote machine
git push --delete origin oldtag

# Propapate newtag to remote machine
git push --tags
somenxavier
  • 1,206
  • 3
  • 20
  • 43
  • With this solution, the comment in annotated tag is the old one. If ypu want to replace to another, you have to delete the old tag (`git tag -d oldtag`, `git push --delete origin oldtag`) and make new tag (`git tag -a newtag HASH`, `git push --tags`) – somenxavier Apr 04 '23 at 21:31
6

Way simpler way to replace a tag, also on remote:

git tag -f mytagname
git push -f --tags
L3P3
  • 73
  • 1
  • 5
  • Other working trees may still have the old tag ref, even after pulling – jchook Sep 19 '22 at 20:05
  • 1
    This is good as a one step solution when you're in total control of the remote as an alternative to delete the remote reference then add back the same reference. Because github releases break if the ref is removed remotely – Iuri Guilherme Oct 21 '22 at 21:11
3

I'm not sure I understand your question, but it sounds like it would be simplest to delete the tag, push your change, then re-add the tag...

CDub
  • 13,146
  • 4
  • 51
  • 68