I've been learning git lately. Here are a few tips for sharing branches I've collected during the past few weeks.

Create a branch

In git, branches are stored on your local machine. Even the commonly named "master", is just a branch on your local machine. Master is simply set up to track a branch on a remote machine by default. To create a new local branch, you can do one of two things. Create the branch and then check it out, or do a checkout with a branch switch.

  1.  
  2. # using the branch tag
  3. $ git branch newbranch
  4. $ git checkout newbranch
  5. # or just do it with checkout first
  6. $ git checkout -b newbranch
  7. # you can look at your local branches
  8. $ git branch
  9.  

Pushing a branch to the server

This is only applicable for using git with a central server to track changes. You can of course, use the same method to push a branch to another machine that isn't being used as a central server (like another development machine) but I haven't done that as much.

  1.  
  2. $ git push origin newbranch
  3. # You can look at the new branch on the server
  4. $ git branch -r
  5.  

Checkout a remote branch

If someone else has pushed a branch to the server, you can check it out on your machine.

  1.  
  2. $ git checkout -b newbranch origin/newbranch
  3.  

NOTE There is a --track switch on the checkout command you might need to add. For me, it has been doing that by default though.

NOTE After you push a branch to a server, your local branch isn't set up to track changes on that branch. There may be another way to accomplish getting that set up, but I just check out the branch again.

  1.  
  2. $ git push origin newbranch
  3. $ git checkout -b newbranch_renamed origin/newbranch
  4.  

You could of course, delete "newbranch" and then re-checkout the branch to the same name. It's important to realize that the names don't matter really. Newbranch can be named differently on the local and remote machine.

Deleting branches

To delete a branch, just use the -d switch on branch.

  1.  
  2. $ git checkout master # or another branch
  3. # do your merging before you do the delete
  4. $ git branch -d newbranch
  5. # you can delete the remote branch after you have deleted the local one like this:
  6. # the syntax is slightly confusing, but it works
  7. $ git push origin :newbranch
  8.