Git

How to Tag an Older Commit in Git?

On Git, tags are the reference’s that point to a particular HEAD position in the Git history. Moreover, they are like a branch that cannot be modified(immutable). However, after being created, there is no need for further commit history. Additionally, developers can tag an older commit in the Git repository using the “$ git commit -a <tag> -m <commit-message>” command.

This guide will elaborate on the procedure to tag an older commit in the Git repository.

How to Tag an Existing Commit in Git?

Check out the below-stated procedure to tag an old commit in the Git repository:

  • Go to the particular Git repository.
  • Check the log history of the current working Git repository.
  • Copy the required commit SHA hash.
  • View the list of the existing Git tags.
  • Use the “$ git commit -a <tag> -m <commit-message>” command.

Step 1: Move to Required Git Repository
First, navigate to the desired local repository by running the “cd” command along with its path:

$ cd "C:\Users\nazma\Git\test_004"

Step 2: Check Repository Reference Log History
Then, view the reference commit history of the particular repository with the help of the “git log .” command:

$ git log .

As a result, the above-stated command will display all the log history. Copy the SHA hash of the desired commit you want to tag. For instance, we have copied the “a8d5313” commit Id of the “deleted” commit message:

Step 3: Checkout to Commit SHA Hash
Next, execute the “git checkout” command along with the copied commit SHA hash and switch to it:

$ git checkout a8d5313

Step 4: View Existing Git Tag List
Run the “git tag” command to display the list of existing Git tags:

$ git tag

Step 5: Tag Desired Commit
After that, tag the selected old commit by running the “git tag” command along with the tag and commit message:

$ git tag -a v2.2 -m "tag an older commit"

In the above-stated command:

  • -a” flag is used to track all the added changes automatically.
  • v2.2” is the new Git tag.
  • -m” option adds a commit message to the repository.

Step 6: Check Log History
Lastly, check the log of a particular Git repository by running the “git log .” command:

$ git log .

That’s all! We have explained the procedure to tag an older commit in the Git repository.

Conclusion

To tag an old commit in the Git repository, first, move to the Git particular repository, check the log history of the current working Git repository, and copy the required commit SHA hash. After that, display the existing Git tags. Lastly, run the “$ git commit -a <tag> -m <commit-message>” command. This guide explained the method to tag an older commit in the Git repository.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.