Git Tag For Version Control

Like most VCSs, Git has the ability to tag specific points in history as being important. Generally, people use this
functionality to mark release points (v1.0, and so on). Git uses two main types of tags: lightweight and annotated.
However, it is highly recommended to use the annotated tags, because they are stored as full objects in the Git database. They’re checksummed; contain the tagger name, e-mail, and date; have a tagging message; and can be signed and verified with GNU Privacy Guard (GPG).

  • Create a tag

Creating an annotated tag in Git is simple. The easiest way is to specify -a when you run the tag command:

1
2
3
$ git tag -a v1.0 -m 'first release'
$ git tag
v1.0

The -m specifies a tagging message, which is stored with the tag. If you don’t specify a message for an annotated tag,
Git launches your editor so you can type it in.

  • Show a tag

You can see the tag data along with the commit that was tagged by using the git show command.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$git show v1.0
tag v1.0
Tagger: ewagheg <heng.a.wang@ericsson.com>
Date: Tue May 13 10:15:48 2014 -0400
first release
commit b46d1aab653e27d7104638582233de02d23cd55f
Author: ewagheg <heng.a.wang@ericsson.com>
Date: Tue May 13 10:15:24 2014 -0400
first release
diff --git a/haha.txt b/haha.txt
new file mode 100644
index 0000000..5d28dfc
--- /dev/null
+++ b/haha.txt
@@ -0,0 +1,2 @@
+version 1.0
+

  • Checkout a tag

You can check out a specific version of the tool by using the command “git checkout v1.0”.

  • Delete a tag

You can delete a tag any time if that version is discarded by using the command “git tag –d v1.0”.

1
2
3
4
$git tag -d v1.0
Deleted tag 'v1.0' (was 716206f)
$git tag
v2.0

  • Example

The following example shows how to add a tag to the tool, and how to switch between different versions of the tool.

!!!NOTE: Commit first before adding the tag to the tool.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$git commit -m "First release"
[master (root-commit) 132abfe] First release
1 file changed, 3 insertions(+)
create mode 100644 test.txt
$git tag -a v1.0 -m "the first tag"
$git tag
v1.0
$git show
commit 132abfe4e4ce8f8cefaf2ad9bb1f39c59d5f2b7b
Author: ewagheg <heng.a.wang@ericsson.com>
Date: Tue May 13 10:37:45 2014 -0400
First release
diff --git a/test.txt b/test.txt
new file mode 100644
index 0000000..7c2ac43
--- /dev/null
+++ b/test.txt
@@ -0,0 +1,3 @@
+This is a test file for git tag.
+
+First release.

By doing the above procedure, a tag has been added to the tool. Let’s do some modification for the tool and then create
another tag for it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
$git commit -m "Second release"
[master 3669b09] Second release
1 file changed, 3 insertions(+)
$git tag -a v2.0 -m "the second tag"
$git tag
v1.0
v2.0
$git show v2.0
tag v2.0
Tagger: ewagheg <heng.a.wang@ericsson.com>
Date: Tue May 13 10:43:30 2014 -0400
the second tag
commit 3669b097e09991b6dde1cd66b0e29dd7654755eb
Author: ewagheg <heng.a.wang@ericsson.com>
Date: Tue May 13 10:43:10 2014 -0400
Second release
diff --git a/test.txt b/test.txt
index 7c2ac43..0274106 100644
--- a/test.txt
+++ b/test.txt
@@ -1,3 +1,6 @@
This is a test file for git tag.
First release.
+
+Modifications!
+Second release.

To see the current version of the tool, use “git describe”.

1
2
$git describe
v2.0

Now, let’s check out the first version of the tool.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$git checkout v1.0
Note: checking out 'v1.0'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b new_branch_name
HEAD is now at 132abfe... First release
$git describe
v1.0
$git status
# HEAD detached at v1.0
nothing to commit, working directory clean

Now the tool has been switched to the version v1.0. Let’s switch back to version 2.0.

1
2
3
4
5
6
7
8
$git checkout v2.0
Previous HEAD position was 132abfe... First release
HEAD is now at 3669b09... Second release
$git describe
v2.0
$git status
# HEAD detached at v2.0
nothing to commit, working directory clean

*References

For more information about Git, please find the free online book Git Pro here:
http://git-scm.com/book