How to Create a Tag in a GitHub Repository

Avatar

By squashlabs, Last Updated: Oct. 28, 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 Fix Git Error: Pre-Receive Hook Declined

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 Ignore Node Modules Folder Globally

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:

Fixing the Git Error: "Git Not Recognized As A Command"

Learn how to resolve the 'git' is not recognized error in simple steps. Understand the potential reasons for the error, explore solutions to fix it, … read more

How to Perform a Hard Reset of a Single File in Git

Executing a hard reset on a single file in Git is a useful skill for software engineers. This guide provides step-by-step instructions on how to perf… read more

How To Fetch All Git Branches

Keeping your local Git repository up to date is essential for collaboration and ensuring that your code is always in sync with the latest changes. In… read more

How to Delete a Git Branch Locally and Remotely

Deleting a Git branch both locally and remotely is essential for maintaining a clean and organized Git repository. This article provides simple steps… read more

How To Push And Track A Local Branch In Git

Learn how to push a new local branch to a remote Git repository and track it for seamless collaboration. This step-by-step guide will walk you throug… 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 a… read more

How To Uncommit Last Git Commit

Learn how to uncommit your last commit in Git with simple steps and avoid unnecessary changes in your codebase. Find out two methods to uncommit, und… read more

How to Cherry Pick Multiple Commits in Git

Cherry picking multiple commits in Git can be a useful technique when you only want to apply specific changes to your codebase. This article provides… read more

How To Handle Git Refusing To Merge Unrelated Histories On Rebase

Git refusing to merge unrelated histories on rebase can be a frustrating issue to encounter. This article provides possible answers and suggestions t… read more

How to Move Recent Commits to a New Branch with Git

Guide to relocating recent commits to a new branch using Git commands. Learn two methods: using git branch and git cherry-pick commands, or using the… read more