When I initially started using Git I only ever bothered with the local repository. Add some files, change some files, stage them, commit them but never did I push them to a remote repository. Then after a while I decided that I wanted to move some of the projects I had hosted online in Subversion to a host offering Git so I had a bit to learn. I’m documenting it here for when I need to do it again as I tend to forget things that I don’t do too often.
I have three online Git hosts: GitHub, unfuddle.com, and projectlocker.com. All of them want a public key as a means of authentication when connecting to the remote repository. To create that key using GitBash, type the following:
ssh-keygen -t rsa -C "firstname.lastname@example.org"
This creates two files – a public/private key pair, one of which ends in a .pub extension which contains the public key. You need to open the file, copy the key and add it to your Git account.
In GitBash, link your local repository to your remote repository:
git remote add origin git@github:<YourName>/<YourProjectName.git>
Now you can push all the commits in your local repository to your remote:
git push origin master
Where “origin” is an alias for the address of your remote repository and “master” is the branch.
Now, if you have more than one online Git host you’ll want to add a key to those as well. You could use the same key pair amongst them all or you could create a new key for each one. Alternatively, you might have only one Git host but multiple repositories and and you want to restrict access to those with the correct key. Generate your public/private key pairs as before but you’ll need to enter a unique name when prompted. I have three files named id_rsa, id_rsa_uf, id_rsa_pl where uf is my unfuddle key and pl my project locker key. The default id_rsa is for my GitHub account.
All these files should be in your .ssh directory which is in Documents and Settings under your user name. In the same directory create a .config file. Windows doesn’t like files beginning with a period and won’t let you create one but you can do it in GitBash:
Now you can tell Git which key to use for which host. Open the .config file and enter something similar to the following:
Host github.com User git IdentityFile ~/.ssh/id_rsa Host yourprojectname.unfuddle.com User git IdentityFile ~/.ssh/id_rsa_uf Host projectlocker.com User git IdentityFile ~/.ssh/id_rsa_pl
Make sure you get the Host line correct otehrwise Git won’t be able to connect. Save the file. Now when you’re working in your source directory Git will automatically resolve the correct key to use for authentication as you push or pull from your remote repository. The first time you connect to your remote repository Git will warn about being unable to validate the remote end and ask you to confirm that you want to connect. When you type yes, an entry will be added to a file called known_hosts, also in the .ssh directory. After that you won’t be asked again.
As an aside, you don’t need an online account to push to/pull from of course. At work, we use TFS, but I have spikes that I want under version control without committing to the company VCS. In this case, I create a directory on a network drive that gets backed up every night and turn it into a bare repository:
git init --bare
Then I add the remote origin as before but specify a mapped drive path:
git remote add origin h:/repositories/myspikeproject
Now, I can push/pull to the mapped drive and have all the benefits of source control without polluting the company VCS.