Read more

Git: Moving a commit between repositories

Tobias Kraze
January 30, 2012Software engineer at makandra GmbH

If you want to move a complete commit from one repository to another (and you don't want to add it as a remote), you can use these steps:

Create a patch

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

In the source repository, create a patch based on the commit by running

git format-patch SHA1_OF_COMMIT~..SHA1_OF_COMMIT # Note the ~
git format-patch HEAD~ # Shortcut for the latest commit

This will create a .patch file that describes this commit.

Apply the patch

In the target repository, restore the commit from the patch file with

git am -3way path/to.patch

Should this fail, you can fall back to only applying the diff. This way, you will need to create a new commit yourself.

git apply path/to.patch --reject

The parts that could not be applied cleanly end up in .rej files. Take care of those manually.


Also see Git: How to create and apply patches.

Posted by Tobias Kraze to makandra dev (2012-01-30 13:38)