How to push to git on EC2

GitAmazon Ec2

Git Problem Overview


I am trying to follow this instruction. I have a local git repo and when I do a git push, I need the repo to be pushed to my EC2 instance.

But, in the above tutorial, when I do a git push origin master, I get Permission denied (publickey) error because I did not specify the identity file.

Say, I login to EC2 like this: ssh -i my_key.pem [email protected]

So, can I do something similar here to: git -i my_key.pem push origin master or set the identity file in .git/config

So, how can I set it up?

Update: Output of git config -l

user.name=my name
user.email[email protected]
github.user=userid
core.repositoryformatversion=0
core.filemode=true
core.bare=false
core.logallrefupdates=true
core.ignorecase=true
remote.origin.url=ec2_id@my_e2_ip_address:express_app
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

Update (from @Jon's comment):

If you have your key in an odd path just run ssh-add /private/key/path. This worked for me.

Git Solutions


Solution 1 - Git

To copy your local ssh key to amazon try this

cat ~/.ssh/id_?sa.pub | ssh -i amazon-generated-key.pem ec2-user@amazon-instance-public-dns "cat >> .ssh/authorized_keys"

replacing the names of the key and amazon ec2 public dns, of course.

you will then be able to setup your remote on amazon

Solution 2 - Git

The instructions listed here were more useful to me.

From the link:

Adjust your ~/.ssh/config and add:

Host example
Hostname example.com
User myuser
IdentityFile ~/.ssh/other_id_rsa

Now use the ssh host alias as your repository:

$ git remote add origin example:repository.git
$ git pull origin master

And it should use the other_id_rsa key!

Solution 3 - Git

On your local machine, edit your ~/.ssh/config and add:

Host example
Hostname example.com
User myuser
IdentityFile ~/.ssh/YOURPRIVATEKEY

You should be able to login to your instance with "ssh example". Remember your private key should be chmod 400. Once you can ssh in without using "ssh -i mykey.pem username@host", do the following.

On your EC2 instance, initialize a bare repository, which is used to push to exclusively. The convention is to add the extention ".git" to the folder name. This may appear different than your local repo that normally has as .git folder inside of your "project" folder. Bare repositories (by definition) don't have a working tree attached to them, so you can't easily add files to them as you would in a normal non-bare repository. This is just they way it is done. On your ec2 instance:

mkdir project_folder.git
cd project_folder.git
git init --bare

Now, back on your local machine, use the ssh host alias when setting up your remote.

git remote add ec2 EXAMPLEHOSTFROMSSHCONFIG:/path/to/project_folder.git

Now, you should be able to do:

git push ec2 master

Now your code is being pushed to the server with no problems. But the problem at this point, is that your www folder on the ec2 instance does not contain the actual "working files" your web-server needs to execute. So, you need to setup a "hook" script that will execute when you push to ec2. This script will populate the appropriate folder on your ec2 instance with your actual project files.

So, on your ec2 instance, go into your project_folder.git/hooks directory. Then create a file called "post-receive" and chmod 775 it (it must be executable). Then insert this bash script:

#!/bin/bash
while read oldrev newrev ref
do
  branch=`echo $ref | cut -d/ -f3`
  if [ "ec2" == "$branch" -o "master" == "$branch" ]; then
    git --work-tree=/var/www/example.com/public_html/ checkout -f $branch    
    echo 'Changes pushed to Amazon EC2 PROD.'
  fi
done

Now, on your local machine, do a "git push ec2 master" and it should push the code to your bare repo, and then the post-receive hook script will checkout your files into the appropriate folder that your webserver is configured to read.

Solution 4 - Git

You need to generate and upload a SSH key onto the EC2 instance. Follow this tutorial: http://alestic.com/2010/10/ec2-ssh-keys

Solution 5 - Git

I found this was the quickest way: https://gist.github.com/matthewoden/b29353e266c554e04be8ea2058bcc2a0

Basically:

ssh-add /path/to/keypair.pem (the"-add" needs to be RIGHT AFTER the ssh)

check to see if it worked by: ssh ubuntu@crazylongAWSIP (maybe your username is not ubuntu)

After that you can set up a git repo on your ec2 and push to it:

git remote add origin [email protected]:/path/to/your/repo-name.git 
git config --global remote.origin.receivepack "git receive-pack" # needed for aws ec2 stuff.
git push origin master

Your options are to set up a 'bare' git repo on your ec2 (which means other git repos can pull from it and push to it, but it won't hold any files), or you can set up a NORMAL repo and push to it directly (my preference if you want to push local changes to your ec2 without having to constantly ssh into your ec2).

If you want to set up a NORMAL repo on the ec2, ssh in to the ec2, do a git init where you want, and then do this:

git config receive.denyCurrentBranch updateInstead

See: https://stackoverflow.com/questions/3221859/cannot-push-into-git-repository for explanation of "recieve deny current branch"

Solution 6 - Git

  1. Run ssh-keygen locally
  2. In your local ~/.ssh/ directory you should now see a public key file called id_rsa.pub - copy the contens of this file to the /etc/ssh/authorized_keys file, which is located on your remote server.

You can either copy and paste the contents, or upload the file to your remote server first and use the following command:

cat id_rsa.pub >> /etc/ssh/authorized_keys

Solution 7 - Git

I'm not posting anything new here, I think, but I had to dig through the above answers to address my particular case. I have an Ubuntu instance on EC2.

To login to my instance, I needed to do:

ssh -i "pemfile.pem" ubuntu@very-long-amazon-address

the key file "pemfile.pem" had to be in quotes.

I added the remote:

remote add origin ubuntu@very-long-amazon-address/home/ubuntu/git/REPO/gitfile.git

But when I tried to push:

git push origin master

I got:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

To fix, I did:

/<path to pemfile>/pemfile.pem

Which gave me a response,

Identity added: /<path to pemfile>/pemfile.pem (/<path to pemfile>/pemfile.pem )

After which the push went through fine.

Solution 8 - Git

I was getting permission denied when deploying via source control and couldn't figure out why. I realized my user I was creating an ssh key for (named ubuntu, also the recommended login for my ec2 server) was not the user who was responsible for cap deploy (root). Running an ssh-keygen for root and uploading that ssh key as a deploy key to bitbucket solved my issues.

Solution 9 - Git

I know I'm too late for this but I just wanted to share this article which in just seconds I've successfully pushed to EC2 git repo

http://shirtdev.wordpress.com/2011/05/04/setting-up-a-git-repository-on-an-amazon-ec2-instance/

Solution 10 - Git

Here is the EASIEST way that worked great for me... I was having trouble cloning a repository... it was not recognizing the SSH Key I created... Instead of changing your config file and all that, I simply copied the REAL ssh key it was trying to connect with and I added this to bitbucket... here is the command:

 sudo vi /root/.ssh/id_rsa.pub

Used VI to open the REAL RSA key and copied the content and pasted into bitbucket... Done!

Solution 11 - Git

maybe this isn't a popular response, but I was struggling with the same problem and finally decided to store the folders on AWS S3 Bucket, it was the fastest solution because I was dealing with very large files and +3000 archives.

Simply install Aws cli, use aws configure and aws s3 cp SOURCE_DIR s3://DEST_BUCKET/ --recursive

After that, you could download it to your computer and use GitHub like always, or make your bucket public so you can get the archives anywhere.

Solution 12 - Git

For anyone else who might be interested, this solution proved to be the cleanest and easiest for me:

http://eric.sau.pe/accessing-a-git-repository-using-a-key-pair/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionzengrView Question on Stackoverflow
Solution 1 - GitrealgtView Answer on Stackoverflow
Solution 2 - GitTinyTimZamboniView Answer on Stackoverflow
Solution 3 - GitdevdrcView Answer on Stackoverflow
Solution 4 - GitJonView Answer on Stackoverflow
Solution 5 - GitrikkitikkitumboView Answer on Stackoverflow
Solution 6 - GitAlastairView Answer on Stackoverflow
Solution 7 - Gitguero64View Answer on Stackoverflow
Solution 8 - GitDannyView Answer on Stackoverflow
Solution 9 - GitJoe EjesView Answer on Stackoverflow
Solution 10 - GitrckehoeView Answer on Stackoverflow
Solution 11 - GitJosé Luis MarrugoView Answer on Stackoverflow
Solution 12 - GitTyguy7View Answer on Stackoverflow