如何在 EC2上推入 git

我试图遵循 这个指令。我有一个本地 git repo,当我执行 git 推送时,我需要将 repo 推送到我的 EC2实例。

但是,在上面的教程中,当我执行 git push origin master时,会得到 Permission denied (publickey)错误,因为我没有指定标识文件。

比如,我像这样登录到 EC2: ssh -i my_key.pem username@11.111.11.11

那么,我可以在这里做类似于: git -i my_key.pem push origin master或者在 .git/config中设置身份文件的事情吗

那么,我该怎么安排呢?

更新: git config -l的输出

user.name=my name
user.email=my_email_addreess@gmail.com
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/*

更新(来自@Jon 的 评论) :

如果你有你的关键在一个奇怪的路径只是运行 ssh-add /private/key/path。这为我工作。

63346 次浏览

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

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

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!

  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

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.

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/

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!

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.

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/

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.

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 ec2Username@long-crazy-amazon-ip.com:/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: cannot push into git repository for explanation of "recieve deny current branch"

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.