Skip to content

Instantly share code, notes, and snippets.

@usmanity
Created May 8, 2023 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save usmanity/a6864ea83f3ec85fdc643d7e323c7804 to your computer and use it in GitHub Desktop.
Save usmanity/a6864ea83f3ec85fdc643d7e323c7804 to your computer and use it in GitHub Desktop.
creates a git branch like: `username/year/branch-name` where branch can be passed a space separated branch name
function branch() {
local branch_name
if [[ $# -eq 0 ]]; then
echo "Usage: branch <name>"
return 1
fi
if [[ $1 =~ [^a-zA-Z0-9._-] ]]; then
echo "Invalid branch name: $1"
return 1
fi
branch_name="$(id -un)/$(date +%Y)/${*:1}"
branch_name=${branch_name// /-} # Replace spaces with dashes
if git rev-parse --verify "$branch_name" >/dev/null 2>&1; then
echo "Branch '$branch_name' already exists"
return 1
fi
git checkout -b "$branch_name"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment