Cyberithub

How to Rename a Tag in Git Using 10 Easy Steps

Advertisements

In this article, we will see how to rename a tag in git using 10 easy Steps. Sometimes you might face a scenario where after tagging a commit with the release version, immediately a bugfix or hotfix will also come under the same release for which there was another commit done and that also needs to be tagged. Now the problem occurs when you already tagged the older commit with the current release version and now you have to tag the latest commit again with the same release version.

This problem can actually be solved by first renaming the old tag to something else so that it will be available for tagging to the latest commit and then tag the latest commit. This whole activity can be performed by following few easy steps which we have explained below in detail with the help of a real world example.

 

How to Rename a Tag in Git Using 10 Easy Steps

How to Rename a Tag in Git Using 10 Easy Steps

Also Read: How to Tag a Git Commit Using 6 Simple Steps

Step 1: Prerequisites

a) You should have a running Windows/Linux/Unix System.

b) You should have git utility installed in your System.

c) You should have access to rename a tag in your System.

 

Step 2: Clone Repo

You need to first clone the repo in your local system by using git clone <repo_url> command. In our case, we are cloning a GitHub repo called atlantis-example by using git clone https://github.com/cyberithub/atlantis-example.git command as shown below.

cyberithub@ubuntu:~$ git clone https://github.com/cyberithub/atlantis-example.git

After cloning the repo, you need to switch to the directory using cd atlantis-example/ command as shown below.

cyberithub@ubuntu:~$ cd atlantis-example/

 

Step 3: Rename a Tag in Local 

To rename a tag, you need to use git tag <new_tag_id> <old_tag_id> command as shown below. So in our case, we are renaming tag v1.4 to v1.4-bugfix using git tag v1.4-bugfix v1.4 command as shown below. You could choose to rename the tag to something else.

cyberithub@ubuntu:~/atlantis-example$ git tag v1.4-bugfix v1.4

If you check git log output then you will notice that the same commit will have both the tag i.e v1.4-bugfix and v1.4 as shown below.

commit 835146f16381163e992e8460d7f61f43a6fa9eae (HEAD -> master, origin/master, origin/HEAD)
Author: CyberITHub <admin@cyberithub.com>
Date: Tue May 23 21:14:07 2023 +0530

Minor changes

commit d09611850909e47b2ee27f4fab23f7ddddd298ab (tag: v1.4-bugfix, tag: v1.4)
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 14:52:42 2023 +0530

Added Changes

commit 4f32267e28fa4463b08748a50dad2136a39a6992
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 13:11:07 2023 +0530

Deleted file

commit fbabc9f74ee363d638ec023c7dba2b74a5e33728
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 11:03:51 2023 +0530

Updated all the changes

 

Step 4: Verify Tag

After renaming the tag, you can verify it by using git tag -l command as shown below.

cyberithub@ubuntu:~/atlantis-example$ git tag -l
v1.1
v1.2
v1.3
v1.4
v1.4-bugfix

 

Step 5: Rename Old Tag in Remote 

Now the changes done in local repo needs to be propagated to the remote repo as well by using git push origin v1.4-bugfix:v1.4 command as shown below.

cyberithub@ubuntu:~/atlantis-example$ git push origin v1.4-bugfix:v1.4
Total 0 (delta 0), reused 0 (delta 0)
To git+ssh://github.com/cyberithub/atlantis-example.git
* [new tag] v1.4-bugfix -> v1.4

 

Step 6: Delete Old Tag in Local 

You then have to delete the old tag i.e v1.4 in our case using git tag -d v1.4 command as shown below. Here -d switch is used for deleting the tag.

cyberithub@ubuntu:~/atlantis-example$ git tag -d v1.4
Deleted tag 'v1.4' (was d3487b0)

After deleting the old tag, you can again verify the list of tags by running git tag -l command as shown below. You will notice that v1.4 is deleted now and no longer tagged to a commit.

cyberithub@ubuntu:~/atlantis-example$ git tag -l
v1.1
v1.2
v1.3
v1.4-bugfix

If you now check the git log output again then you will notice that v1.4 is deleted from the commit d09611850909e47b2ee27f4fab23f7ddddd298ab as shown below.

commit 835146f16381163e992e8460d7f61f43a6fa9eae (HEAD -> master, origin/master, origin/HEAD)
Author: CyberITHub <admin@cyberithub.com>
Date: Tue May 23 21:14:07 2023 +0530

Minor changes

commit d09611850909e47b2ee27f4fab23f7ddddd298ab (tag: v1.4-bugfix)
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 14:52:42 2023 +0530

Added Changes

commit 4f32267e28fa4463b08748a50dad2136a39a6992
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 13:11:07 2023 +0530

Deleted file

commit fbabc9f74ee363d638ec023c7dba2b74a5e33728
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 11:03:51 2023 +0530

Updated all the changes

 

Step 7: Delete Old Tag in Remote 

Similarly, you also need to delete the tag in remote using git push -d origin v1.4 command as shown below.

cyberithub@ubuntu:~/atlantis-example$ git push -d origin v1.4
To git+ssh://github.com/cyberithub/atlantis-example.git
- [deleted] v1.4

 

Step 8: Tag Your Commit

Since the tag id v1.4 is removed, it means this tag is now available to be used in our latest commit. So as per our plan, we would like to tag the latest commit by using git tag v1.4 HEAD command as shown below.

cyberithub@ubuntu:~/atlantis-example$ git tag v1.4 HEAD

After tagging your latest commit, you can verify it by using git log output as shown below. You can see that the latest commit is now tagged with release version v1.4.

commit 835146f16381163e992e8460d7f61f43a6fa9eae (HEAD -> master, tag: v1.4, origin/master, origin/HEAD)
Author: CyberITHub <admin@cyberithub.com>
Date: Tue May 23 21:14:07 2023 +0530

Minor changes

commit d09611850909e47b2ee27f4fab23f7ddddd298ab (tag: v1.4-bugfix)
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 14:52:42 2023 +0530

Added Changes

commit 4f32267e28fa4463b08748a50dad2136a39a6992
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 13:11:07 2023 +0530

Deleted file

commit fbabc9f74ee363d638ec023c7dba2b74a5e33728
Author: CyberITHub User <user@cyberithub.com>
Date: Thu Jan 19 11:03:51 2023 +0530

Updated all the changes

 

Step 9: Push latest Tag Changes to Remote

Then you need to push the local changes to remote using git push origin v1.4 command as shown below. With this step, renaming process is also completed.

cyberithub@ubuntu:~/atlantis-example$ git push origin v1.4
Total 0 (delta 0), reused 0 (delta 0)
To git+ssh://github.com/cyberithub/atlantis-example.git
* [new tag] v1.4 -> v1.4

 

Step 10: Pull all the latest Changes

After updating the changes in remote repo, it is important that all the other users working on the same project must have that latest changes. So before started doing anything, users need to pull the latest changes done by using git pull --prune --tags command as shown below.

cyberithub@ubuntu:~/atlantis-example$ git pull --prune --tags
From git+ssh://github.com/cyberithub/atlantis-example
* [new tag] v1.4 -> v1.4
Already up to date.

Leave a Comment