Sharing Git Branches

Say you’re working on a site and want to try something out, then show it to someone else or push it to a test server, all without touching your master/production code on Git. It’s branches you need! As I forgot exactly how to do this the past couple of times, thought it best to write it down. Come on memory.

Create a new local branch:

[bash]git checkout -b new_exciting_thing[/bash]

You are now on a new branch:

[bash]git branch -a[/bash]

See it with a little star next to it. Good. Write some code and commit. Now push the branch to the remote repo (could be called origin, or beanstalk if you named it strangely like me):

[bash]git push -u origin new_exciting_thing[/bash]

Get on your test server that also tracks that remote and pull:

[bash]git pull[/bash]

You should see your new branch on the remote:

[bash]git branch -a

* master
remotes/origin/HEAD -> origin/master
remotes/origin/new_exciting_thing
remotes/origin/master[/bash]

Check it out and you’re done:

[bash]git checkout –track origin/new_exciting_thing[/bash]

Hooray! …or “Boom!” or “Booyah!” if you want to sound like a twat.