Skip to content

Instantly share code, notes, and snippets.

@chrisbloom7
Last active October 3, 2015 15:58
Show Gist options
  • Save chrisbloom7/2482194 to your computer and use it in GitHub Desktop.
Save chrisbloom7/2482194 to your computer and use it in GitHub Desktop.
Unix function and alias to create git branches based off arbitrary text (e.g. A ticket number and description)
# Create a git branch based off of an arbitrary string. Call the function from
# the command line inside a git project and pass in the description (quoted or
# not) as the argument. Your description will be converted to slug format and
# used to checkout a new branch. Useful for quickly creating branches named
# after a ticket # and description.
# Example:
$ git-mb 123 Add a new feature \(Don't forget the tests\!\)
Switched to a new branch '123-add-a-new-feature-don-t-forget-the-tests'
-- Created branch 123-add-a-new-feature-don-t-forget-the-tests
# Or quoted:
$ git-mb "123 Add a new feature (Don't forget the tests\!)"
Switched to a new branch '123-add-a-new-feature-don-t-forget-the-tests'
-- Created branch 123-add-a-new-feature-don-t-forget-the-tests
# Or simply:
$ git-mb Add a new feature
Switched to a new branch 'add-a-new-feature'
-- Created branch add-a-new-feature
function git-make-branch ()
{
echo "\$@ = '$@'"
typeset branch
if [[ "$@" != "" ]]; then
branch=$(ruby -e 'puts ARGV.join(" ").strip.gsub(/[\W\s_]+/, " ").downcase.split(" ").join("-")' "$@")
# echo "branch = '$branch'"
if [[ "$branch" = "" ]]; then
echo "-- Could not create branch from supplied arguments"
else
git co -b "$branch"
echo "-- Created branch $branch"
fi
else
echo "-- Could not create branch from supplied arguments"
fi
}
alias git-mb='git-make-branch'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment