There are a number of scripts and git aliases that allow you to bump a semver tag in git, but none of them completely suit my needs. Consequently, I wrote my own bash script to do exactly what I need for my own admittedly-simple workflows.
#!/usr/bin/env bash
set -e
USAGE_TEXT="Bump and tag the current repository using semantic versioning (https://semver.org/).
Usage: git-bump [options] [major|minor|patch]
Default: patch
Options:
-n|--no-update Don't update repository before tagging.
-d|--dry-run Show what the next version will be but do not tag and push the next version.
-h|--help Show this message and exit.
"
DRY_RUN=''
NO_UPDATE=''
ACTION='patch'
while [[ "$#" -gt "0" ]]; do
case "$1" in
major|minor|patch)
ACTION="$1"
shift
;;
-n|--no-update)
NO_UPDATE='yes'
shift
;;
-d|--dry-run)
DRY_RUN='yes'
shift
;;
-h|--help)
echo "$USAGE_TEXT"
exit 0
;;
*)
echo "> Unknown option: $1"
echo "$USAGE_TEXT"
exit 1
;;
esac
done
if [[ -z "$NO_UPDATE" ]]; then
echo "> Updating repository..."
git pull --rebase
fi
CURRENT_VERSION=$(git tag --sort=-version:refname --list "v[0-9]*" | head -n 1)
if [[ -z "$CURRENT_VERSION" ]]; then
CURRENT_VERSION='v0.0.0'
fi
echo "> Current version is ${CURRENT_VERSION}"
# strip the leading "v"
CURRENT_VERSION="${CURRENT_VERSION:1}"
# replace period and split into parts
CURRENT_VERSION_PARTS=(${CURRENT_VERSION//./ })
MAJOR_PART=${CURRENT_VERSION_PARTS[0]}
MINOR_PART=${CURRENT_VERSION_PARTS[1]}
PATCH_PART=${CURRENT_VERSION_PARTS[2]}
case "$ACTION" in
major)
MAJOR_PART=$((MAJOR_PART+1))
MINOR_PART='0'
PATCH_PART='0'
;;
minor)
MINOR_PART=$((MINOR_PART+1))
PATCH_PART='0'
;;
*)
PATCH_PART=$((PATCH_PART+1))
;;
esac
NEW_VERSION="v${MAJOR_PART}.${MINOR_PART}.${PATCH_PART}"
echo "> New version is $NEW_VERSION"
if [[ -z "$DRY_RUN" ]]; then
echo "> Tagging repository..."
git tag "$NEW_VERSION"
git push origin "$NEW_VERSION"
fi
This is also available as a Gist.
Hope you find it useful.