Skip to content

Instantly share code, notes, and snippets.

@amiryal
Last active December 13, 2019 20:09
Show Gist options
  • Save amiryal/00ac8af24d850239c78b to your computer and use it in GitHub Desktop.
Save amiryal/00ac8af24d850239c78b to your computer and use it in GitHub Desktop.
Create branch on origin and track it locally

Script for creating tracking branches on origin

The script attempts to be agnostic to both git version and gitconfig settings.

To mimic the script in a git alias, run the following command:

git config -g alias.mkbranch '!foo() { git fetch origin && git checkout -b "$1" "${2:-origin/master}" && git push -u origin "$1":"$1"; }; foo'

Alternatively, copy or create a symlink from this script to somewhere in your $PATH and give it the name git-mkbranc for the same effect:

ln -s /path/to/script/git-mkbranch.sh ~/bin/git-mkbranch
#!/bin/sh
set -e
if [ -z "$1" ]; then
echo "Usage: $0 <new_name> [<base>]"
echo "Creates branch <new_name> on origin and tracks it locally."
echo "Default <base> starting point is origin/master."
exit 1
fi
git fetch origin
git checkout -b "$1" "${2:-origin/master}"
git push -u origin "$1":"$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment