Skip to content

Instantly share code, notes, and snippets.

@rastislavcore
Created May 8, 2026 12:02
Show Gist options
  • Select an option

  • Save rastislavcore/c35d82931c29f3b4a004d04818fb56a8 to your computer and use it in GitHub Desktop.

Select an option

Save rastislavcore/c35d82931c29f3b4a004d04818fb56a8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
set -euo pipefail
latest_branch=$(
git for-each-ref refs/heads \
--sort=-committerdate \
--format='%(refname:short)' \
| grep -E '.+/.+-[0-9]+$' \
| head -n 1 || true
)
if [[ -z "$latest_branch" ]]; then
echo "No matching branch pattern detected."
exit 1
fi
if [[ "$latest_branch" =~ ^(.+/[^/]+-)([0-9]+)$ ]]; then
prefix="${BASH_REMATCH[1]}"
number="${BASH_REMATCH[2]}"
width="${#number}"
next_number=$((10#$number + 1))
next_number_padded="$(printf "%0${width}d" "$next_number")"
new_branch="${prefix}${next_number_padded}"
echo "Latest branch: $latest_branch"
echo "Creating: $new_branch"
git checkout -b "$new_branch"
echo "Done."
else
echo "Detected branch does not match expected pattern."
exit 1
fi

Comments are disabled for this gist.