Tuesday, June 2, 2009

Git Cheat Sheet or My Reminder against git headaches

Ok since I'm a git noob and I'm working on the move from JBoss Rails to the new Torquebox. I need to hack the Telco part of Torquebox so that any JRuby app located in Mobicents Sip Servlets on JBoss 5 can setup and handle SIP calls. Since the torquebox repo is git based and I'm usually dealing with svn or cvs, and I messed up quite some times already with git (even after checking out the famous git svn crash course), I'll use this post to be a placeholder for the thing I usually do so that me an git get along very well.

First things first, the repo is already created on git hub so I'll just check it out using :

$ git clone git://github.com/torquebox/torquebox.git

Since Torquebox is using a notion of submodules :

I need to checkout the underlying submodules, this is achieved by doing :

$ cd torquebox
$ git submodule init
$ git submodule update

Since the master branch is locked up, I use for each module the following command to be able to push my changes to the master branch :

$ git remote add central git@github.com:torquebox/torquebox.git
$ cd torquebox-core
$ git remote add central git@github.com:torquebox/torquebox-core.git
# and so on for each submodule

Then I hack away and do my changes in the various torquebox project. When I'm ok with my changes, I use the following to commit :

$ git commit -a

If there is files or directories that needs to be added to the commit, I usually do :

$ git add 'filename'
# example : git add src/main/java/org/torquebox/ruby/enterprise/sip/sip_environment_builder.rb

This will commit things only locally, then I need to push everything to the central repo located on github, this is done by doing :

$ git push central master

Sometimes I get into a no-branch strange state (don't ask me why :-)) and I end up in no branch at all, you can check on which branch you are by doing :

$ git branch -a

The following command can be useful too :

$ git status

so when I'm on no branch, to recover, I use the following commands sequence :

# going back on master branch
$ git checkout master

# checking the lost commits on the no branch
$ git fsck --lost-found

# the previous command will out put something like
# dangling tree a628672f320e4e311a59ce4bcd879125500141ff
# dangling commit 69ade48b3183ffc7a4e09cf87a1d0e5cedaa39bc
# to merge the last commit into the master branch I just switched to
$ git merge 69ade48b3183ffc7a4e09cf87a1d0e5cedaa39bc

#then I can push to github again
$ git push central master.

I'll update this blog post once in a while on my path to non newbiness to git.
Please comment to make me understand things a bit further or advice me on some good eclipse plugin that would avoid me such pain...

Back to git now

Update : Ranga gave a link with a few more tips http://sipx-wiki.calivia.com/index.php/Mirroring_sipXecs_subversion_repository_with_git#Making_and_committing_changes

Update 2 : very nice and thorough visual tutorial http://www.ralfebert.de/blog/tools/visual_git_tutorial_1/


  1. so, mobicents jain slee, mms, mss, jsr309 projects will get git?

    I'd enjoy a one way git repo where 'git svn' is used to synchronize mobicents svn repo into a git 'read only' repo.

  2. @Anonymous: They are already on git and exactly as you wish ;-)
    See http://github.com/deruelle/mobicents/tree/master

  3. Thanks. You nailed it.

    I thought github had limited support for git-svn. Cloning: Yes. Rebase: No. A policy based decision that would encourage projects into git and away from their old ways. Do I have that right? Its evident that recent mobicents commits are finding their way here. Are you running synchronization manually?


  4. @Anonymous: yeah we do only cloning at the moment. I created a specific branch under http://github.com/deruelle/mobicents/tree/mirror (granted that was not the best name to pick up) to try to work under that branch but didn't have the time to experiment...
    I set up a cron job and a scrip to automatically invoke the git svn every 15 minutes, but for some reason I can get it to update the git repo, if I launch the script manually on the command line it works, I'm still trying to figure what's wrong with my cron job (it runs but don't commit, wonder if the permissions are ok)