How to use tags in Git

A tag in Git is a user-friendly label attached to a particular commit. It is commonly used to mark a particular commit, for example to mark as a release version. Using tags in Git is quite easy, once you notice how they are pushed to a remote repository.

  • To list tags in your repository:
$ git tag
  • To checkout a tag, it has to be checked out to a new local branch:
$ git checkout -b FOOBAR_BRANCH FOOBAR_TAG
  • To mark the current commit with a tag named FOOBAR_TAG:
$ git tag FOOBAR_TAG
  • If you know the hash of a particular commit, say abcd1234, and want to attach a tag to it:
$ git tag FOOBAR_TAG abcd1234
  • If you want to label the head commit of a branch with a tag:
  • There are times when you want to do a reverse lookup, to lookup the commit associated with a tag. There are a couple of ways to do this:
$ git log -n 1 FOOBAR_TAG
$ git rev-list -n 1 FOOBAR_TAG
  • When you push your new commits to a remote, the tags you created locally are not pushed. This command pushes only the tags (not any new commits) to a remote:
$ git push FOOBAR_REMOTE --tags
  • There are times when you want to move a tag to a different commit. This operation essentially removes the old tag and writes a tag with the same name at the new commit. This can be done if you create the new tag (with the same name) with force:
$ git tag -f FOOBAR_TAG
  • If you try to push a moved tag (like the above example) to a remote, Git will complain that the remote already has a tag of the same name pointing at a different commit. Again, you use force to coerce Git to do this:
$ git push -f FOOBAR_REMOTE --tags

Tried with: Git 2.7.4 and Ubuntu 16.04

One thought on “How to use tags in Git

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.