Skip to content

Instantly share code, notes, and snippets.

@wdhowe
Created October 24, 2024 13:20
Show Gist options
  • Save wdhowe/7ffc4ae270343e50c460b38421d3e1e9 to your computer and use it in GitHub Desktop.
Save wdhowe/7ffc4ae270343e50c460b38421d3e1e9 to your computer and use it in GitHub Desktop.
Git: Move files and history from oldrepo to newrepo

Git: Move files and history from oldrepo to newrepo

Example commands show moving the "data" directory from oldrepo to newrepo and includes all history.

oldrepo (the source)

  • Clone a fresh copy of oldrepo.

    git clone [email protected]:USERHERE/oldrepo.git
    cd oldrepo
  • Switch to the branch you want the code/data from.

    git checkout main
  • Optional: git-filter-repo if you want to prune history to match certain dirs/files.

    # Example (multiple "--path file" or "--path dir" args may be passed, space separated)
    git filter-repo --path data

newrepo (the destination)

  • Clone a fresh copy of newrepo.

    git clone [email protected]:USERHERE/newrepo.git
    cd newrepo
  • Checkout the branch on newrepo that you want to merge oldrepo into.

    git checkout develop
  • Add a new remote that tracks the local oldrepo.

    git remote add local-oldrepo ../oldrepo/
  • Fetch branches from the local oldrepo 'remote':

    git fetch local-oldrepo
  • Create a new temporary branch on newrepo that tracks the local 'remotes/local-oldrepo/branch' as an upstream.

    git branch develop-tmp remotes/local-oldrepo/main
  • Merge the newly created temporary branch into the currently checked out branch on newrepo. (In this example, develop is still checked out on newrepo.)

    git merge develop-tmp --allow-unrelated-histories
    • You may have to deal with merge conflicts for commonly named files.
  • Push your branch to the upstream git server (once satisfied with the results)

    git push --set-upstream origin develop

Cleanup

Remove newrepo's remote reference to the local oldrepo and the temporary branch that tracked it.

git remote rm local-oldrepo
git branch -d develop-tmp

Remove oldrepo's local copy.

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