How to move commits between two repos that don't share a commit history:
- I have two repos
github.com/<my employer>/playbooks
andgithub.com/roots/trellis
github.com/<my employer>/playbooks
has a directorywordpress/trellis
wordpress/trellis
ingithub.com/<my employer>/playbooks
contains code fromgithub.com/roots/trellis
, has changes made but commited ingithub.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
- Clone
git@github.com:roots/trellis.git
git clone git@github.com:roots/trellis.git
- Find commit right before first commit on Ona side (Jan 5th 2017). Appears to be
a6f0793579913ee9121ba4be445f430ef266d54c
- Reset to that commit
cd trellis
git reset --hard a6f0793579913ee9121ba4be445f430ef266d54c
- Create patch files out of the work done in
git@github.com:<my employer>/playbooks.git
that affect thewordpress/trellis
dir.
cd ../playbooks
git log --pretty=format:"%h" --reverse wordpress/trellis | xargs -i git format-patch -1 {} wordpress/trellis > patch-order.out
- 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' {} \;
- Move all the patch files to the newly cloned trellis repo
mkdir ../patches
mv *.patch ../patches/
mv patch-order.out ../patches/
- One by one, using the order defined in the
patch-order.out
file. Apply the files on thegit@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