Git Tutorial

Tagging versions in git

Lets learn how to tag commits for future references

Let’s call the current version of the hello program version 1 (v1).

Step1:Creating a tag for the first version


git tag v1

Now, the current version of the page is referred to as v1.

Step2:Tags for previous versions

Let’s tag the version prior to the current version with the name v1-beta. First of all we will checkout the previous version. Instead of looking up the hash, we are going to use the ^ notation indicating “the parent of v1”.

If the v1^ notation causes troubles, try using v1~1, referencing the same version. This notation means “the first version prior to v1”.


git checkout v1^
cat hello.html


$ git checkout v1^
Note: checking out 'v1^'.

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 8c32287... Added standard HTML page tags
$ cat hello.html
    <h1>Hello, World!</h1>

This is the version with <html> and <body> tags, but without <head>. Let’s make it’s the v1-beta version.


git tag v1-beta

Step3:Check out by the tag name

Now try to checkout between the two tagged versions.


git checkout v1
git checkout v1-beta


$ git checkout v1
Previous HEAD position was 8c32287... Added standard HTML page tags
HEAD is now at fa3c141... Added HTML header
$ git checkout v1-beta
Previous HEAD position was fa3c141... Added HTML header
HEAD is now at 8c32287... Added standard HTML page tags

Step4:Viewing tags with the tag command

You can see the available tags using the git tag command.


git tag


$ git tag

Step5:Viewing tags in logs

You can also check for tags in the log.


git hist master --all


$ git hist master --all
* fa3c141 2020-01-09 | Added HTML header (v1, master) [kwikl3arn]
* 8c32287 2020-01-09 | Added standard HTML page tags (HEAD, v1-beta) [kwikl3arn]
* 43628f7 2020-01-09 | Added h1 tag [kwikl3arn]
* 911e8c9 2020-01-09 | First Commit [kwikl3arn]

You can see tags (v1 and v1-beta) listed in the log together with the name of the branch (master). The HEAD shows the commit you checked out (currently v1-beta).