Skip to main content

Moving a folder between two git repos

·2 mins

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.

  1. Install git-filter-repo

    pip install git-filter-repo

  2. Create a clone of the source repo

    md sourceclone

    git clone <sourceclone.git> sourceclone

  3. Break the connection to the source repo

    git reomte rm origin

  4. Filter out what you want to keep in the destination repo

    git filter-repo --path <path to folder you want to move> --force

  5. Create the destination folder

    md destination

  6. Create the destination repo and clone it to your local machine

    git clone <desinationrepo.git> destination

    cd destination

  7. Create a remote on the destination which points to the sourceclone

    git remote add sourceclone <path to sourceclone>

  8. Fetch the source

    git fetch sourceclone

  9. Create a branch to track the sourceclone from the remote main branch

    git branch sourceclone remotes/sourceclone/main

  10. Merge the sourceclone main branch into the main branch of the destination

    git merge sourceclone --allow-unrelated-histories

  11. Remove the remote sourceclone branch

    git remote rm sourceclone

  12. Delete the local sourceclone branch

    git branch -d sourceclone

  13. 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