Skip to content

Instantly share code, notes, and snippets.

@jiroshimaya
Last active January 16, 2026 19:03
Show Gist options
  • Select an option

  • Save jiroshimaya/5f4524ca296357e1c5347f1674217529 to your computer and use it in GitHub Desktop.

Select an option

Save jiroshimaya/5f4524ca296357e1c5347f1674217529 to your computer and use it in GitHub Desktop.
A Bash script to simplify creating and incrementing Git tags with semantic versioning support.

update_version.sh

このスクリプトは、プロジェクトのバージョンを更新します。これはgitタグを参照し、指定されたバージョンに更新するか、現在のタグに基づいてインクリメントします。 'v'で始まるセマンティックバージョニングをサポートしています。 -vが指定された場合、タグをローカルで作成し、リモートにプッシュします。-rが指定された場合、タグがローカルまたはリモートに既に存在する場合は削除して再作成します。 -vが指定されておらず、-i(patch/minor/major)が指定された場合、最新のタグに基づいて自動インクリメントされたタグを作成し、リモートにプッシュします。 -vも-iも指定されておらず、-rのみが指定された場合、最新のタグを現在のコミットで再作成します。 スクリプトは、実際にタグ付けを行わずに新しいバージョンを表示するドライラン(-d)を実行できます。さらに、-nオプションを使用すると、タグが作成されてもリモートにプッシュされるのを防ぎます。

Usage

./update_version.sh  [-v <version>] [-i <increment_type>] [-r] [-n] [-d] 

すべての引数はオプショナルです。

  • -v, --version: バージョンを指定します
  • -i, --increment: バージョンのインクリメントタイプを指定します(major, minor, patch)
  • -r, --recreate: 既に存在する場合、タグを削除して再作成します
  • -n, --nopush: タグが作成されてもリモートにプッシュしません
  • -d, --dryrun: タグ付けやプッシュを行わずにドライランを実行します

Examples

./update_version.sh -v v2.0.0
./update_version.sh -i patch -n
./update_version.sh -r -d

update_version.sh

This script updates the project version, which refers to the git tag, to a specified version or increments it based on the current tag. It supports semantic versioning, starting with 'v'. If -v is specified, create the tag locally and push it to the remote. If -r is specified, delete and recreate the tag if it already exists locally or remotely. If -v is not specified and an -i (patch/minor/major) is specified, create an auto-incremented tag based on the latest tag and push it to the remote. If neither -v nor -i is specified and only -r is specified, recreate the latest tag on the current commit. The script can perform a dry run (-d) to display the new version without actually tagging. Additionally, using the -n option prevents the tag from being pushed to the remote even if it is created.

Usage

./update_version.sh  [-v <version>] [-i <increment_type>] [-r] [-n] [-d] 
  • -v, --version: Specify a version (optional)
  • -i, --increment: Specify the type of version increment (major, minor, patch) (optional)
  • -r, --recreate: Recreate the tag if it already exists by deleting and retagging (optional)
  • -n, --nopush: Do not push the tag to the remote even if it is created (optional)
  • -d, --dryrun: Perform a dry run without tagging or pushing (optional)

Examples

./update_version.sh -v v2.0.0
./update_version.sh -i patch -n
./update_version.sh -r -d
#!/bin/bash
# Initialize variables
version=""
increment=""
dryrun=false
recreate=false
nopush=false
# Parse arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-v|--version) version="$2"; shift ;;
-i|--increment) increment="$2"; shift ;;
-d|--dryrun) dryrun=true ;;
-r|--recreate) recreate=true ;;
-n|--nopush) nopush=true ;;
*) echo "Unknown parameter passed: $1"; exit 1 ;;
esac
shift
done
# Validate increment
if [[ "$increment" && "$increment" != "major" && "$increment" != "minor" && "$increment" != "patch" ]]; then
echo "Invalid version increment: $increment"
exit 1
fi
# Output the new version
if [[ -n "$version" ]]; then
new_version=$version
else
# Get the current version if not provided
current_version=$(git ls-remote --tags origin | awk -F'/' '{print $3}' | grep '^v' | sort -V | tail -n1)
# Remove the leading 'v'
current_version=${current_version#v}
# Split the version number
IFS='.' read -r -a version_parts <<< "$current_version"
# Update the version based on the increment
case "$increment" in
major)
((version_parts[0]++))
version_parts[1]=0
version_parts[2]=0
;;
minor)
((version_parts[1]++))
version_parts[2]=0
;;
patch)
((version_parts[2]++))
;;
esac
new_version="v${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"
fi
echo $new_version
# 新しいバージョンのタグを作成
create_tag() {
if [ "$recreate" = true ]; then
if git rev-parse "$new_version" >/dev/null 2>&1; then
git tag -d "$new_version"
fi
fi
git tag "$new_version"
}
# 新しいバージョンのタグをプッシュ
push_tag() {
if [ "$recreate" = true ]; then
if git ls-remote --tags origin | grep -q "refs/tags/$new_version"; then
git push --delete origin "$new_version"
fi
fi
git push origin "$new_version"
}
if [ "$dryrun" = false ]; then
create_tag
if [ "$nopush" = false ]; then
push_tag
fi
else
echo "Dry run enabled, not tagging or pushing."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment