Search code examples
gitgit-remote

How do I move my remote git repo to another remote git repo?


I want to move my remote git repository and all its branches to a new remote repository.

old remote = git@github.com:thunderrabbit/thunderrabbit.github.com.git

new remote = git@newhub.example.net:tr/tr.newrepo.git


Solution

  • So what none of these other answers explains too well is that if you want to move all of your remote repository's branches to a new remote using Git's push mechanism, then you need local branch versions of each of your remote's branches.

    You can usegit branch to create local branches. That will create a branch reference under your .git/refs/heads/ directory, where all of your local branch references are stored.

    Then you can use git push with the --all and --tags option flags:

    git push <new-remote> --all  # Push all branches under .git/refs/heads
    git push <new-remote> --tags # Push all tags under .git/refs/tags
    

    Note that --all and --tags can't be used together, so that's why you have to push twice.

    Documentation

    Here's the relevant git push documentation:

    --all
    

    Instead of naming each ref to push, specifies that all refs under refs/heads/ be pushed.

    --tags
    

    All refs under refs/tags are pushed, in addition to refspecs explicitly listed on the command line.

    --mirror

    Note also that --mirror can be used to push both branch and tag references at once, but the problem with this flag is that it pushes all references in .git/refs/, not just .git/refs/heads and .git/refs/tags, which may not be what you want to push to your remote.

    For example, --mirror can push your remote tracking branches from your old remote(s) that are under .git/refs/remotes/<remote>/, as well as other references such as .git/refs/original/, which is a by-product of git filter-branch.