I have a repository with only one branch (master
). I'm the only contributor to my repo.
I've recently added a tag
, both locally and pushed to GitHub. After making what I though was the last necessary commit, but now I realize I should have made one more change/commit.
So what I have is:
commit 124
commit 125
commit 126 <-- tag v1.0
commit 127
and I want to move the v1.0
tag to the next commit, ie: 127
, both locally and in GitHub.
How can I do that?
Moving tags is generally discouraged since it can cause problems due to Git's highly distributed nature. Consider:
v1.0
on commitabcd123
v1.0
tag onabcd123
v1.0
to commitcccc222
and pushv1.0
tag on the server doesn't match his localv1.0
tag, so Fred has to manually fix this conflict, even though he didn't do anything to cause it--tags
option to add a new tagsome-tag
that he created; this push is rejected by the server because his localv1.0
tag and the server'sv1.0
tag disagreeWith more than two developers this gets much more complicated; if even one person doesn't take the step to update his or her local tag you can get trouble down the line.
If you're still sure that you want to move the tag (perhaps this is a one developer project, or you're otherwise sure that nobody has fetched the tag, or you're prepared to communicate with all other developers and make sure that they update their local tags) you can do something like this:
Other developers should be encouraged to delete their local copy of the tag and fetch the new one:
Have you ever been to a book club where members do not all use the same edition of the "book of the week"? It's a nightmare, right? Moving a tag would essentially put you in the same situation.
If you think of your repository as a book that chronicles progress in your project, you can think of a tag as a chapter heading.
Moving a tag to a different commit after sharing it is like telling all your book-club buddies
Not good. Moving a tag is a form of history rewriting, and you shouldn't rewrite history that has been shared. It's the surest way to piss your collaborators off. Besides, you write
That may be true for now, but if other people than you have access to your GitHub repository (e.g. if it's public), some of them may already have forked or cloned it (although there is a way to find out), and you run the risk of pissing them off if you rewrite history.
If you're 100% sure that you want to move that tag anyway, Git does allow you to do it. Here, you could use
and then you would have to force push that tag, using
But again, think twice before going ahead...
Addendum (2018/09/26)
I feel the need to revisit my answer...
Over the years, some people have objected in the comments to my injunction not to move an already published tag. Of course, this piece of advice is contextual rather than universal; I don't doubt that good cases for moving a published tag exist. However, I stand firm in the belief that, as a general rule, the decision to move a published tag should be made deliberately and with extreme care.
One recent example comes to mind. Go 1.11 added experimental support for a module system that relies heavily on Git tags for versioning. Moving a tag in Go module that has been published (on GitHub, say) would have disastrous consequences.
By doing so, you would break the contract established between you (the module author) and your users (those who depend on your module), because you would negate the guarantees that Go's module system intends to provide:
That's one sure way to piss people off.
This example may be enough to convince you that, at least in some cases, you shouldn't mindlessly move published tags. I rest my case.