Git: Easy Script to create/update tag

Thân Đặng
2 min readJul 26, 2020

For those who works with CI, where we use tags for build/deploy. We sometimes come to crazy result that the output result which the issues that we were sure that it absolutely 100% has fixed but now it happens again. Especially there might some argues with QA and that bring to us some stresses. We come to check again from the commit, where it uses to build, and then we realise the tag pointed to wrong commit. Why? What happen?

There are two possibilities:

  • Tag name existed: in remote and we try to push same tag without delete old one (where it points to old commit)
  • Network issue: this issue usually comes from Source tree where we create a tag, and then submit, close window before the tag creation success.

In order to avoid it, we need to do following steps:

  1. Make sure local is up to date with remote:
git remote update

2. Make sure we’re on right branch

git checkout <branch name>

3. Check if tag is existed.

git tag -l <tag name>

4. If tag existed -> Deleted it

git tag -d <tag name>
git push origin :<tag name>

5. Create new tag anyway:

git tag -a <tag name> <commit id> -m <message>
git push origin --tags

You could find the full script here:

Cheers.

--

--