How to Create a Tag in a GitHub Repository

Avatar

By squashlabs, Last Updated: October 27, 2023

How to Create a Tag in a GitHub Repository

Creating tags in a GitHub repository allows you to mark specific points in your project’s history as important milestones, releases, or versions. Tags provide a convenient way to reference and retrieve specific versions of your code. In this guide, we will walk you through the steps to create a tag in a GitHub repository.

Step 1: Clone the Repository

First, you need to clone the GitHub repository to your local machine. To do this, open your terminal or command prompt and navigate to the directory where you want to store the repository. Then run the following command:

git clone https://github.com/username/repository.git

Replace “username” with your GitHub username and “repository” with the name of the repository you want to clone.

Related Article: How to Delete a Remote Tag in Git

Step 2: Checkout the Desired Commit

Once the repository is cloned, navigate into the repository’s directory using the cd command:

cd repository

Next, you need to find the commit that you want to tag. You can use the git log command to view the commit history:

git log

The output will display a list of commits with their corresponding commit hashes. Identify the commit that you want to tag and copy its commit hash.

Step 3: Create the Tag

To create a tag, use the git tag command followed by the desired tag name and the commit hash:

git tag -a tag_name commit_hash -m "Tag message"

Replace “tag_name” with the name you want to give to the tag, “commit_hash” with the commit hash you copied earlier, and “Tag message” with a descriptive message for the tag. The -a option creates an annotated tag with additional information such as the author and date.

Step 4: Push the Tag to GitHub

After creating the tag, you need to push it to the GitHub repository using the git push command:

git push origin tag_name

Replace “tag_name” with the name of the tag you created. The origin parameter specifies the remote repository to push the tag to.

Related Article: How to Git Pull from a Specific Branch

Step 5: Verify the Tag

To verify that the tag has been successfully created and pushed to the GitHub repository, you can visit the “Tags” section of your repository on the GitHub website. The tag should be listed there along with its associated commit.

Best Practices

– Use semantic versioning for your tags to clearly communicate the significance of each release or version.
– Consider using lightweight tags for simple markers that don’t require additional information.
– Annotate your tags with informative messages to provide context and details about the tagged commit.

Alternative Method: Creating a Tag on GitHub Website

Alternatively, you can create a tag directly on the GitHub website without using the command line. Here’s how:

1. Navigate to the repository on GitHub.
2. Click on the “Releases” tab.
3. Click on the “Create a new release” button.
4. Fill in the “Tag version” field with the desired tag name.
5. Optionally, provide a release title and description.
6. Choose the target branch or commit for the tag.
7. Click on the “Publish release” button to create the tag.

This method is convenient for creating tags without having to clone the repository or use the command line.

More Articles from the Git Tutorial: From Basics to Advanced Concepts series:

How to Undo/Revert a Git Commit Before Push

When working with Git, it's important to know how to undo a commit before pushing it to the repository. This article provides a simple guide on removing a Git commit... read more

How to Login to a Git Remote Repository

Logging in to Git is an essential skill for any software developer. This article provides a step-by-step guide on how to navigate the login process, from installing Git... read more

How to Download a Single Folder from a Github Repo

Downloading a single folder from a GitHub repository using Git can be done in two ways: using the GitHub website or using the Git command-line tool. Additionally, there... read more

How to Clone a Git Repository Into a Specific Directory

Cloning a Git repository into a specific directory is a fundamental skill for software developers. This step-by-step guide provides two methods for accomplishing this... read more

How to Revert a Pushed Merge Commit in Git

Reverting a pushed merge commit in Git can be a daunting task, but with the right approach, it can be done efficiently. In this article, we provide a step-by-step guide... read more

How To Find The Original URL of a Local Git Repository

In this article, we will guide you on how to find the original URL of a local Git repository. By using the 'git show origin' command or checking the Git Config file, you... read more