You can create tags in git by using the git tag command. You can use -a to specify the tag name, -m argument to specify a message. The tag will represent all the commits till the commit where this tag is applied.
With tags, you can perform operations such as a push or pull on the branch with the commits only till that come under this tag. It also helps in versioning the branch and helps maintaining the code.
Let us see a practical step-by-step example on a real machine with a repository you can access. The repository is present at https://github.com/codeversionmaster/repoone.
This is the initial setup.
~/repoone$ git log --oneline 378e483 (HEAD -> main) Sixth line in newfile 0516dab This is new file 4632c0d Adding empty directory 908d6ac Third commit in main 42296d0 Second commit in main 453424e This is first commit 2673111 Initial commit
Apply the tag using the git tag command. Use -a to specify the version as v1.0 and -m for the message. In this case, the message is “This is tag v1.0”.
~/repoone$ git tag -a v1.0 -m "This is tag v1.0"
When you display the commits using git log –oneline, you can see the tag version displayed at the topmost commit. Note: You can apply a tag even at a specific commit using an alternative of this command. Here, we are discussing the most common use.
~/repoone$ git log --oneline 378e483 (HEAD -> main, tag: v1.0) Sixth line in newfile 0516dab This is new file 4632c0d Adding empty directory 908d6ac Third commit in main 42296d0 Second commit in main 453424e This is first commit 2673111 Initial commit
git describe can be used to describe the current tag applied as shown.
~/repoone$ git describe --tag v1.0
git tag -n is used to display all the tags available and their descriptions.
~/repoone$ git tag -n v1.0 This is tag v1.0
As seen in the output of git remote -v, the remote repository is origin.
~/repoone$ git remote -v origin firstname.lastname@example.org:codeversionmaster/repoone.git (fetch) origin email@example.com:codeversionmaster/repoone.git (push) repotwo firstname.lastname@example.org:codeversionmaster/repotwo.git (fetch) repotwo email@example.com:codeversionmaster/repotwo.git (push)
We can push the tag from local to remote using the git push command with tags/ instead of the branch name.
Now, the branch, as well as the tag, is pushed. Even if there are changes in the branch, all these commits tagged by the v1.0 tag will be available for download or other operations by the tag name.
~/repoone$ git push origin tags/v1.0 Enumerating objects: 8, done. Counting objects: 100% (8/8), done. Compressing objects: 100% (5/5), done. Writing objects: 100% (7/7), 833 bytes | 833.00 KiB/s, done. Total 7 (delta 3), reused 3 (delta 2), pack-reused 0 remote: Resolving deltas: 100% (3/3), completed with 1 local object. To github.com:codeversionmaster/repoone.git * [new tag] v1.0 -> v1.0
Viewing the tag in online GitHub repository link
Below is how the number of tags displays in the online GitHub repository link – https://github.com/codeversionmaster/repoone.
You can click on the tag label shown in the red box in the above image to display the list of all the tags. The screen would look as below.
Now, you can click further on any tag to display the details of that specific tag. Now, this tag represents all the commits till which we tagged them. Although the branch may have several commits, you can download the branch with only these commits using a specific tag name.