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-repo
pip install git-filter-repo
Create a clone of the source repo
md sourceclone
git clone <sourceclone.git> sourceclone
Break the connection to the source repo
git reomte rm origin
Filter out what you want to keep in the destination repo
git filter-repo --path <path to folder you want to move> --force
Create the destination folder
md destination
Create the destination repo and clone it to your local machine
git clone <desinationrepo.git> destination
cd destination
Create a remote on the destination which points to the
sourceclone
git remote add sourceclone <path to sourceclone>
Fetch the source
git fetch sourceclone
Create a branch to track the sourceclone from the remote main branch
git branch sourceclone remotes/sourceclone/main
Merge the sourceclone main branch into the main branch of the destination
git merge sourceclone --allow-unrelated-histories
Remove the remote sourceclone branch
git remote rm sourceclone
Delete the local sourceclone branch
git branch -d sourceclone
Push 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