76

I have a tag already pushed onto the remote. When another user creates the same tag and tries to push, the push will fail because the tag already exists on the remote.

But I thought if I did --f force tag push, it should work. But that is not what I see.

I think I have to do this.

 Create tag
 Push tag -> If push fails -> Delete tag on remote
                           -> push tag again.

Is this correct? Isn`t force pushing a tag supposed to take care of this?

I am using annotated tags with

 git -a v1.0 -f -m "message"
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
user3606175
  • 2,013
  • 1
  • 13
  • 16
  • 1
    Have you tried something like git push origin :refs/tags/tag_name to remove the remote tag? – John Powell Sep 12 '14 at 19:44
  • 4
    Normally you can force-push a tag, but a remote can disallow it. Normally you can then delete the tag on the remote, but the remote can disallow this as well. If the remote is sufficiently strict you'll have to get someone with direct access to update the remote. See also http://stackoverflow.com/questions/19298600/tag-already-exists-in-the-remote-error-after-recreating-the-git-tag – torek Sep 12 '14 at 20:03
  • Okay. I assumed -f should always force update the tag on remote. I will do this with delete and recreate logic I added. Thanks for your responses. – user3606175 Sep 12 '14 at 20:27
  • Does this answer your question? [“tag already exists in the remote" error after recreating the git tag](https://stackoverflow.com/questions/19298600/tag-already-exists-in-the-remote-error-after-recreating-the-git-tag) – SherylHohman Jul 24 '20 at 17:40
  • strangely, `git push origin -f` worked for me on git 2.28 – Dr_Zaszuś Oct 02 '20 at 08:48

7 Answers7

102

This will force push all the tags and overwrite the existing ones.

git push -f --tags
David Dekanozishvili
  • 1,163
  • 1
  • 7
  • 3
  • 2
    Great answer, thank you. Or, if your remote is not named "origin", then `git push -f --tags` – Reversed Engineer Jan 30 '19 at 11:35
  • 14
    This is not recommended as it will push all local tags and it is common to have stale/local-only tags created in error which should not be pushed. – Steven Mark Ford Sep 24 '19 at 01:34
  • @StevenMarkFord, how it should be then? – Vassilis Apr 06 '22 at 20:42
  • @Vassilis The stack overflow question here was basically how do you solve the issue with trying to push to a branch that already contains a tag you are trying to push (i.e. it fails because the tag already exists). You can either delete the tag on remote or delete the tag on local before pushing see: https://devconnected.com/how-to-delete-local-and-remote-tags-on-git/ – Steven Mark Ford Apr 08 '22 at 00:44
87

In my case, remote was rejecting an force push when the tag already exists.

So, when the push was rejected, I did

git push --delete origin <tagname>

and pushed the new tag.

Please see Torek's comment to my question. There is a case when remote can reject the delete too.

Marioanzas
  • 1,663
  • 2
  • 10
  • 33
user3606175
  • 2,013
  • 1
  • 13
  • 16
13

Firstly, update the tag on your local:

git tag v0.6.0 -f
Updated tag 'v0.6.0' (was cb85425)

Then update the tag on remote:

git push origin v0.6.0 -f
Total 0 (delta 0), reused 0 (delta 0)

 + cb85425...bf17993 v0.6.0 -> v0.6.0 (forced update)
Izabella Raulin
  • 131
  • 1
  • 2
6

I recommend against force pushing all tags - obv. this force pushes every local tag overwrites the remotes. This can be damaging in situations with state represented with moving tags or if any such feature is added later.

To force push/overwrite the one tag you care about and not all of them.. do:

git push origin tagName -f

TheJeff
  • 3,665
  • 34
  • 52
1

Basically you asking how do you solve the issue with trying to push to a branch that already contains a tag you are trying to push (i.e. it fails because the tag already exists). You can either delete the tag on remote or delete the tag on local before pushing see: devconnected.com/how-to-delete-local-and-remote-tags-on-git

NB: do not use, or be careful when using

git push -f --tags

Because although this may work in some scenarios, be aware it will push ALL local tags and it is common to have stale/local-only tags created in error which should not be pushed

Steven Mark Ford
  • 3,372
  • 21
  • 32
0

if your getting fatal: tag 'beta' already exists error then use

git tag -a beta --force  #To create tag locally
git push -f --tags #To push your tag to the repository
-3

Firstly, delete that tag you want to replace in remote:

git push origin --delete <tag-name>

then push your tag to remote:

git push --tags
Yates Zhou
  • 51
  • 4