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.

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.

A better way to build and deploy Web Apps

  Cloud Dev Environments
  Test/QA enviroments
  Staging

One-click preview environments for each branch of code.

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.

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: