I read a post on dev.to which shows how to create git tags using GUI-based git clients. I think that tags are useful to know even when using the git cli.
Tags are specific points in your code history which are useful to re-visit later e.g
you just released a new version of your app. You can tag the commit as
git tag v1.0. Anytime you want to reproduce bugs encountered on that version, simply do
git checkout v1.0 and investigate.
The tag is linked to the specific commit and not to a branch. When you checkout the tag, git tells you that you are in "detached HEAD" state. Do not worry, all it means is that you need to create a new branch if you want to retain any changes you make after checking out the tag.
Create a new branch exactly at the commit of the tag using
git checkout -b BRANCH_NAME TAG_NAME
You can add more information using
git tag -a TAG_NAME -m 'MESSAGE'
The tag information can be viewed without having to checkout the tag using
git show TAG_NAME.
You do not always have to be at the HEAD or in the tip of the branch to create a tag. If you want to create tag say 5 commits before HEAD, you can use
git log to get the correct commit hash e.g
git log --pretty=oneline -10 which shows the last 10 commits on the current branch.
Then, create tag using
git tag -a TAG_NAME -m 'MESSAGE' COMMIT_HASH
Listing all tags is simply:
Similarly, delete a tag using
git tag -d TAG_NAME
The tag created is not pushed to remote automatically. If you want your tags to be used by other contributors too, you need to push them using
git push origin TAG_NAME
If you are working on a major project and want to show without any doubt that you have worked on the release, you can sign it using your GPG private key as
git tag -s TAG_NAME -m 'MESSAGE'. Anyone who runs
git show TAG_NAME on the tag will also see your public key signature along with the tag information.
They can additionally verify the tag using
git tag -v TAG_NAME. This checks using your public key whether the signature is indeed yours.
Why use tags when you have branches? Because branches can change and tags are linked to a specific commit. Thus, marking releases with a tag will give you the state of code when the particular software release was done.
- Tags are great way to remember specific commits
- It is good to add a message to the tag so that people can easily see why you tagged a particular commit
The Git book chapters: