Dealing with git Remote Branches

If you're wondering what branches a remote git repository has, you can use the command git branch -r to show all the remote branches:

origin/HEAD -> origin/master

But I prefer this command: git branch -a that shows all available branches:

* torontomap-dev
  remotes/origin/HEAD -> origin/master

The colourization that you probably have (not shown above) is helpful: green is your current branch (and you'll be able to tell this even without colour because it has a star beside it). White branch names are checked out locally, red ones are only available remotely so far (shown with the leading "remotes/origin" above). Unfortunately it doesn't show you which local branches are associated with which remote branches. The best way to sort that out is git remote show origin .

If you want to get one of those remote branches onto your local machine: git checkout -b torontomap remotes/origin/torontomap . "-b" specifies creating a new local branch with a name, and the last parameter specifies which remote branch to check out. There's probably a shorter method - "origin/torontomap" probably would have worked - but git is far too flexible and I'd already managed to create a new empty branch called "origin/torontomap" that wasn't tied to any remote. You can also call the branch (the local title after the "-b") anything you want ... but giving it the same name as the remote seems the sanest choice in most cases. I can imagine circumstances where giving an alternative name would be helpful, ie. another programmer gave a branch an innocuous name too similar to a branch name you're already using, so you could now call her branch "janes-issue5" to distinguish it from your "issue4" branch.

If you mess up (and I have, several times, in trying to get this right), just do git checkout master (and don't say git checkout origin ... I got the lovely "detached head" error) and then git branch -d offending-branch-name - that will delete the local copy and not affect the remote.

As you may have guessed, to get between branches (once they're on your system) it's just git checkout branchname .

Finally, after you've made changes to the code, do your normal local commit - but think twice before you push. I have muscle memory that says git push origin master is the correct command, but it's not: you will literally push your branch changes to "master" on the remote, probably not what you meant to do! So I'm going to try to retrain myself to just type git push. git will then squawk at you about "simple" vs. "matching" pushes, but at least in this case it explains itself and gives you a nicely detailed opportunity to set things up properly. I'm going with "simple" - it makes more sense to me (this pushes only the current branch, and only to its matching branch on the remote).

To create a new local branch (without a remote), just git checkout -b branch_name . If you want this to be on the remote server, git push -u origin branch_name . I suspect you could make the remote branch_name different, but again, probably not a good idea.