Skip to content

Instantly share code, notes, and snippets.

@robandpdx
Last active December 20, 2022 08:14
Show Gist options
  • Save robandpdx/c22c34b539c0653fc2036a43c6612c7d to your computer and use it in GitHub Desktop.
Save robandpdx/c22c34b539c0653fc2036a43c6612c7d to your computer and use it in GitHub Desktop.

Migrating git repos without metadata

Metadata, like pull requests and issues, are not part of a git repo. These features are provided by the platform that hosts your shared git repo. There are tools to migrate metadata from BitBucket, GitLab, Azure DevOps, GitHub Enterprise Server, and github.com. If tools don't exist to migrate the metadata from the platform you are using, your only option is to migrate only the git repo. To migrate a git repo without medatadata, follow the instructions below.

Pre-migration

Familiarize yourself with the pre-migration documentation here.
If you are migrating a repo that uses LFS, make sure you have git-lfs installed.

Migration

  1. Clone the repo
  2. Checkout all remote branches locally.
for REF in $(git for-each-ref --format='%(refname)' refs/remotes/origin/ | grep -v main | grep -v master | grep -v HEAD); do
    BRANCH_NAME=${REF#refs/remotes/origin/}
    git branch --track ${BRANCH_NAME} ${REF}
done
  1. If the repo uses LFS, fetch all LFS objects git lfs fetch origin --all
  2. Add new origin git remote add github <new repo url>
  3. Push default branch git push -u github $(git branch --show-current)
  4. Push all other branches
for REF in $(git for-each-ref --format='%(refname)' refs/heads); do
        BRANCH_NAME=${REF#refs/heads/}
        git push -u github ${BRANCH_NAME}
done
  1. Push all tags git push github --tags

Post-migration

Familiarize yourself with the post migration documentation here.

Automation

If you want to script all this, you might consider automating the new repo creation on GitHub using the REST API. https://docs.github.com/en/rest/repos/repos#create-an-organization-repository

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