Follow Along as I Stumble on the Path to Learning RoR

Getting EC2 to Use Git for Rails App

I wanted to use git to push my code.  Since I used an Amazon Machine Image (AMI) that installed Ruby on Rails that also included git, git was already installed on my Linux server, and I just needed to make it work.

(1) go into Services > EC2 and make sure that you have a key pair attached to your instance.  If not click the key pairs link on the left side, create a key pair and make a new instance that using this, so you have SSH access to your server.

(2) log into your instance using SSH

(3) go to the home directory, enter:

cd /home

(4) create a new directory in home (this is not where your Rails app will go):

mkdir repo

(5) go into repo:

cd repo

(6) initiate git for repo:

git init –bare

(7) create the directory where your Rails app will go.  Mine went into /var/www/app, so:

cd /var/www

mkdir app

(7) next go to the repo/hooks directory:

cd /home/repo/hooks

(8) hooks are scripts that run at given points in the git process.  we need to edit the receive-post file – this is triggered each time a new git push is made.  enter this:

cat > post-receive

(9) after hitting enter, type this:


GIT_WORK_TREE=/var/www/app (or wherever you want to put you Rails app)


git checkout -f

(10) after saving that you should be able to see your changes:

pico post-receive

(11) once you’ve confirmed that, run:

chmod +x hooks/post-receive

(12) next, when you do git pushes, you may have a personal ssh that is used, it’s an “” file.  for me, on windows, it was located in C:/Users/Me/.ssh

(13) copy the contents of that file (open it with notepad)

(14) go back to your SSH session and go to the ssh directory:

cd ~/.ssh

(15) In that directory, there is an authorized_keys file that contains a key that must match yours to do the git.  You can use the pico command to edit the file.  If you’ve never edited in pico, here are commands.

pico authorized_keys

(16) remove anything that is in there, paste your SSH details from step 8, then save the file

(17) go to git bash and add this new remote (if you have trouble with using your public ip, assign an elastic ip to your instance in the aws management console, in services > ec2):

git remote add origin ssh://[ec2 instance user name]@[public ip for your ec2]/home/repo

(18) in git bash:

git push origin



Comments are closed.