Moving a folder between two git repos
I’m now in a new role with our Enterprise Architecture team and needed to move some of the work I maintained in my original team to my new team. This work was a folder inside of one of our repos and I wanted to turn it into it’s own repo. The library git-filter-repo was extremely helpful for this migration. Grégoire Welraeds’ post was also helpful.
Below are the steps I used to move those files while maintaining their history.
Install
git-filter-repopip install git-filter-repoCreate a clone of the source repo
md sourceclonegit clone <sourceclone.git> sourcecloneBreak the connection to the source repo
git reomte rm originFilter out what you want to keep in the destination repo
git filter-repo --path <path to folder you want to move> --forceCreate the destination folder
md destinationCreate the destination repo and clone it to your local machine
git clone <desinationrepo.git> destinationcd destinationCreate a remote on the destination which points to the
sourceclonegit remote add sourceclone <path to sourceclone>Fetch the source
git fetch sourcecloneCreate a branch to track the sourceclone from the remote main branch
git branch sourceclone remotes/sourceclone/mainMerge the sourceclone main branch into the main branch of the destination
git merge sourceclone --allow-unrelated-historiesRemove the remote sourceclone branch
git remote rm sourcecloneDelete the local sourceclone branch
git branch -d sourceclonePush the changes to the destination main branch
git push origin main
Now you have the source repo folder in your new repo. From here you can move files and folders around in the new repo as needed.
If you have questions, connect with me on twitter