I'd like to checkout a branch in such way that subsequent git push
will properly push it to origin under same name.
This is easy when remote branch already exists
git checkout -b branch origin/branch
However, I'd like to do that even if origin/branch
is not there yet.
Just create a local branch
git checkout -b branch
The remote repository doesn't know anything about your local branch, so you must push it the first time "by hand"
git push origin branch
Now, if you wants your local repository to let it track your local branch with the remote one
git branch --set-upstream branch origin/branch
There is no easy way, AFAIK.
This is my current stab at it. This is a bash function that sets up a new local branch such that git push
from that branch pushes to a new remote branch with the same name.
function featurebranch() {
if [ "$@" != "" ]; then
git branch "$@" origin/master
git checkout "$@"
git config branch."$@".remote origin
git config branch."$@".merge refs/heads/"$@"
git config branch."$@".rebase true
fi
}
It has to use git config
, because using git push -u
or git branch --track
or git branch --set-upstream-to
commands all involve a remote branch that actually exists.
I'd appreciate it if any git gurus checked it over and pointed out any problems. Ta. ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With