Skip to content

Instantly share code, notes, and snippets.

@dantailby
Last active May 12, 2016 13:02
Show Gist options
  • Save dantailby/9ff47646ea3dd24bb43b to your computer and use it in GitHub Desktop.
Save dantailby/9ff47646ea3dd24bb43b to your computer and use it in GitHub Desktop.
This Gist contains instructions of how to preserve Git history of an existing repository as you convert that repo into a subdirectory of a larger repo.

Preface

@mintuz gist contains instructions of how to preserve history of a subdirectory between repos, for example a template or lib within morph-renderer which you are transferring into morph-modules. However this script doesn't work for a whole repository, such as something already (incorrectly) versioned or a standalone lib.

Based off of article found here: http://www.nomachetejuggling.com/2011/09/12/moving-one-git-repo-into-another-as-subdirectory

Instructions

You have two repos here. morph-modules and my-module. We will be copying 'my-module' and preserving it's history into morph-modules/my-module.`

Part 1

  1. Navigate in to my-module. cd ~/workspace/my-module
  2. Create a new directory within my-module. mkdir temp-module
  3. Move the contents of my-module into this subdir. mv !(temp-module) temp-module
  4. Make a LOCAL commit. DO NOT PUSH TO REMOTE. git commit -am "Prepare to move repo"

Part 2

  1. Navigate to morph-modules. cd ~/workspace/morph-modules
  2. Add my-module as a temporary remote. git remote add temp ~/workspace/my-module
  3. Fetch. git fetch temp.
  4. Merge. This uses your pulls in your local copy and copies the history too. git merge temp/master.
  5. Delete the remote. git remote rm temp.

Troubleshooting

You might need to resolve some conflicts in the normal manner, or follow Adam's instructions on dealing with mergepatches.

#!/bin/sh
OLD_REPO=~/workspace/$1
NEW_REPO=~/workspace/$2
cd $OLD_REPO
mkdir $OLD_REPO
mv !($OLD_REPO) $OLD_REPO
git commit -am "Prepare to move repo"
cd $NEW_REPO
git remote add temp $OLD_REPO
git fetch temp
git merge temp/master
git remote rm temp
echo 'Complete.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment