How can I move a tag on a git branch to a different commit?
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
Delete the tag on any remote before you push
git push origin :refs/tags/<tagname>
Replace the tag to reference the most recent commit
git tag -fa <tagname>
Push the tag to the remote origin
git push origin master --tags
To sum up if your remote is called origin
and you're working on master
branch:
git tag -d <tagname> # delete the old tag locally
git push origin :refs/tags/<tagname> # delete the old tag remotely
git tag <tagname> <commitId> # make a new tag locally
git push origin <tagname> # push the new local tag to the remote
Description:
- Line 1 removes the tag in local env.
- Line 2 removes the tag in remote env.
- Line 3 adds the tag to different commit
- Line 4 pushes the change to the remote
You can also change line 4 to git push origin --tags
to push all of your local tag changes/updates to the remote repo.
The above answer is based on content in the question by @eedeep, as well as answers by Stuart Golodetz, Greg Hewgill, and @ben-hocking, and comments below their answers, and @NateS's original comments below my answer.
More precisely, you have to force the addition of the tag, then push with option --tags and -f:
git tag -f -a <tagname>
git push -f --tags