gittagsbranch

Replace remote tag with Git


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 ?


Solution

  • 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 us 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