Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create branch that is tracking non-existing remote

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.

like image 709
Almad Avatar asked Apr 06 '11 08:04

Almad


2 Answers

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
like image 53
KingCrunch Avatar answered Nov 15 '22 17:11

KingCrunch


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. ;-)

like image 30
Gregory McIntyre Avatar answered Nov 15 '22 15:11

Gregory McIntyre