Recently I had the need to migrate one of my GIT repositories to a different one. For a long time, I was working on my personal repository. The time had come to move this to a corporate one as we were starting to add more members to our team. However, this created couple of challenges for us.
- Migration of the old repository to a new one so that we have all history
- Continue to use both repositories on my computer
There are a lot of suggested ways to do both of these tasks. In this blog, I will just add what worked for me during this process.
Migration
Migrating from old repository to new is a three step process.
Step 1:
- Go to the directory you want to clone in old repo
- Run the following command
- Provide credentials for the old GitHub server
$ git clone --mirror https://github.com/<user>/<repo_name>.git
Step 2:
- Create the new repository in GitHub
- Login to Github and create an empty repository to push
Step 3:
- Push to the new repository
- Make sure to give the new credentials
$ git push --mirror https://github.com/<new_user>/<repo_name>.git
This should migrate this repository to the new one. However, as you saw, this will not work if you already had your credentials saved in keyring. In that case credentials has to be removed, so that we can pass different credentials for the old and new repositories.
Also now we have a different problem. What happens if we still use both the GitHub accounts? Where do we store the credentials? Gitbash does not allow to store credentials for two or more GitHub accounts.
Next we will configure gitbash to be able to use both accounts.
Multiple Account Configuration
Since gitbash does not support multiple accounts, we will leverage a configuration file provided by SSH. This also means that instead of using the HTTPS link from GitHub, we will be using SSH endpoint.
Of course you will need ssh installed. All Linux or Mac already comes with ssh pre-installed, so we already have the commands avialble.
First we will generate a SSH key.
$ ssh-keygen -t rsa -b 4096 -C <git_user_old_repo>
$ ssh-keygen -t rsa -b 4096 -C <git_user_new_repo>
Each of these commands will create two files in your home/.ssh directory. One of them ends in .pub, and is the public key. The other file generated is the private key. Make sure to rename the files between invocation of the commands – else it will overwrite the previous ssh key files. As a next step, we will store the public key to GitHub account. For the account that we generated the key for, log into GitHub, and go to Settings -> SSH & GPG keys, and add a New SSH key. Copy the key stored in .pub file into the key text area provided.
Now we will create a new file called ~/.ssh/config. Please see below for format.
--# GitHub 'OLD' account
Host github.com-<old_user>
Hostname github.com
User git
IdentityFile ~/.ssh/<old_user>-git
IdentitiesOnly yes
--# GitHub 'NEW' account
Host github.com-<new_user>
Hostname github.com
User git
IdentityFile ~/.ssh/<new_user>-git
IdentitiesOnly yes
That’s all to it. Now we are ready to use this for gitbash check-in/ check-outs. You will only need to substitute appropriate endpoint for GiHub where needed.
$ git clone git@github.com-<old_user>:<user>/<repo>.git
$ git push git@github.com-<new_user>:<user>/<repo>.git
Hopefully this short blog will help someone to work with GIT easier. Ciao for now!