Last active
October 2, 2024 07:22
-
-
Save tdwesten/feee1cd9857a33845696c7f3c56b9527 to your computer and use it in GitHub Desktop.
Create Git Feature Branch Git CLI
This file contains 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
#!/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 git repository is clean | |
if [ -n "$(git status --porcelain)" ]; then | |
echo "Error: Please commit or stash your changes before creating a new branch." | |
exit 1 | |
fi | |
# 0. Fetch the latest changes | |
git fetch --all --prune --quiet | |
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To make the script callable globally as
create_feature_branch
, you can follow these steps:create_feature_branch
(without the.sh
extension).chmod +x create_feature_branch
.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.