uraza uraza - 11 months ago 87
Git Question

Create branch and push it git alias

How do I make an alias that creates a local branch and pushes it upstream? I've tried

publish = !git checkout -b $1 && git push -u origin $1

I get

Switched to a new branch 'mybranch/test'
error: dst ref refs/heads/mybranch/test receives from more than one src.
error: failed to push some refs to 'ssh://myurl'

Answer Source

Your final command turns out to be git checkout -b mybranch/test && git push -u origin mybranch/test mybranch/test as $1 is substituted with the first parameter and the parameter is also added to the end of your command. Either leave out your last $1, so that you have

publish = !git checkout -b $1 && git push -u origin

or wrap your commandchain in a function like

publish = !publish_new_branch() { [ $# -ne 1 ] && echo 'error: publish needs exactly one argument' >&2 && exit 1; git checkout -b "$1" && git push -u origin "$1"; }; publish_new_branch

I usually prever the latter option as it is more clear what happens.