Tags

Tags

Goals

By the end of this lesson you should be able to

  • Label specific commits using the git tag command
  • Remove existing tags

Overview

Using the git tag command you can give a label to any commit. This can be helpful for marking certain important commits.

For example, it is common practice when writing software to tag each commit that corresponds to a major version release. You might tag the commit that is your initial software release with the label v1.0. Later, you might tag another commit as v1.1, and so on.

In most Git commands, anywhere that you can refer to a commit ID you can enter a tag name instead. Git automatically translates that tag name to the correct commit ID.

Tags Figure: Tags
Tags are just labels for specific commits. In this figure, commit 9d52 could also be referred to as v1.0 in many Git commands. Note that here, tag v1.1 points to the same commit as HEAD (and master).
NOTE: There are quite a few rules for valid tag names. These rules can be found in the Git documentation, but in general sticking to standard version numbers like the ones you have encountered here, or basic alpha-numeric names is always safe.

Adding Tags

You can tag a commit by providing a tag name and an optional commit ID as arguments to the git tag command. If you do not specify a commit ID the default is to tag HEAD (usually the most recent commit).

# Format for adding a tag
$ git tag <tagname> [<commit_id>]

Examples

# Tag the most recent commit with the label 'foo'
git tag foo

# Tag commit 3d11 with the label 'foo'
git tag foo 3d11

Tag your most recent commit with a label of your choice:

$ git tag mytag

Choose one of your previous commits and give it a tag of your choice (your commit ID will be different than the one shown below):

$ git tag anothertag 7d7596a

Listing Tags

Running git tag by itself will list all existing tags.

List your existing tags:

$ git tag
mytag         # You should see here the tag names you chose
anothertag

Tags will also be noted in git log:

Run a git log and note that your tags are marked beside the corresponding commit:

$ git log --oneline
8916e31 (HEAD -> master, tag: mytag) Made some content changes
9d52001 Added more files and text
7d7596a (tag: anothertag) Added another change tracking description
312fabf Added a description about Git change tracking
3f21652 Added brief description
17f9667 Initial commit

Removing Tags

Tags can be removed using the -d (delete) option, as in

# Format for deleting tags
$ git tag -d <tagname>

Delete one of the tags you just created:

$ git tag -d mytag
Deleted tag 'mytag' (was b2763fe)

Now that you have a better understanding of the underlying structure of your repository, let's go into a bit more detail about what is happening when you stage changes…