Skip to content

Instantly share code, notes, and snippets.

@timm-oh
Last active July 27, 2023 13:44
Show Gist options
  • Save timm-oh/7cf314af81d279ba0be448d5cc81603a to your computer and use it in GitHub Desktop.
Save timm-oh/7cf314af81d279ba0be448d5cc81603a to your computer and use it in GitHub Desktop.
Creates git branching name according to a specific format
# All other config
# Usage
# $ git:(main) new-task "Setup dockerfile for development"
# $ git:(tim.mccarthy/setup-dockerfile-for-development)
function new-task () {
main_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
username=$(git config user.name | tr '[:blank:]' '.')
branch_detail=$(echo $1 | sed 's/[^[:blank:][:alnum:]-]//g' | tr '[:blank:]' '-')
branch_name=$(echo "$username/$branch_detail" | tr '[:upper:]' '[:lower:]')
git checkout $main_branch
git pull origin $main_branch
git checkout -b "$branch_name"
}
function rebase () {
main_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
current_branch=$(git rev-parse --abbrev-ref HEAD)
if [[ "$main_branch" == "$current_branch" ]]; then
git pull origin $main_branch
echo "Git pulled instead of rebasing"
else
git checkout $main_branch
git pull origin $main_branch
git checkout $current_branch
git rebase $main_branch
fi
}
function clear-merged() {
git branch --merged | egrep -v $(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@') | xargs git branch -d
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment