Moving a tag on a Git branch to a different commit

 
Today I slipped up when assigning a release tag to a Git repo. After committing my changes and pushing them to GitHub I tagged the HEAD as v1.0.0 and pushed the tags to the remote. Almost immediately I realised that I had forgotten to make a change that I really wanted to be included in that release. Now, the changes had already been pushed to the remote (GitHub in this case) so the sensible and proper thing to do would be to commit my fix and tag it as v1.0.1 but, as this is currently a private repo which only I have access to, I decided to move the tag.
It’s been a while since I’ve done that so it took me a few attempts to remember exactly the steps involved so I thought I’d document them here “just in case”.

If all you’re looking for is the commands, here they are (in these examples I’m assuming the tag to be moved is ‘v1.0.0’). A more detailed explanation follows.


$ git tag -d v1.0.0
$ git push origin :refs/tags/v1.0.0
$ git tag v1.0.0
$ git push origin master --tags



How it works

Firstly, let’s have a look at the state of the repo before running any of these commands


$ git log

* 276ceee (HEAD, master) Fixed spelling.
* 204f97c (tag: v1.0.0, origin/master) Initial commit.

As expected, the tag v1.0.0 is on the initial commit rather than the HEAD, which is where we want it to be.
[Note that I’ve abbreviated the output for clarity].

A quick check of the repo in GitHub confirms that the tag has already been pushed, and is associated with commit 204f97c

Tags_1

The first thing we want to do is delete the v1.0.0 tag from the local repo using git tag -d:


$ git tag -d v1.0.0

Deleted tag 'v1.0.0' (was 204f97c)

$ git log

* 276ceee (HEAD, master) Fixed spelling.
* 204f97c (origin/master) Initial commit.


Then remove it from the remote repo (GitHub):


$ git push origin :refs/tags/v1.0.0

To https://github.com/davidwaterston/test.git
- [deleted] v1.0.0


Then we can assign the tag to the new HEAD:


$ git tag v1.0.0
$ git lg
* 276ceee (HEAD, tag: v1.0.0, master) Fixed spelling.
* 204f97c (origin/master) Initial commit.


and push it to the remote:


$ git push origin master --tags

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 288 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/davidwaterston/test.git
204f97c..276ceee master -> master
* [new tag] v1.0.0 -> v1.0.0


Another quick look at GitHub and we can see that the tag is still there but has been moved to the new commit, 276ceee:

Tags_2

Mission accomplished.

One thought on “Moving a tag on a Git branch to a different commit

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s