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


Changing URL for Remote

I changed the hosting of my repo from Github to Bitbucket.  I like Bitbucket because not only does it house the code and history, but also it ties in with a bug tracker that allows for tracking (and assigning) of bugs, but also collaboration with developers.

To remap my remote repo, I did this:

git remote set-url [example] [git://]


[example] = repo name

[git://] = new repo url

Git Commands

Here are the git commands I use most often (I use git GUI so I don’t usually type the commands git add, git commit, & git push to move files):

  • git branch – tells you what branch you are currently in
  • git checkout [branch] – switch branches
  • git merge [branch] – bring in the code from the named branch, into the current branch
  • git pull [repository] [branch] – pull my files
  • git push [repository] [branch] – push my files
  • git stash – store local changes (usually when there is a merge conflict)
  • git stash apply (retrieve the stashed local changes)

for me:
[repository] = origin
[branch] = master and dev

Setting up an Existing Rails App Locally

I had to download an existing Rails app and set it up.  To set-up a new Rails app, just follow the directions on the RoR site.

(1) download and install railsinstaller (

(2) in the ruby command window, create a new app in the “sites” directory –

rails new appnamehere

(3) set-up git so you could download/upload to github and heroku (for version control).  follow these directions  and then these (ignore the parts about creating a new app and editing it, just need to set-up git).  when you add the remote, just use change the Hello-World to your app’s name

(4) add heroku.  follow these steps, (skip to step 3 since you set up git earlier)

(5) pull the latest version of your code.  In Git Bash go to (“cd …” for moving up in the directory, cd pathname (ie “cd sites/yourappname”) your app’s directory

(6) in git bash run: git pull heroku master

(7) go back to ruby command prompt to create db, run – rake db:create (i had an issue with this once, but this solved it)

(8) Create tables using schema – (i had connection issues, but this answer helped me)

rake db:schema:load

(9) run

rake db:migrate

(10) the tables should be set-up and you should be able to run “rails server” (or rails s for short).  when you go to localhost:3000 in your browser, you should see your app

(11) go to to download everything you need to run phpmyadmin, which will allow you to edit and view tables (structure and data).  i was having trouble the more recent versions so i had to use 2.4.9 (version) and that worked (set the host/domain name to localhost).  you’ll need to know what is the database login info for the development environment for the install of appserv (it will ask for it during the mysql install) – look in the sites/youappname/config/database.yml file for this info

(12) after install appserv, i could go to http://localhost/phpmyadmin/ in my browser to see my database and tables

(13) run “bundle install” in ruby command prompt (installs all gems specified in gemfile)