How to Move an Existing Tag in Git

John Wachira Feb 02, 2024
How to Move an Existing Tag in Git

In this article, we will discuss moving tags in Git. We are discouraged from moving tags, especially if we work as a team since it can mess up our project’s timeline.

Nonetheless, Git allows us to move tags when we need to. Let’s look at a practical example.

Move an Existing Tag in Git

To simulate a scenario where we want to move a tag to the recent commit, we will create a tag V1, makes some commits, and attempt to move the tag to the recent commits. Let’s get started.

This is our local repository’s commit history.

$ git log --oneline

commit history

We will use the git tag command to place the V1 tag on the Sixth Update commit.

$ git tag V1

git tag V1

The tag is now at Sixth Update, as seen from the git log output. Let’s edit the files in our repo and commit the changes.

Here is our new repository’s commit history.

$ git log --oneline

git log –oneline

We get this error if we attempt to run the git tag command.

$ git tag V1
fatal: tag 'V1' already exists

We could always create another tag, but let’s assume we had created the V1 tag and realized there were some modifications we left out. After committing the left-out changes, how do we move the tag?

Like several other commands in Git, we can force our way through. We will have to use the git tag command with the --force option, as shown below.

$ git tag --force V1

Let’s check our commit history.

$ git log --oneline

git tag –force V1

We could proceed to publish to the remote repository if we had already pushed. However, we will need to add the --force option to force the remote to update.

$ git push origin V1 --force

That’s all you need to do to move tags in Git. But before we sign off, let’s see how we can pull after another developer has updated tags in the remote repo.

The first step is deleting the tag in our local repo. We run the below command.

$ git tag -d V1

Now we can run the git pull command to update our local repo.

$ git pull

This should update the tags in your local repository.

We can move tags in Git by introducing the --force option to the git tag command. Be careful while creating and moving tags when working on joint projects.

You can mess up your project’s timeline.

Author: John Wachira
John Wachira avatar John Wachira avatar

John is a Git and PowerShell geek. He uses his expertise in the version control system to help businesses manage their source code. According to him, Shell scripting is the number one choice for automating the management of systems.

LinkedIn

Related Article - Git Tag