4

Is there a way to export a sequence of commits into a patch from Git. Say I need to export the last 5 commits from a repository and import them into another repository. How would I go about doing that?

Help with this would be appreciated.

Bhaskar
  • 2,549
  • 1
  • 21
  • 23
Chris Smith
  • 764
  • 1
  • 9
  • 22
  • Does this help? http://stackoverflow.com/questions/616556/how-do-you-squash-commits-into-one-patch-with-git-format-patch – Joseph Sep 12 '12 at 20:17

2 Answers2

7

git format-patch is designed for that purpose:

git format-patch --stdout HEAD~5 > ~/patches

The output is a readable BSD-mailbox-style file that contains patches along with some metadata such as the commit messages. To import the patches into the other repository, use git am:

git am < ~/patches
user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • Hey there! Thanks for the help. Everything works well except when I try to import the patch I get the error "Patch format detection failed.". Any idea why that happens? – Chris Smith Sep 12 '12 at 20:46
  • Can you post the exact `git format-patch` and `git am` commands you are using to export and import the patches? (Edit the question if necessary.) Also, are you sure that the patch file didn't get corrupted on the way, if you are transferring it by email or such? – user4815162342 Sep 12 '12 at 21:07
1

You can select any range you want with format-patch

git format-patch --stdout R1..HEAD > output.patch
Eduardo
  • 22,574
  • 11
  • 76
  • 94