Skip to content

Instantly share code, notes, and snippets.

@heimdallrj
Last active July 30, 2021 08:15
Show Gist options
  • Save heimdallrj/6af79d2c5f5e279e0111504807a4701b to your computer and use it in GitHub Desktop.
Save heimdallrj/6af79d2c5f5e279e0111504807a4701b to your computer and use it in GitHub Desktop.

Move directory from one repository to another but still preserve the history

Source Repository

git clone [email protected]:<user|org>/<src-repo>.git

cd <src-repo>
git remote rm origin
git filter-branch --subdirectory-filter <target-dir> -- -- all

mkdir <new-dir>
git mv <target-files-and-dir> <new-dir>/

git add -A
git commit -m "Isolate target directories and files"

Destination Repository

cd ..
git clone [email protected]:<user|org>/<destination-repo>.git
cd <destination-repo>

git remote add <src-repo> ../<src-repo>/

git fetch <src-repo>
git branch <src-repo> remotes/<src-repo>/master

git merge <src-repo> --allow-unrelated-histories

git remote rm <src-repo>
git branch -d <src-repo>

git push origin <master|new-branch>

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment