Copying/moving a Git repository including all branches to a new remote repository

Please note: this is written from the perspective of using the Git Bash shell.

  1. Clone the repository to be copied into a new location to ensure it is a fresh checkout
    git clone old-repo-url checkout-location
  2. cd into checkout-location, and run the following to checkout all branches from the remote:
    for remote in `git branch -r | grep -v HEAD`; do git checkout --track $remote; done
  3. If it doesn’t already exist, create the new remote repository e.g. new-repo
  4. Add a new remote that points to the new repository:
    git remote add new-repo new-repo-url
  5. Run the following to push all branches to the new remote:
    git push --all new-repo

Renaming a Git repository under Gitolite, keeping an alias for the old name

(If you haven’t yet seen it, Gitolite is a set of scripts for configuring a central Git server – see https://github.com/sitaramc/gitolite)

Renaming a repository is simple and fairly painless:

  1. Log onto your Git server (and any slaves), go to the repositories directory (by default it’s at ~/repositories), and rename your repository:

    mv OldRepoName.git NewRepoName.git

  2. Update your Gitolite configuration to use the new repository name, and push your changes

However, if after renaming the repository you also want to continue supporting the old name (e.g. for a transition period while users move over to the new name), then you can use the alias feature of Gitolite to do that:

  1. Log onto your Git server, and open the “rc” file, which is at ~/.gitolite.rc
  2. Look for the INPUT variable, and add (or uncomment) the line 'Alias::input', inside the array (ensuring it is before the 'Mirroring::input' element), for example:
    INPUT                       =>
            [
                # 'CpuTime::input',
                # 'Shell::input',
                 'Alias::input',
                 'Mirroring::input',
            ],
  3. Add a new variable called REPO_ALIASES with the aliases you want to support:
    REPO_ALIASES    =>
        {
            'foo'   =>  'bar',
        }

Now if you try to interact with the old repository (e.g. git@server:foo) it will redirect to the new repo (git@server:bar), giving the user a warning:

WARNING: 'foo' is an alias for 'bar'