Skip to content

Instantly share code, notes, and snippets.

@tdwesten
Created May 16, 2024 10:29
Show Gist options
  • Save tdwesten/feee1cd9857a33845696c7f3c56b9527 to your computer and use it in GitHub Desktop.
Save tdwesten/feee1cd9857a33845696c7f3c56b9527 to your computer and use it in GitHub Desktop.
Create Git Feature Branch Git CLI
#!/bin/bash
if [ -z "$1" ]; then
echo "Error: Please provide the name of the new branch as the first argument."
exit 1
fi
new_branch_name="feature/$1"
# 1. Check if the main branch exists
if git show-ref --verify --quiet "refs/heads/main"; then
git checkout main --quiet
else
git checkout master --quiet
fi
# 2. Create the new branch
if git checkout -b $new_branch_name --quiet; then
echo -e "\033[1;32mSuccessfully created new branch: \033[1;34m$new_branch_name\033[0m"
else
echo "Error: Failed to create new branch."
exit 1
fi
@tdwesten
Copy link
Author

To make the script callable globally as create_feature_branch, you can follow these steps:

  1. Save the Bash script to a directory that is included in your system's PATH.
  2. Rename the script file to create_feature_branch (without the .sh extension).
  3. Make the script executable using the command chmod +x create_feature_branch.
  4. Refresh your system's PATH to ensure it recognizes the new script.

Once you've completed these steps, you can run the script from any directory by simply typing create_feature_branch in the terminal. The script will then prompt you for the name of the new feature branch and create it accordingly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment