How to tag an older commit in Git

We can tag an older commit in Git using the git tag command.

The git tag command is used to add tag references. The basic syntax of the git tag command is as follows:

git tag <OPTIONS>

The options relevant to this Answer are as follows:

  • -a/--annotate: This creates an unsigned tag object.
  • -m/--message:This is the tag message associated with the tag.

There are two steps in tagging an older commit in Git.

  • Tag the relevant commit ID with the git tag command. (Assuming we already know the commit ID).
  • Specify the tag in the git push command.

Refer to git push to understand more about the Git push command.

Tag the commit

The following command can be used to tag the commit.

git tag -a <tag_name> <commit_id> -m <message>

For example,

git tag -a v0.2.1 u4bgd123 -m "v0.2.1"

The code u4bgd123 is the beginning part of the commit ID.

Push the tag

The following command can be used to push the tag to the remote.

git push <head> <tag_name>

For example,

git push origin v0.2.1

Finally, we can verify using the git log command to see all the commit IDs in the current branch.

Free Resources

Copyright ©2024 Educative, Inc. All rights reserved