Stop looking up the last Git tag
I want to tag a release. First, I have to look up the last version. Was it 1.4.2 or 1.4.3? Then I copy the number, change one part, and hope I did not mistype it.
It is a tiny chore, but it comes up every time. I already know what I want: the next patch, minor, or major version. Git already has the tags. So I added three aliases:
git new-patch
git new-minor
git new-major
Pick one. If the highest matching tag is 1.4.2, each command produces:
| Command | New tag |
|---|---|
git new-patch | 1.4.3 |
git new-minor | 1.5.0 |
git new-major | 2.0.0 |
Those are three alternatives starting from the same version. Running them one after another would keep advancing the version.
Install once
Paste this into a terminal running Bash or Zsh. It adds four entries to your global Git configuration: one helper and three short aliases. They are then available in all your repositories.
git config --global alias.semver-tag \
'!f() {
level="$1"
shift
latest=$(
git tag --list --sort=-version:refname |
grep -E "^v?[0-9]+\.[0-9]+\.[0-9]+$" |
head -n 1
)
if [ -n "$latest" ]; then
prefix=""
case "$latest" in
v*) prefix=v ;;
esac
version=${latest#v}
major=${version%%.*}
rest=${version#*.}
minor=${rest%%.*}
patch=${rest#*.}
else
prefix=""
major=0
minor=0
patch=0
fi
case "$level" in
major)
major=$((major + 1))
minor=0
patch=0
;;
minor)
minor=$((minor + 1))
patch=0
;;
patch)
patch=$((patch + 1))
;;
*)
echo "usage: git semver-tag {major|minor|patch} [git tag options]" >&2
return 2
;;
esac
next="${prefix}${major}.${minor}.${patch}"
git tag "$@" "$next" && echo "Created tag $next"
}; f'
git config --global alias.new-patch 'semver-tag patch'
git config --global alias.new-minor 'semver-tag minor'
git config --global alias.new-major 'semver-tag major'
What the helper does
semver-tag lists local tags using Git’s version sorting, highest first. That puts 1.10.0 ahead of 1.9.0. It keeps tags shaped like 1.2.3 or v1.2.3, takes the first match, and increments the part you asked for. A minor bump resets the patch; a major bump resets both minor and patch.
If the selected tag starts with v, the new one does too. If there are no matching tags, it starts from 0.0.0: the first patch is 0.0.1, the first minor is 0.1.0, and the first major is 1.0.0.
The !f() { … }; f wrapper makes this a shell command alias. The three shorter aliases call that helper with the bump level already filled in. git new-patch is just git semver-tag patch.
Use it when the commit is ready
The new tag points at HEAD, your current commit. Commit the changes you want to release first. To make an annotated tag, pass the usual options through:
git new-patch -a -m "Fix the printer timeout"
The helper prints the tag it created. Pushing that tag is a separate step. For example, if it prints Created tag 1.4.3:
git push origin 1.4.3
To push all local tags to origin at once, you can also use:
git push origin --tags
This includes any other local tags you have not pushed yet, not just the tag you created with the alias.
A few boundaries
- It uses local tags. If releases happen elsewhere, run
git fetch --tagsfirst. It considers tags across the repository, including other branches, and chooses by version rather than creation date. - Keep the naming consistent. Use either
v1.2.3or1.2.3throughout a repository. Sorting includes the prefix, so mixing the two can selectv1.0.0ahead of2.0.0. - It handles simple release numbers. Tags such as
1.2.3-rc.1,1.2.3+build.1, andrelease-1.2.3are ignored. Use ordinary numeric components without leading zeroes. - You choose the bump. The alias creates a tag; it does not inspect commits, change package versions, generate a changelog, or push anything.
Now, when I want the next patch, I can type git new-patch and let Git do the counting.