Migrating Git Repository
If you are thinking about migrating a Git directory from an existing machine (i.e, the one from which you make commits to your repository) to another machine. Then I believe, this post could be a great help.
There is a folder named ‘.git’, which is created the moment you initialize the git repository under a folder. This folder contains the information about ‘origin’, ‘trunk’, etc. Or in simple words, the information about your ‘Git’ repository.
So, lets start with the directory migration, I will explain using a test scenario.
1) Two machines (abc & xyz) and corresponding ‘Git’ user accounts, ‘gituser@abc’ & ‘gituser@xyz’.
2) A ‘Git’ directory that is required to be migrated: my_git_folder
1) Copy all the contents of ‘/home/gituser/my_git_folder’ present at ‘abc’ machine to ‘/home/gituser/my_git_folder’ present at ‘xyz’ machine.
2) To allow the ‘user’ which is present at other machine (gituser@xyz), make commits to your git repository, you need to create an SSH-key pair and then add the public key (id_rsa.pub) to your account at Github (Account Settings > SSH Keys)
[gituser@xyz]# ssh-keygen -t rsa -C "email@example.com" Generating public/private rsa key pair. Enter file in which to save the key (/home/gituser/.ssh/id_rsa):[Press Enter] Enter passphrase (empty for no passphrase): [Type a passphrase] Enter same passphrase again: [Type passphrase again] Your identification has been saved in /home/gituser/.ssh/id_rsa. Your public key has been saved in /home/gituser/.ssh/id_rsa.pub. The key fingerprint is: 05:0e:44:gb:ca:55:f6:7d:61:7f:f1:95:9f:f0:22:99 firstname.lastname@example.org
NOTE: Please make sure that the folder ‘/home/gituser/.ssh’ and a file ‘/home/gituser/.ssh/authorized_keys’ is already present. If not, create them and then proceed with creating the SSH key-pair.
3) Add the public key (/home/gituser/.ssh/id_rsa.pub) to your Git account. (Account Settings > SSH Keys)
4) Test the connection:
[gituser@xyz]# ssh -T email@example.com The authenticity of host 'github.com (18.104.22.168)' can't be established. RSA key fingerprint is 05:0e:44:gb:ca:55:f6:7d:61:7f:f1:95:9f:f0:22:99. Are you sure you want to continue connecting (yes/no)? [Type yes] Hi username! You've successfully authenticated, but GitHub does not provide shell access.
By now, your new machine has the authentication to commit to your Git repository.
But, to make the commits you need to tell Github that it is you using the new machine and trying to commit !
5) So, while pushing some new data to your repository from your new machine, you would be trying something like:
[gituser@xyz my_git_folder]# git init [gituser@xyz my_git_folder]# git add the_contents [gituser@xyz my_git_folder]# git commit -a #Save the file with a commit identification. [gituser@xyz my_git_folder]# git push origin master
Here above, you will be prompted to identify your self:
*** Please tell me who you are.
6) Run the following commands:
[gituser@xyz my_git_folder]# git config --global user.email "your_git_email_id@youremail" [gituser@xyz my_git_folder]# git config --global user.name "Your Name"
7) Now again, try to push:
[gituser@xyz my_git_folder]# git push origin master
You will see output, something like:
Counting objects: 7, done. Delta compression using up to 2 threads. Compressing objects: 100% (5/5), done. Writing objects: 100% (7/7), done. total 7 (delta 1), reused 0 (delta 0)
Deal done !