Friday, January 9, 2009

what a git!

so here's the real reason i started it — nothing too exciting, but after a hunch of a discussion it simply had to go somewhere... :)
since i just figured the below out for the second time (after forgetting what the suitable git commands were since the first time a few months ago) i thought i might as well write it down. perhaps it'll come in handy at some point... :)

imagine you're offline, in a train or plane or cafe, and feel like getting some work done. you're starting to make changes and quickly realize you'd rather split things into several commits than just a single big one. however, you're working against a subversion repository and have — since you're offline — no way to commit things back right away or (conveniently) keep the patches separate. well, except perhaps for copying the whole directory for every single one and later merge/commit them one by one. not really worth the trouble... but git to the rescue!

even without a previously cloned svn repos it's quite easy to locally commit away and later replay your changes. here's a quick step-by-step howto:

  • initialize an empty git repos, propbably best done in the top dir of your working tree:
  • $ git init
  • add the current state to it (you might have to be more explicit here):
  • $ git add *
    $ git commit -m 'import'
  • do your work, i.e. change things, locally commit to git:
  • $ vi ... # or emacs if you must ;)
    $ git commit -a
being offline you leave it at that. once you're back online you wanna transfer those changes to svn, or, iow, apply them to a cloned git repository:
  • format/export the patches you've made (the rev specifier ":/import.." translates to "between a commit starting with 'import' and head) to separate files:
    $ git format-patch :/import..
    
    this will output numbered files, one file per patch, like:
    0001-fix-1.patch
    0002-fix-2.patch
    ...
  • remove your temporary git repos (or move it out of the way):
  • $ rm -rf .git
  • if you haven't done so already, set up the cloned repos (extra options might be required here), which allows you to commit back to svn:
  • $ git svn clone svn://svn-url
  • re-apply the patches:
  • $ git am 00*.patch
  • and finally commit back to svn:
  • $ git svn dcommit

No comments:

Post a Comment