How do you add tags?

Answered by Jeremy Urbaniak

To add tags in Git, you have two options: lightweight tags and annotated tags. Lightweight tags are simply pointers to specific commits, while annotated tags include additional information such as a tag message and the person who created the tag. Let’s go through adding both types of tags.

1. Adding a Lightweight Tag:
First, create a new directory for your project and navigate into it using the command line or terminal. To initialize a new empty repository, run the following command:
“`
Git init
“`
This sets up the directory as a Git repository.

2. Adding a Lightweight Tag:
To add a lightweight tag, use the `tag` command followed by the tag name and the commit hash you want to tag. For example:
“`
Git tag mytag abcdef123456
“`
Here, “mytag” is the name of the tag, and “abcdef123456” is the commit hash you want to tag.

3. Adding an Annotated Tag:
To add an annotated tag, use the `-a` option with the `tag` command. This opens the default text editor where you can add notes to the tag. Run the following command:
“`
Git tag -a v1.0
“`
This will open the text editor where you can enter your tag message. Once you save and exit the editor, the tag will be created.

4. Viewing Tags:
To view the tags in your repository, you can use the `git tag` command without any arguments. This will list all the tags available.

5. Pushing Tags:
By default, when you push your changes to a remote repository, the tags are not automatically pushed. To push the tags along with your commits, use the `–tags` option while pushing:
“`
Git push –tags
“`
This will push both lightweight and annotated tags to the remote repository.

Adding tags to your Git repository is a great way to mark important milestones or versions in your project’s history. Lightweight tags are simple and straightforward, while annotated tags provide additional information about the tag. Choose the type of tag that suits your project’s needs and start tagging your commits.