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 web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
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)