Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zubaer-ahammed/17aa03be6cf93a22ff626679717f013c to your computer and use it in GitHub Desktop.
Save zubaer-ahammed/17aa03be6cf93a22ff626679717f013c to your computer and use it in GitHub Desktop.
Create Tag on a Branch (with branch name input, automatic tag name, prompt to create pull request)

Steps to Create the Script

  1. Create the Script File Open your terminal and create a new script file:
nano ~/create_tag.sh
  1. Add the following content to the file:
  2. Save and close the file by pressing Ctrl + O, then Enter, and then Ctrl + X.
  3. Make the Script Executable Run the following command to make the script executable:
chmod +x ~/create_tag.sh
  1. Run the Script Now, you can run the script directly from your terminal:
~/create_tag.sh

The script will:

  1. Prompt you to enter the tag name.
  2. Confirm the tag name.
  3. Checkout to the master branch.
  4. Create the tag with the given name.
  5. Push the tag to the origin.

Optional: Add the Script to Your PATH

If you want to run the script from anywhere without specifying the full path, add it to a directory in your PATH (e.g., /usr/local/bin):

mv ~/create_tag.sh /usr/local/bin/create_tag

Now, you can run the script by typing:

create_tag
#!/bin/zsh
# Define Colors for Styling
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
RED=$(tput setaf 1)
BOLD=$(tput bold)
RESET=$(tput sgr0)
# Eye-catching box with messages
echo "${YELLOW}###############################################${RESET}"
echo "# #"
echo "# ${GREEN}Developed by Md. Zubaer Ahammed${RESET} #"
echo "# Website: ${GREEN}https://zubaer.com${RESET} #"
echo "# Email: ${GREEN}[email protected]${RESET} #"
echo "# #"
echo "${YELLOW}###############################################${RESET}"
# Get the current branch name
current_branch=$(git symbolic-ref --short HEAD)
# Get the latest tag name or set a default message if no tags exist
latest_tag=$(git describe --tags $(git rev-list --tags --max-count=1) 2>/dev/null)
# Function to properly increment the last numeric part of a version (Zsh-compatible)
increment_tag() {
local last_tag="$1"
local prefix=""
# Extract prefix (e.g., 'v' in 'v1.3.0') if present
if [[ "$last_tag" =~ ^[^0-9]+ ]]; then
prefix=$(echo "$last_tag" | grep -o '^[^0-9]*')
last_tag=$(echo "$last_tag" | sed "s/^$prefix//")
fi
# Ensure valid version format
if [[ "$last_tag" =~ ^[0-9]+(\.[0-9]+)*$ ]]; then
IFS='.' read -r -A version_parts <<< "$last_tag" # Zsh uses -A for arrays
((version_parts[-1]++))
echo "$prefix${version_parts[*]}" | sed 's/ /./g'
else
echo "${prefix}1.0.0" # Fallback to default version
fi
}
# Determine suggested tag
if [[ -z "$latest_tag" ]]; then
suggested_tag="v1.0.0"
tag_hint="example: v1.0.0"
else
suggested_tag=$(increment_tag "$latest_tag")
tag_hint="latest: $latest_tag"
fi
# Display current branch and latest tag information
echo ""
echo "${YELLOW}Current Branch:${RESET} ${GREEN}$current_branch${RESET}"
echo "${YELLOW}Current/Latest Tag:${RESET} ${GREEN}$latest_tag${RESET}"
echo ""
# 🚨 Check for uncommitted changes
if [[ -n $(git status --porcelain) ]]; then
echo "${RED}${BOLD}WARNING: You have uncommitted changes!${RESET}"
echo "${RED}Please commit & push or stash your changes before creating a tag.${RESET}"
exit 1
fi
# Prompt for the branch name with default
read "branch_name?Enter the branch name (default: master): "
branch_name=${branch_name:-master}
# PR confirmation flow
echo "${BOLD}${YELLOW}Have you created and merged a pull request to the '${GREEN}$branch_name${YELLOW}' branch from your working branch (current branch is: '${GREEN}$current_branch${YELLOW}')?${RESET}"
read "pr_confirmation?Type 'y' to confirm or 'n' to cancel (default: n): "
pr_confirmation=${pr_confirmation:-n}
if [[ "$pr_confirmation" != "y" ]]; then
read "create_pr_confirmation?Do you want to create PR (pull request) now? (default: y): "
create_pr_confirmation=${create_pr_confirmation:-y}
if [[ "$create_pr_confirmation" == "y" ]]; then
repo_url=$(git config --get remote.origin.url)
# Convert SSH URL to HTTPS
if [[ "$repo_url" == [email protected]:* ]]; then
repo_url=$(echo "$repo_url" | sed -E 's/git@github\.com:(.*)\.git/https:\/\/github.com\/\1/')
elif [[ "$repo_url" == *.git ]]; then
repo_url=$(echo "$repo_url" | sed -E 's/\.git$//')
fi
pull_request_url="${repo_url}/compare/${branch_name}...${current_branch}"
echo "Opening: $pull_request_url"
open "$pull_request_url"
exit 0
else
echo "Operation cancelled."
exit 1
fi
fi
# Tag creation flow
read "tag_name?Enter the tag name (${tag_hint} - suggested[default]: $suggested_tag): "
tag_name=${tag_name:-$suggested_tag}
[[ -z "$tag_name" ]] && { echo "Tag name required. Exiting."; exit 1; }
echo "Using branch: ${GREEN}$branch_name${RESET} and tag: ${GREEN}$tag_name${RESET}"
read "confirmation?Do you want to proceed? (default: y): "
confirmation=${confirmation:-y}
[[ "$confirmation" != "y" ]] && { echo "Operation cancelled."; exit 1 }
# Switch to the specified branch
echo "${YELLOW}Switching to branch ${GREEN}$branch_name${RESET}..."
git checkout "$branch_name" || { echo "Failed to checkout branch $branch_name"; exit 1; }
# Pull the latest changes from the remote repository
echo "${YELLOW}Pulling latest changes for branch ${GREEN}$branch_name${RESET}..."
git pull origin "$branch_name" || { echo "Failed to pull latest changes for branch $branch_name"; exit 1; }
# Create the tag
echo "${YELLOW}Creating tag ${GREEN}$tag_name${RESET}..."
git tag -a "$tag_name" -m "Tag version $tag_name" || { echo "Failed to create tag"; exit 1; }
# Push the tag to origin
echo "${YELLOW}Pushing tag ${GREEN}$tag_name${RESET} to origin..."
git push origin "$tag_name" || { echo "Failed to push tag to origin"; exit 1; }
echo "${GREEN}Success! Tag $tag_name created on $branch_name.${RESET}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment