Git tag Tutorial with Practical Examples


Written By - Steve Alila
Advertisement

Getting started with git tag

  • A tag object assigns an arbitrary yet presumably human readable name to a specific object, usually a commit.
  • Although 9da581d910c9c4ac93557ca4859e767f5caf5169 refers to an exact and well-defined commit, a more familiar tag name like Ver-1.0-Alpha might make more sense!
  • Tags are labels you can pin to a commit, but unlike branches, they will stay there.
  • git checkout tag is crucial in showing a branch version at a time.
  • For instance, you want to reference a portion of the data in the future. You can do git tagging when you want to mark a stable release version of your code.

But how do you checkout a git tag in a quick and straightforward way?

The simplest way to apply git checkout tag  is to create a branch from a tag and git checkout the branch.

git checkout -b <branch name> <tag name>

Although both tagging and branching affect commits, a git tag is pegged to a specific commit to denote a special significance, while a git branch maps to changes as you add commits. You can understand this concept by reading subsequent sections of this article.

 

Different git tag types

The core types of git tags are annotated and lightweight tags. The difference between the tags results from their creation method and the type of data they hold.

  • Annotated tags are crucial for version releases. You give a message when creating an annotated tag. The resulting tag is a complete git object in the repo, carrying info about the tagger. An annotated tag is both a reference and a git object such as commits, trees, and blobs.
  • Lightweight tags, on the other hand, suit temporary object labels. They are like branch, since it is just a named reference like refs/tags/version123, which points to the commit hash of the commit you are tagging; whereas if it were a branch, it would be refs/heads/version123. The difference is the branch moves forward when you work and commit to it. The tag should always point to the same commit hash.

Now that you have a basic knowledge of git tagging and git checkout tags, let us dive into practical applications of git checkout tags by doing some operations.

Setup Lab Environment

I am creating a new Github repo, git-tags-lesson, to demonstrate the creation, reading, remote pushing, and deletion of the git tags.

git checkout tag PROPERLY [Practical Examples]

 

Next, let us grab the clone URL.

git checkout tag PROPERLY [Practical Examples]

Then open a terminal and clone the remote repo using

Advertisement
git clone <remote repo URL>

git checkout tag PROPERLY [Practical Examples]

Let us build a series of commits before tagging the repo.

Navigate into the cloned repo.

cd git-tags-lesson

Create two text files.

touch file1.txt file2.txt

git status shows we have two untracked files.

git checkout tag PROPERLY [Practical Examples]

git add . and git commit the changes with the message -m "add file1.txt and file2.txt"

git log shows we have two commits in history.

git checkout tag PROPERLY [Practical Examples]

We can see the summarized git version by running

git log --oneline

The 40-character long commit ids are now seven characters

git checkout tag PROPERLY [Practical Examples]

You can still view the full checksum hash with fewer details by running:

git log --pretty=oneline

Let us push the changes to the remote repo before doing git tag and git checkout tag.

Now is the perfect time to create, read, push and delete git tags. Let us get started.

Example-1: Create a lightweight git tag

You can create a lightweight tag by running the following command on your command line:

git tag <tag name>

Proceeding with our project, let us create a lightweight tag by typing:

git tag v1.0

 

Example-2: Create an annotated git tag

When creating an annotated tag, use a -a flagged tag and a commit message.

git tag -a <tag name> -m <message>

For example, we can create a second tag on a third commit by introducing more changes to our repo.

echo git is helpful >> file1.txt

git add ., git commit changes with a message -m "write something in file1" then git push the changes to our remote repo.

Let us recheck our commit history.

git log --oneline

We now have three commits.

git checkout tag PROPERLY [Practical Examples]

Let us tag the third commit:

git tag -a v1.1 -m "tag the third commit"

Additionally, git allows us to create a tag from a post-commit by referencing the id (also known as checksum number) of the commit, as follows:

git tag <tag name> <portion of checksum number>

For example, we can run git tag v1.2 02bed80

 

Example-3: List git tags

There are three ways to list tags.

The first way is typing:

git tag

For example, the command lists our two tags in alphabetical order.

Secondly, we can run:

git log --oneline

The above ways of listing tags are suitable for small projects. For a project with hundreds of tags, you can promptly check tags by running the command with the -l flag and a star, shown here:

git tag -l v1.*0.

Here is a summary (and accompanied results) of the ways to read tags.

git checkout tag PROPERLY [Practical Examples]

You can also git show <tag name> to view the tag's data. For instance, git tag v1.0.

 

Example-4: Perform git checkout tag properly

If you run git checkout <tag name>, you will get warnings (as shown below) with options for conducting git checkout in the recommended way.

git checkout tag PROPERLY [Practical Examples]

Although git documentation does not provide a straightforward way to do a git checkout tag, you can achieve the desired outcome in a simple step. All you do is create a branch then git checkout the new branch, as follows:

git checkout -b <branch name> <tag name>

For instance, we can git checkout tags on our project by running:

git checkout -b git-checkout-tags v1.0

By running the above command, we retain the repository state we had tagged at v1.0.

 

Example-5: Pushing tags to a remote repository

Let us revisit the remote repo we created earlier. Currently, the repository has no tags.

git checkout tag PROPERLY [Practical Examples]
Let us push the two tags we have created to the remote repo.

git push origin <tag name>

For example, we can push the first tag by running:

## push a single tag using
git push origin v1.0

## or 
git push origin --tags

## push all tags at once
git push --tags

Recheck the repo. What do you see? You should see our two tags like this:

git checkout tag PROPERLY [Practical Examples]

Refresh the page if you cannot see the tags. Each of the tags holds a portion of code revealing what was in the working area when the commit was made. You can inspect the contents of each tag by clicking on it.

 

Example-6: Deleting git tags

You want to clear a code release version or no longer want to reference a commit history. You can delete the tags by either running git tag -d <tag name>or git tag --delete <tag name> , as shown below:

git tag -d v1.0 
## or 
git tag --delete v1.0

Running git tag reveals there are no tags. However, the tags are still present in the remote repo.

To delete the tags in the remote repo, run either of these commands in the terminal:

## method 1
git push origin -d v1.0 

## method 2 
git push origin --delete v1.0

## method 3
git push origin: v1.0

You can delete multiple tags at once by combining their names on the delete command

git tag -d v1.0 v1.1

git checkout tag PROPERLY [Practical Examples]

The repo now lacks tags after deleting them from the command line.

 

Summary

You have just learned how to create, read, push to the repo, and delete git tags and git checkout tags. Now that you can manage tags, it would help to learn how to create and handle releases via the command line while practicing the skills you have learned today.

 

Categories GIT

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment