Git Tag

A Tag is used to represent the particular version of a branch in Git repository and unlike branch, the tag does not change, once it is created it loses the ability to changes the history of commits. Tags are created by using the git tag command.

There are two types of Tags:

Both the tags allow us to refer a specific commit in the repository but differ in the amount of metadata they can store

  • Lightweight Tags
  • Annotated Tags

Lightweight Tags:

  • These Tags are the very simplest form of tags to the repository, they store only the Hash of the commit they refer to.
  • These Tags are created with the absence of -a, -s, -m
  • They do not contain any extra information.
  • Lightweight tags are just like bookmarks to a commit.
  • They are just a name and pointer to the commits
  • Useful in creating quick links to relevant commits.

Example:

Lightweight-tag

Annotated Tags:

  • Annotated tags are those, which stores extra metadata like authors name, release notes, tag message, and date as full objects in the git repository.
  • And the above details are very important for the public release of our projects.
  • usually, annotation tags can be achieved by using the -a parameter

Example:

Annotated-tag

  • Executing the above command, you will create a new annotated tag identified with version v2.0.0. The command will then open up your commit editor so that you can fill up the metadata as shown in the above image
  • In case you wanted to add a tag-message you can pass the -m option, this is a method similar to git commit -m.
    Annotated-tag-m

Adding an Android Studio Project to GitHub

Lightweight Tags with Example

In this section am going to cover how to use lightweight tags in Git repository

  • Am currently in the working directory of my Git repository
  • If you do a git status, you can see a clean working directory ahead of origin/master.
    LT-git-status
  • Have a look at our past history by using the command git log --oneline --graph --decorate --all , you can see with the latest commit at the top, And the --decorate puts these markers like HEAD, origin/master, origin/HEAD and master into our git log output.
  • Now HEAD is just a pointer and origin/master and master are branches.
  • Master, of course, refers to the master branch on the local repository and origin/master refers to the master branch on the remote repository.
    LT-git-log

And hence, we have made a lot of changes in our repository. We can mark each significant event or milestones by using the git tagging.

Tags are nothing but labels that we can apply at any commit in history. Now, let us create a lightweight tag by the name called myTag git tag myTag
LT-git-tag-mytag

  • Once again go ahead and do git log git log --oneline --decorate --graph --all, you can see a new item as a tag: and then the tag name myTag at the very top of it.
    LT-git-log-oneline
  • We can also see the list of tags by using the command called git tag --list, you can see that Git has listed our one tag called myTag.
    LT-list-tag
  • Also, we can use the tag name as a reference in other commands like git show myTag , so Git will respond with the commit at that tag.
    LT-git-show-mytag
  • Once again list the tag git tag --list
    LT-list-tag-one
  • So am done with myTag, and hence am going to delete this tag git tag --delete myTag
    LT-git-delete-tag
  • Once again if you list the tag git tag --list, there is nothing to list.
    LT-git-tag-list
  • And if you do git log again git log --oneline --graph --decorate --all there also you will not see the tag name.
    LT-git-log-decorate

Git Editing File

Annotated Tags with Example

This section going to cover how to use the Annotated tags in Git repository.

Let us start from where we left off. Am currently in the working directory of my Git repository.

Let us create an Annotated tag, you already know that annotated tag is going to contain little extra information as I mentioned in the previous section, git tag -a v-1.0 press enter you will invoke to the editor page, enter your tag message as Release 1.0, save and then exit.
AT-creating-annotated-tag

  • Now list the tag git tag --list, you can see our v-1.0 listed over here.
    AT-listout-tags
  • Now let us see this tag in history git log --oneline --graph --decorate --all , you can see at the very top the last commit has associated with tag v-1.0
    AT-git-log
  • Let us show the tag git show v-1.0 , on an annotated tag, we get a slightly different output.
    AT-git-show-tag
  • Now, if we remember from the lightweight tags, the output started with the commit and the commit id but, with an annotated tag, we start with a tag and a tag name, and the person that is issued the tag command and then the date, that the tag was actually created
  • And at last with the tag message then after that, you are going to start with the commit id.

Deleting File that is being tracked by Git

Comparing Tags

This part is going to cover how to compare different types of tags in Git.

  • Start from where we left off. If you do a git status, You are currently in the working directory of my Git repository with a clean working directory on the master branch.
    CT-git-status
  • If you pass the command git tag --list, you can see the v-1.0 tag listed over here.
    CT-git-tag-list
  • Where v-1.0 points to the current commit, Let us add some more tags, before that just list out the files which are part of this repository by doing an ls.
    CT-ls
  • Go ahead and edit the Index.html file, add some text in the title as Title For tagging Example then save and close.
    CT-modify-index-html
  • Commit the changes, git commit -a press enter, the default editor will occur, write your commit message Tweaking file for tagging Example and then save and close.
    CT-commit-a
    CT-commit-message
  • Now let us do a git log, git log --oneline --graph --decorate --all , here our latest commit is not associated with the tag. so whenever we issue the tag command, it will apply that tag to the current commit.
    CT-git-log
  • Now let us create an annotated tag, git tag -a v-1.1 press enter, editor page will open and then enter the tag details over here and then save and exit.
    CT-creating-tag-v-1-1
  • Now let us edit the simple.html file, add some text as updating the title for tagging compare example and then save and close.
    CT-modify-simple-html
  • Now let us commit the changes, git commit -am "updating the title for tag 1.1"
    CT-commit-am
  • I actually got a little bit carried away and hence I wanted to amend my commit message, git commit --amend , and this will allow us to amend our commit message.
    CT-git-commit-amend
  • When your editor appears just update your commit message accordingly, so here am writing commit message as updating tag for 1.2 and then save and close.
  • Let us apply a tag to this commit by using the command, git tag v-1.2 -m "Release 1.2" and then press enter.
    CT-creating-tag-v-1-2
  • It has created our tag, now list out the tags, git tag --list
    CT-git-tag-lis-one
  • We can see these tags in our history by repeating git log command, git log --oneline --decorate --graph --all , you can see that our tags applied to the last three commits.
    CT-git-log-two
  • Now let us show the differences between the different tags, the most popular command to display the difference in Git is git diff v-1.0 v-1.2 , we can see the difference between these two points.
    CT-git-diff
  • We can do the same thing with a visual diff/mergetool if we have that configured, git difftool v-1.0 v-1.2
  • That brings up the first file that is part of the diff, which is the index.html file. So, you can see the changes that we have made between the two tags.
    CT-difftool
  • Once you're done comparing the differences, you can press command+q to exit and advance to the next file.
  • Now, we're comparing the differences between the different tags with the simple.html file. When you're done looking at the differences, command+q.
    CT-coparing-simple-html
  • Since that was the last file involved in our diff, we return to the command prompt



Synchronizing Changes Back to GitHub

Tagging a Specific Commit

Let us understand how to tag a specific commit in the Git repository

  • If I do a git status, Am currently in the working directory of my Git repository, with a clean working directory on the master branch.
    TSC-git-status
  • Let us check for the previous history by using the git log command, git log --oneline --graph --decorate --all , you can see that our last three commits have associated with the tag.
    TSC-git-log
  • Let us say that, we really forgot to tag something that should have been tagged, perhaps a major milestone.
  • So am going to pick up one commit here and am going to tag this commit by using the command called, git tag -a v-0.9-beta 727C2e2 press enter, the text editor will appear to enter the tag message in that as Bets Release 0.9 save and then close.
    TSC-creating-beta-tag
  • Now execute the git log command, git log --oneline --decorate --graph --all, you can see that our tag has associated with the specific commit.
    TSC-tagging-a-remote-commit
  • Repeat the same thing one more time for another commit, just copy the commit id and create an annotated tag by using the command, git tag -a v-0.8-alpha 7bd64fc and enter your tag message in the editor alpha release 0.8 and then save and exit.
    TSC-creating-tag-alpha
  • Once again if you issue the git log command git log --oneline --decorate --graph --all , you can see that our tag has associated with the specific commit.
    TSC-tagging-a-quick-fix-commit

Configure P4merge with Git

Updating Tags

Let us update the tags which we have created with the specified commit message and see how it looks in history.

  • Start From where we actually left off.
  • So if I do git status, am in the clean working directory of my Git Repository.
    UT-git-status
  • If you do a git log command git log --oneline --graph --decorate --all, you can see that we have several commits associated with the various commits.
    UT-git-log-one

And hence, in the previous section, we have associated tag v-0.8-alpha with the commit 7bd64fc. Let us say if I really wanted it to be associated with the done resolving merge commit of id c12321a.

So to do that, we need to take a couple of approaches, the first one is, we can simply delete the tag and then re-create that tag on the corresponding correct commit or we can do it by forcing it.

Here is going to show you how to do it by forcing the tag. So, let's do that now git tag -a v-0.8-alpha -f c12321a , and press enter, this command allows us to edit the previous tag message, so just add in parenthesis as corrected and then save and close.
UT-updating-tag

  • So, it responds as the tag has updated.
    UT-update-message
  • If you issue the git log command git log --oneline --graph --decorate -- all, you can see that the tag has been moved to the correct commit.
    UT-tag-moved

Text Editor Configuration with Git

Using Tags with GitHub

We had reached the last section of this part, let's have a look at using tags with Github.

  • If I do a git status, Git tells that, being in the clean working directory of my Git Repository.
    UTG-git-status
  • Let us list the tags using the command called git tag --list, you can see several tags which we have created in the previous sections.
    UTG-git-tag-list
  • And also if I do git log to see the past history git log --oneline --decorate --graph --all, you can see that we have several tags associated with the specific commits.
  • With our most recent commit being associated with the tag v-1.2.
    UTG-git-log
  • So go to Github and click on the Releases tab and then select the Tags, Github will respond as there are not any releases here, and this is how Github tracks the Tags.
    UTG-github-page
  • Just come back to Git bash and push one particular tag to GitHub git push origin v-0.9-beta and then press enter.
    UTG-git-push-one
  • Now, you can go back to GitHub and refresh the page, you can see our tag v-0.9-beta over there and it also contains the commit id of to which commit it has been associated.
    UTG-git-tag-on-github
  • You can also check your commit history if you go to my commits, you can see the commit that starts with 727c2e2 is the last commit on the Github, So the commit associated with v-1.0.
    UTG-git-tag-commit-changes
  • Let us push one more tag on the Github, this time we are going to push not only the tag but also the commit that was associated with that tag, git push origin v-1.1
    UTG-git-push-two
  • Now, you can go back to Github and select the Releases, we can see that our v-1.1 and if you click on the commit id, you can see the changes for that commit on Github.
    UTG-git-tags-on-github
  • And, there are still several tags that are not on Github, so to push all of our local tags to GitHub at one time we can issue the command including our branch name, git push origin master --tags , where the --tags will synchronize our master branch and push up any tags that were missing.
    UTG-git-push-origin-master
  • If you go back to Github and see Releases, you can see, we have all our release tags.
    UTG-all-tags-github
  • Releases are tracked by the tags and for each of the tag if you click on the "...." it will show you the tag message and it will also list, which commit that each of these tags is associated with.
    UTG-tag-message
  • In addition to that Github will automatically generate zip file or tarball, that contains all the information as that of the commit.
    UTG--zip-tarball
  • What if you accidentally pushed a tag that shouldn't be on GitHub? How would you then delete a tag that's on GitHub?, Well, we can do that by going back to our terminal.
  • And if I do not want to publicize our alpha commit, so am going to use the command as, git push origin :v-0.8-alpha Git responds as v-0.8-alpha has been deleted.
    UTG-delete-tag-alpha
  • To Confirm, you can check it in Github, so the tag v-0.8-alpha has gone.
    UTG-after-deleting-alpha-tag
  • We still have it in our local repository, but it's just not on the remote repository

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions