How to create a remote branch in git

Git branching model and capabilities is certainly one of git's powers, but like many things in git I often find my self in a situation where I know what I want to do and I kind of know which git commands I will probably want to use but exactly how to do it sometimes remains a mystery. And like most things in git you can do a task in many different ways.

lets take this simple use case and I will show here my way of doing it:

You have a repository with a master branch and you want to create a new branch based on master, you want to push the new branch to your remote repository and have other people collaborate with it. you want your git push/pull command to be short without having to specify your local branch and remote refs.

 

I find 'git branch -av" to be very helpfull when working with branches, it show you what your repository looks like and which branch you are currently on. I have a shortcut for it that I created with:

git config --global alias.bav "branch -av"

And this is the output of my `git bav` which says I'm on the master branch:

 

* master                5583f06 init
  remotes/origin/master 5583f06 init
 

now create a remote branch called 'exp' based on master:

git push origin master:refs/heads/exp

 

`git bav` will show us that we now have a remote branch called 'exp' but still no local branch to work on:

 

* master                5583f06 init
  remotes/origin/exp    5583f06 init
  remotes/origin/master 5583f06 init
 

You will also see this new branch in your remote repository UI, we use gitorious.

run `git fetch origin` once to make sure everything is up to date.

 

And now create a local branch to track the remote branch exp:

git branch --track exp origin/exp
git checkout exp

This can be combined into one command as:

git checkout --track -b exp origin/exp

 

`git bav` output now shows that we're on the local branch exp:

 

* exp                   5583f06 init
  master                5583f06 init
  remotes/origin/exp    5583f06 init
  remotes/origin/master 5583f06 init
 

Actually thats it, you can now push and pull with simple `git push` and `git pull` commands.

 

a collaborator to your work can now clone the repository, or if they already have a local clone they can just run `git fetch` to synch the local repository with the remote. They will now have to create their local branch to work on with this simple command:

git checkout -b exp origin/exp

which creates a local branch named 'exp' that tracks the remote origin/exp and switches to it. they now can also push and pull with simple `git push` and `git pull` commands.

 

I find it better to name local branches as the remote names, if you wish to use different names for your local branches then some of the commands syntax may be different. I usually don't see a reason for that.

 

(just to mention: I used git version 1.7.1)

 

You can try and check the git_remote_branch tool, I didn't, I usually like to walk the hard way. but I maybe will review it when I have a few minutes.

 

Enjoy.

Thank you for your interest!

We will contact you as soon as possible.

Send us a message

Oops, something went wrong
Please try again or contact us by email at info@tikalk.com