Skip to content

Instantly share code, notes, and snippets.

@chris-redbeed
Created December 12, 2022 08:26
Show Gist options
  • Save chris-redbeed/e0b56b2a2ce3a80f8c4d9ef5199f60c4 to your computer and use it in GitHub Desktop.
Save chris-redbeed/e0b56b2a2ce3a80f8c4d9ef5199f60c4 to your computer and use it in GitHub Desktop.
Create Git Branch based on Master, after reset.
#!/bin/bash
TXT_GREEN='\033[0;32m'
TXT_RED='\033[0;31m'
TXT_NC='\033[0m'
# Ask for continue
echo "This will reset all your local changes."
read -p "Are you sure you want to continue? (y/n) [y]" answer
answer=${answer:-y}
# Stop if no
if [ "$answer" != "y" ]; then
printf "${TXT_RED}Aborted! ${TXT_NC}\n"
exit 0
fi
# Reset current branch to origin/master
git reset --hard
echo "-> Reset current branch"
# Remove all untracked files
git clean -fd
echo "-> Removed all untracked files"
# Checkout master
git checkout master
echo "-> Checked out master"
# Pull latest changes
git pull
echo "-> Pulled latest changes"
# Create new branch type (feature, bugfix, hotfix, others)
read -p "Enter branch type (feature, bugfix, hotfix, others) [feature]: " branch_type
branch_type=${branch_type:-feature}
# Get Ticket ID
read -p "Enter JIRA Ticket ID (DCO..): " ticket_id
# Get Branch title
read -p "Enter branch title/short description: " branch_title
ticket_id=$(echo "$ticket_id" | iconv -t ascii//TRANSLIT | sed -E 's/[~\^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+\|-+$//g' | sed -E 's/^-+//g' | sed -E 's/-+$//g')
branch_title=$(echo "$branch_title" | iconv -t ascii//TRANSLIT | sed -E 's/[~\^]+//g' | sed -E 's/[^a-zA-Z0-9]+/-/g' | sed -E 's/^-+\|-+$//g' | sed -E 's/^-+//g' | sed -E 's/-+$//g' | tr A-Z a-z)
# Ask if branch name is correct
echo "Branch name: $branch_type/$ticket_id-$branch_title"
read -p "Is this correct? (y/n) [y]" answer
answer=${answer:-y}
# Stop if no
if [ "$answer" != "y" ]; then
printf "${TXT_RED}Aborted! ${TXT_NC}\n"
exit 0
fi
# Create new branch
git checkout -b $branch_type/$branch_name
printf "${TXT_GREEN}-> Created new branch $branch_type/$ticket_id-$branch_title ${TXT_NC}\n"
echo "Happy working!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment