Skip to content

Instantly share code, notes, and snippets.

@jasonrogena
Last active October 26, 2020 15:40
Show Gist options
  • Save jasonrogena/ee270164bc16172a4718c17414e78166 to your computer and use it in GitHub Desktop.
Save jasonrogena/ee270164bc16172a4718c17414e78166 to your computer and use it in GitHub Desktop.

How to move commits between two repos that don't share a commit history:

  • I have two repos github.com/<my employer>/playbooks and github.com/roots/trellis
  • github.com/<my employer>/playbooks has a directory wordpress/trellis
  • wordpress/trellis in github.com/<my employer>/playbooks contains code from github.com/roots/trellis, has changes made but commited in github.com/<my employer>/playbooks/.git all tracked changes from upstream have been lost (github.com/<my employer>/playbooks/wordpress/trellis/.git was deleted).

We need to merge the latest changes in github.com/roots/trellis with whatever changes we staged for github.com/<my employer>/playbooks/wordpress/trellis

  1. Clone git@github.com:roots/trellis.git
git clone git@github.com:roots/trellis.git
  1. Find commit right before first commit on Ona side (Jan 5th 2017). Appears to be a6f0793579913ee9121ba4be445f430ef266d54c
  2. Reset to that commit
cd trellis
git reset --hard a6f0793579913ee9121ba4be445f430ef266d54c
  1. Create patch files out of the work done in git@github.com:<my employer>/playbooks.git that affect the wordpress/trellis dir.
cd ../playbooks
git log --pretty=format:"%h" --reverse wordpress/trellis | xargs -i git format-patch -1 {} wordpress/trellis > patch-order.out
  1. Fix the paths in the patch files, since the patches will be applied to a repository where technically everyting in wordpress/trellis is in the root dir.
find . -iname "*.patch" -maxdepth 1 -exec sed -i -e 's/wordpress\/trellis\///g' {} \;
  1. Move all the patch files to the newly cloned trellis repo
mkdir ../patches
mv *.patch ../patches/
mv patch-order.out ../patches/
  1. One by one, using the order defined in the patch-order.out file. Apply the files on the git@github.com:roots/trellis.git clone.
cd ../trellis
git am --3way ../patches/<name of patch file>

If conflic occurs while patching, resolve then:

git add <file where conflict was resolved>
git am --resolve
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment