Extract branch to a new repo using git-format-patch

Share on TwitterSave on DeliciousDigg ThisSubmit to reddit

I know this should probably be done using git-filter-branch, but with format-patch, I don’t need to learn anything new. More importantly, filter-branch would give me more to forget :(

Given a git repo with a branch ‘find-ls’ with history like this:

* 10c757c (origin/master, master) handle errors better
| * 073892b (HEAD, find-ls) don't allow new(dist => ...)
| * 2684370 give up on Test::Class
| * fd7f30c constructor_should_have_valid_cpan
| * 67c3d4d initial commit
|/
* 9758db3 add deploy-prefs.pl
* 2c4d459 monitor.pl: show startup message if log shrinks

Create a bunch of patches, like so:

git checkout find-ls
git format-patch 9758db3 # where find-ls branched

If you need to have your new repo have a different structure, you might need to hack the patches. I just used:

perl -pi -E 's|LEADING-DIR/||g' *.patch

Then you can apply the patches:

mv 0*.patch where/I/want/my/new/repo
cd where/I/want/my/new/repo
git am *.patch

One Response

  1. Move this (or re-title) to ‘git tips’

Comments are closed.