Programblings

Rambling about programming and life as a programmer - by Mathieu Martin

Git remote branches

23rd June 2008

I’m ready to bet that setting up and deleting remote branches is something you do rarely enough that you always find yourself looking up the documentation. Or maybe it’s just me.

Due to its roots, Git supports a wide array of usage scenarios for interacting with remote repositories, and we love it that way. It’s a big factor in the flexibility and power of the tool.

However in simple scenarios, there’s still a bunch of commands you must run to accomplish simple tasks. I believe the commands for the simple scenario can be simpler.

Last January, Carl Mercier created git-remote-branch. I’ve found this script very useful and, with his permission, I’ve decided to keep moving it forward and add a few features to it.

git_remote_branch

The first purpose of git_remote_branch is to encapsulate all the commands that need to be run to interact with remote branches in simple scenarios.

Its second purpose is to be a learning tool. git_remote_branch does two things to help you learn these commands:

  •   It clearly displays the commands it runs on your behalf, in a beautiful shade of red;
  •   It has an ‘explain’ meta-command that will simply display the list of commands instead of running them for you.

Examples

Help

Let’s start with the simplest of all:

$ grb

or

$ grb help
git_remote_branch version 0.2.2

  Usage:

  grb create branch_name [origin_server]

  grb delete branch_name [origin_server]

  grb track branch_name [origin_server]

  If origin_server is not specified, the name ‘origin’ is assumed (git’s default)

  The explain meta-command: you can also prepend any command with the keyword ‘explain’.
  Instead of executing the command, git_remote_branch will simply output the list of commands
  you need to run to accomplish that goal.
  Example:
    grb explain create
    grb explain create my_branch github

  All commands also have aliases:
  create: create, new
  delete: delete, destroy, kill, remove
  track: track, follow, grab, fetch

As you can see, the syntax for all commands is very regular: action, branch_name and optionally, origin_server.

To facilitate learning even more, aliases are also provided. So to take an example,

$ grb track his_branch

and

$ grb fetch his_branch

are perfectly equivalent.

create

Create lets you create a new branch both remotely and locally. Note that it’s not made to share an existing branch (that feature’s coming).

So what it does is to push your current branch as a new remote branch, then create it locally and track it, for easier pulling afterwards.

$ grb create some_branch
git_remote_branch version 0.2.2

git push origin master:refs/heads/some_branch
Total 0 (delta 0), reused 0 (delta 0)
To /path/to/repo/
* [new branch]      master -> some_branch

git fetch origin

git branch −−track some_branch origin/some_branch

git checkout some_branch
Switched to branch “some_branch”

delete

Presenting features with names that are too self-evident is boring. Let’s get to the point, already.

$ grb delete some_branch
git_remote_branch version 0.2.2

git push origin :refs/heads/some_branch
To /path/to/repo/
- [deleted]         some_branch

git checkout master
Switched to branch “master”

git branch -d some_branch

track

Track lets you easily track the changes that are made to an existing remote branch you’re not tracking already. Each time you pull from the remote repository, the local branch will be automatically merged with the remote branch.

$ grb track his_branch
git_remote_branch version 0.2.2

git fetch origin
From /path/to/repo/
* [new branch]      his_branch -> origin/his_branch

git branch −−track his_branch origin/his_branch

explain

Explain will spew out all commands necessary to accomplish one of the previous actions. There are two ways of using it. The simplest will give you dummy commands:

$ grb explain create
git_remote_branch version 0.2.2

List of operations to do to create a new remote branch and track it locally:

git push origin master:refs/heads/branch_to_create
git fetch origin
git branch −−track branch_to_create origin/branch_to_create
git checkout branch_to_create

Or you can have steps that are tailor-made for what you want to accomplish.

$ grb explain create my_branch github_origin
git_remote_branch version 0.2.2

List of operations to do to create a new remote branch and track it locally:

git push github_origin master:refs/heads/my_branch
git fetch github_origin
git branch −−track my_branch github_origin/my_branch
git checkout my_branch

get git_remote_branch

(Hey, this title has a nice ring to it)

sudo gem install webmat-git_remote_branch −−source=http://gems.github.com

Now with rubygems 1.2.0 out (don’t do it with a prior version), you can also add GitHub as a permanent source for your gems:

sudo gem sources -a http://gems.github.com

Now and ever after, you will be able to get anything from GitHub with a simple

sudo gem install webmat-git_remote_branch

If in your eagerness you’ve added github as a source before running

sudo gem update −−system

Please refer to What to do when gems.github.com breaks gems FOREVAR

Look ma, no tests!

Uhhhh, yeah, I know…

This tool is still extremely early in its life and - dare I say it - it’s only hand-tested for now. I’ve been using it personally for a while and it’s working very well for me, if that means anything :-)

So this is still a quick script, only now it has lipstick.

Works on my machine logo

This software should be considered an early version of a pre-alpha. You’ve been warned!

If this makes you queasy, there’s always the ‘explain’ command that can act as your cheat sheet without actually having grb run the commands on your behalf.

For the extra queasy, well you can find the commands very easily without running grb. Just point your favorite editor to lib/git_remote_branch.rb. All commands are there, at the beginning of the file.

For the brave, try it out and tell me what you think. Improvements, bug reports and contributions are all welcome.

And remember, in case of an emergency, you can always refer to my illustrated guide to recovering lost commits with Git ;-)

Here’s what’s to come:

  • lots of tests
  • a ‘remotize’ functionality (for existing local branches)
  • a much better resilience to use in faulty situations (e.g. deleting something that’s not present locally or remotely)
  • the possibility to specify different branch names locally vs remotely
  • slap an open source licence on it all
  • and so much more!

12 Responses to “Git remote branches”

  1. Ryan McGeary Says:

    Mathieu, Cool stuff, but after installing the gem, I get the following error when running grb:

    $ grb -h
    /opt/local/bin/grb:19:in `load’: no such file to load — grb (LoadError)

    I’m not sure what went wrong, but the grb script in the gem’s bin directory was not world readable:

    $ ls -l /opt/local/lib/ruby/gems/1.8/gems/webmat-git_remote_branch-0.2.2/bin
    total 8
    -rwx-wx-wx 1 root admin 446 Jun 23 17:17 grb

    Chmod’ing it to be world readable fixed the problem

    $ sudo chmod 777 /opt/local/lib/ruby/gems/1.8/gems/webmat-git_remote_branch-0.2.2/bin/grb

  2. webmat Says:

    Hmmmm, that’s strange, was that on OS X? I think I installed the freakin’ gem 25 times over here.

    But it’s my first gem, so maybe I made a n00b mistake somewhere along the way :-)

    I’ll check it out.

  3. Labnotes » Rounded Corners - 204 (Git Day) Says:

    […] demystified. If you haven’t figured out Git remote branches yet (raises hand in shame), check out GRB.  A little command line utility for dealing specifically with GRB.  Hopefully this will show up […]

  4. Micha Says:

    yup. for me, grb was not worl-readable, too.
    rubygems 1.2.0 and leopards Ruby.framework
    chmod’ing resolved the problem

  5. Ryan McGeary Says:

    Webmat, Yes, I ran into the problem on OSX 10.5.3 with RubyGems 1.2.0.

  6. webmat Says:

    Confirmed, it’s crapping out on my other machine… I’ll look at the rubygems’ doc to see if I must do something else than registering it in ‘executables’. You’d think that’s enough :-)

    I’ll keep you posted!

  7. Ryan McGeary Says:

    Webmat, I’ve had this issue happen on other github gems. I’m not sure if it’s a github or rubygems 1.2 issue, but I don’t think it’s your fault,

    For example, I had this same permission issue happen to the defunkt-github gem.

  8. webmat Says:

    Thanks for mentioning it Ryan.

    In fact I had a sneaking suspicion that this may be the case. Maybe due to Git itself or to GitHub. Although from what I can see, Git seems to have a correct 755 mode for the file.

    I’ve been extremely busy in the last few days and couldn’t spent any significant amount of time trying to get to the bottom of it. I should have time to nail this down towards the middle of next week.

    I also have the RubyForge project approved so this should simplify the installation greatly by having g_r_b on the default gem server and this may help me avoid the problems with GitHub’s server (if it is due to it).

  9. Brendan Baldwin Says:

    I’m having this problem with github myself. It’s like every bin file gets re-chmod’d from 755 to 733 — perhaps there’s a bug in github’s gem builder? Does anyone know whats up?

  10. webmat Says:

    @Brendan
    Last time I checked, I seemed to have a new problem, in fact. It apparently had something to do with the usernames being prepended to the gem name. The bin file that’s actually generated by rubygems in the public bin directory did

    ...
    gem 'webmat-git_remote_branch', version
    load 'grb'

    The load command failed.
    This week-end I think I’ll just post the gem to Rubyforge. This will make it easier until we figure out what the deal is.

  11. webmat Says:

    By the way, I can’t believe I didn’t suggest this long ago… Until the remote gem install is fixed, just clone the repo and install the gem from the rake task:

    git clone git@github.com:webmat/git_remote_branch.git
    cd git_remote_branch
    rake gem:gem
    sudo rake gem:install

    This gem works perfectly fine, that’s the version I’m using :-)

  12. Brian Says:

    FYI that I was looking for basic information on git (googled “checking out git”), and you came up first! :)

    Hope you are enjoying your summer thus far…

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>