We’re just starting to use GIT at Defensio and although there are plenty of great resources like the Peepcode video and Jean-François Couture’s blog, the learning curve (from SVN) can be a little rough. The problem is that GIT is very powerful, and probably too powerful for a small shop like ours. This unfortunately leads to some complexity that we’d rather not have to deal with, especially since we don’t need all the fancy features.
In an effort to simplify our workflow, we started to build a few scripts that automate complex tasks. My plan is to post them on my blog for your own benefit. If they’re useful for us, they should be useful for other people too.
The first script I’m posting is git-remote-branch. It is meant to simplify the process of creating and deleting remote and local tracking branches. Instead of having to execute multiple commands, easy to remember one-liners are now enough. (hey! we’re lazy!)
Check this out:
This command creates an experimental branch on a remote server (’origin’ by default) from the one you’re currently sitting in, then a local tracking branch. When that’s done, it checks out to the newly created local branch. You can then pull and push as you wish.
This command does the same as the previous one, but creates experimental on ‘origin2′ instead of ‘origin’.
When you’re done dealing with the remote/local branch combo you created, you can delete both in one go:
The script will delete the remote branch, then check out “master” if you’re still sitting in “experimental”. The local “experimental” branch is then deleted.
git-remote-branch can be downloaded here. Make sure to chmod +x it before executing it. For the record, it’s written in Ruby. Let me know if it’s useful for you!
January 27th, 2008 at 5:50 pm
That looks really useful! Deleting remote branches is rather cryptic in git right now, so this will help.
January 27th, 2008 at 7:22 pm
Nice work! It’s true that git out of box might not have all the niceties required for small shops, working with a central server. But git is so powerful it’s easy to script a little something like this!
January 27th, 2008 at 8:13 pm
Geoff, J-F: thanks for the kind words. I hope to write more of those small quick-n-dirty scripts in the future. This one has been of a lot of help so far for me.
February 3rd, 2008 at 8:04 pm
[...] understand git from different perspectives. One last goodie for the week: Carl Mercier released a small script to help work with remote branches. If you want to script common git tasks in ruby, take a look at [...]
February 6th, 2008 at 5:27 pm
Thank you for posting this script. Similar to the form that exists for git-remote-branch-create, does git-remote-branch-delete also have a more general form that can delete a given branch from any given remote Git repo?
February 6th, 2008 at 5:33 pm
I reviewed the script again and found that git-remote-branch-delete does allow you to specify the remote name just as you can with git-remote-branch-create.
February 6th, 2008 at 6:03 pm
Does Git allow you to delete all branches from a remote repo, including the master branch? I ask because I’d like to delete the master branch of a project at repo.or.cz because repo.or.cz allows one to delete neither an entire project nor the Git repo of a project.
February 6th, 2008 at 6:06 pm
By the way, I just verified that Git does not allow one to delete all branches from a *local* repo. Once you’ve committed the first commit to the initial branch (default is ‘master’), it seems that you cannot delete this branch. I suppose this makes sense because it would be very dangerous to allow a user to delete the entire main history of a repo. It follows from this reasoning that Git should also disallow the deletion of all branches in a remote repo, but I haven’t yet verified that this is true.
February 6th, 2008 at 6:07 pm
My guess is that you can’t delete the master branch. Why would you want to do so anyways?
February 6th, 2008 at 8:35 pm
I want to delete the master branch on a remote repo at repo.or.cz because repo.or.cz does not appear to allow one to delete a project or to delete the Git repo that it associates with a project. If I could re-create the master branch, I could get around this shortcoming. Otherwise, I’ll just avoid repo.or.cz in favour of some other Git hosting service or host the repo on my own site, though it wouldn’t be as visible (findable) as on a well-known hosting service.
June 13th, 2008 at 11:50 am
Great script, but I noticed a small bug. If you call it with no arguments you get some errors instead of the excepted usage message.
But I managed to fix it by changing
action = get_action
branch = get_branch
to:
action = get_action if ARGV[0]
branch = get_branch if ARGV[1]
June 23rd, 2008 at 5:16 pm
[...] January, Carl Mercier created git-remote-branch. I’ve found this script very useful and, with his permission, I’ve decided to keep [...]