Accessing a Git Repository Using a Key Pair

We recently moved our git repositories over to Amazon Web Services.  We ran into one issue with it which was that now our git requests needed to have a key pair attached.  The Internet was not very kind on explaining how to do this very well so I'm documenting it here.

The key pair you'll want to use is the one generated for you by AWS.  It is the same one you use when you ssh into your AWS EC2 instances.  Take this key pair file, which I will call keypair.pem from now on, and we'll need to move it and configure our git to use it.

  1. Copy the keypair.pem into your .ssh folder.
    cp /path/to/file/keypair.pem ~/.ssh/keypair.pem
  2. Create/open your .ssh config file.
    nano ~/.ssh/config
  3. In your config file
    Host git
    Hostname EC2PublicDNS
    User ubuntu
    IdentityFile /home/username/.ssh/keypair.pem
  4.  To clone the repository use the following
    git clone git:path/to/projects/PROJECT_NAME.git
  5. To update existing projects on your machine to use the new address follow the instructions below
  6. Open your project's git config file
    nano ~/path/to/project/.git/config
  7. Change the url line to be
    git:path/to/projects/PROJECT_NAME.git
That's it. Hope this helps anyone who is getting the error 'Error: Permission denied (publickey)'.  There are other ways to do this but this way was pretty easy.