Tags are used to mark a particular commit in your commit history. They are a git ref (reference) to a commit. Much of software project build and distribution is based on tags, e.g., GitHub Releases.
Tags are inexpensive to create, copy, and delete. We will only use lightweight tags.
The following assumes that you just committed and pushed the last commit that is part of v1a
:
Goal | Command | Note |
---|---|---|
Create lightweight tag | git tag v1a |
|
List tags | git tag |
Should see v1a |
Show tags in the log | git log |
Shown in the log as "tag: v1a" |
Push the tag to GitHub | git push origin v1a |
By default, tags are not pushed to GitHub |
Delete local tag | git tag -d v1a |
Verify after by listing the tags |
Delete tag at GitHub | git push origin --delete v1a |
Local delete does not delete at GitHub |
Once you create and push any tags, verify that the tag is there in the repository at GitHub.
Notes:
To check the code for a particular tag, you can check out the repository at that commit:
git checkout v1a
You cannot commit to a tag. So make sure to return to the branch main
:
git checkout main
There is no "rename" for a tag. To rename a tag, you copy it, and delete the original. E.g., to change the tag
prevtag
to v1a
:
git tag prevtag v1a
git tag -d prevtag
git push origin prevtag :old
If you have the GitHub repository cloned in other places, remove the tags deleted at GitHub:
git pull --prune --tags
To add a tag to a previous commit, you need to find the SHA1 commit id. Typically the first 7 characters are unique, so you can use those.
To find the commit id, use the one line git log:
git log --oneline
Find the commit id of the commit you want to tag, e.g., 6536cbc
:
git tag v1a 6536cbc
Make sure to push the commit to GitHub (see above) and verify at GitHub that it is there and tagging the correct commit.