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.
$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 <[email protected]> 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.
$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 <[email protected]> Date: Tue May 13 10:43:30 2014 -0400
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