Last active
January 16, 2026 14:58
-
-
Save gabrielrbarbosa/fc4cc191d75211cd7fc47eac52fb9e30 to your computer and use it in GitHub Desktop.
Git alias to create or checkout to current hotfix branch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| set -euo pipefail | |
| # Ensure we're inside a git repo | |
| git rev-parse --is-inside-work-tree >/dev/null 2>&1 || { | |
| echo "Not a git repository." | |
| exit 1 | |
| } | |
| # Month mapping (portable) | |
| MONTHS=( | |
| janeiro fevereiro marco abril maio junho | |
| julho agosto setembro outubro novembro dezembro | |
| ) | |
| MONTH_INDEX=$(date +%m) | |
| MONTH="${MONTHS[$((10#$MONTH_INDEX - 1))]}" | |
| YEAR=$(date +%Y) | |
| WEEK=$(date +%V | sed 's/^0//') | |
| BRANCH="hotfix/${YEAR}-${MONTH}-${WEEK}" | |
| CURRENT_BRANCH=$(git branch --show-current) | |
| # 1. Already on hotfix | |
| if [[ "$CURRENT_BRANCH" == "$BRANCH" ]]; then | |
| echo "Already on hotfix branch: $BRANCH" | |
| exit 0 | |
| fi | |
| # Fetch refs | |
| git fetch origin | |
| # 2. Local branch exists | |
| if git show-ref --verify --quiet "refs/heads/$BRANCH"; then | |
| echo "Switching to existing local hotfix: $BRANCH" | |
| git checkout "$BRANCH" | |
| exit 0 | |
| fi | |
| # 3. Remote branch exists | |
| if git ls-remote --exit-code --heads origin "$BRANCH" >/dev/null 2>&1; then | |
| echo "Hotfix exists remotely. Checking out: $BRANCH" | |
| git checkout -b "$BRANCH" "origin/$BRANCH" | |
| exit 0 | |
| fi | |
| # 4. Create new hotfix from master | |
| git checkout master | |
| git pull origin master | |
| git checkout -b "$BRANCH" | |
| echo "Created and switched to hotfix branch: $BRANCH" | |
| # HOW TO INSTALL AND RUN: | |
| # git config --global alias.gotofix '!~/httpdocs/v3/gotofix.sh' | |
| # git gotofix |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
HOW TO INSTALL AND RUN:
git config --global alias.gotofix '!~/httpdocs/v3/gotofix.sh'
git gotofix