How to push to git on EC2
Solution 1
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
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
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
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
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: cannot push into git repository for explanation of "recieve deny current branch"
Related videos on Youtube
zengr
Software Engineer in San Fransisco Bay Area. #Java #Python #Generalist ↑ ↑ ↓ ↓ ← → ← → B A
Updated on December 23, 2021Comments
-
zengr over 2 years
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 getPermission 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 [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.-
zengr about 13 years
ssh-add /private/key/path
worked! -
designermonkey about 12 yearsWhen you say it worked, can you add instructions as to what you actually did step by step?
-
zengr about 12 years@Designermonkey Updated.
-
designermonkey almost 12 yearsWhich machine do you run that on, local or EC2 instance? What is the
express_app
in your config? -
zengr almost 12 years@Designermonkey its on ec2 instance. Its the name of the git repo, which is a node's express framework app.
-
Harry almost 10 yearsthis guide works well for me: jeffhoefs.com/2012/09/…
-
-
zengr over 13 yearsbut I already have the key-value pair private key with me, which I used to login to EC2.
-
Jon over 13 yearsTry some of the solutions in this thread: serverfault.com/questions/39733/…
-
zengr over 13 yearsi understand that part, but this is a Git configuration issue.
-
Jon over 13 yearsI don't see anything wrong with your configuration, so I believe that it is something wrong with your SSH keys, either misplaced or what not - it most likely would be that and not your configuration.
-
zengr over 13 yearsokay, where do we specify the identity file (the private key) for git in the local machine?
-
Jon over 13 yearsIf you have your key in an odd path just run
ssh-add /private/key/path
. -
Michael Kohne about 12 yearsI followed this, but used an rsa key instead of a dsa key. Also, I added a space between cat and >>, like: "cat >> .ssh/authorized_keys"
-
isomorphismes over 11 yearsI also found useful from that to
git remote add ec2 ssh://[email protected]:zivot
. I didn't know one could prefix addresses withssh://
before that. -
Eric Wilson over 9 yearsGreat answer, was hoping to take advantage of the ssh config that I'm already using.
-
Saifur Rahman Mohsin over 8 yearsAwesome. This is better than figuring out the complete URL.
-
Abel Callejo over 8 yearsthis works for me. the chmods are very important. @devdrc you may need to edit it further and make the command line statements emphasized.
-
psvj over 8 yearsthis does not answer the question of how we specify the key when executing a git push command
-
JoeTidee over 7 yearsAre steps 2 and 3 the same?
-
Alastair over 7 yearsNo, @JoeTidee - step 2 is getting the key onto the remote server and step 3 is adding it to the right place. :)
-
jeff musk about 7 yearsthe part before bash script worked great, but bash script didnt work for me. This answer stackoverflow.com/a/24027870/847954 worked great for me. Thanks devdrc for this post and @blamb for posting the script.
-
Abel Callejo almost 7 years@jeffmusk you need to make sure that
post-receive
file is executable -
rikkitikkitumbo over 6 yearsI don't understand when you say "To fix I did: /path to pemfile/ " my pemfile.pem doesn't execute anything.... what command do you use to add the identity?
-
Enginerd Sunio almost 5 yearsplease specify and conclude your answer that which command you have used to add key???
-
elthwi over 2 yearsStill saving hours of internet digging 10 years later. Thank you!