Git and Heroku Workflow With Ruby on Rails

Getting a Git workflow going with Heroku can be rough. Using Rails, this tutorial works on granting a new developer access to a development environment, where your code can be worked on locally, a staging environment where your code can be pushed to Heroku first to make sure you haven’t broken anything, and finally a production environment where your code will be pushed directly to your website. Additionally there will be lists of other commands that are frequently used in various situations in Git and Heroku that will be compiled at the end.


  • Rails 5
  • Basic Terminal knowledge
  • Git (installed on your computer)
  • Github (account created)
  • Heroku (account created)
  • A website that’s already been created with Heroku
  • A repository that’s already been created in Github

Setting Everything Up

Granting Access To Github and Heroku

To grant access to the Github repository …

  1. Open the repository on your computer and click settings (it has the gear icon).
  2. In the left sidebar click collaborators and enter your Github password if it asks you to.
  3. An input field should appear, type the username of the person you wish to grant access to, and click the Add Collaborator button.
  4. An email will be sent to the person you wish to grant access to, have them open it and accept the invitation.

To grant access to the Heroku project, navigate to your project folder in the terminal. Once you’re there, type in this command

heroku access:add

From here on out the person you’re granting access to will have to take over on their computer.

Cloning The Github Repository

Create a new folder in the terminal. This folder will be where you store the website, so organize it appropriately. In Github find the main page of the repository you want to clone and copy the url at the top of the page, make sure the url is placed in between quotes. Add the repo to your computer.

git clone ""

Creating Heroku Remotes

Creating remotes allows you to differentiate and organize the ways you can push your code to development, staging, and production. After you sign into Heroku you should be brought to a dashboard that displays a list of all the apps you have on your account. Find the name of the app and type this command into the terminal.

heroku git:remote -a save-the-pandas-1962

Falling wind in this case is one of the strange names Heroku will randomly generate for you when you create a website, just use your web app’s name in its place. Next you will want to rename the remote to staging, so that when you push it’s easier to know that you’re pushing to staging.

git remote rename heroku staging

This command changes the name of the remote you just created to staging. To check that this worked, you can check your remotes in the terminal.

git remote -v

The process should be repeated for production. And that will look like this:

heroku git:remote -a save-the-pandas-1962
git remote rename heroku production
git remote -v

Git and Heroku Workflow

Creating Branches

With the remotes all setup, you’re almost ready to add code, but first you should check what branch you’re on.

git branch

If you’re on master you should create a new branch, this will keep your master branch free from any mistakes or anything else you don’t want to be pushed up to your website.

git checkout -b whatever-youre-calling-your-branch

Pushing To Git

You’re now on your branch, you’ve added some code, and you want to push it up to development first, which is what you’re running locally.

First add the branch.

git add .

Commit the branch with a descriptive message about what you’re going to be pushing up.

git commit -m "your descriptive message"

Push the branch up to Github.

git push origin whatever-youre-calling-your-branch

Pushing To Staging

As you push branches to staging, there will likely be features on staging that you will not want to commit to production. In order to push your branch up to staging you will also need to make sure it is in synch with what’s up on staging, which will require a pull request. So in order to avoid pulling a bunch of code that you don’t want on your production website, another branch in Git needs to be created. This branch should be created while you are on the branch you’re currently working on so that it contains all of that information you’ve added.

git checkout -b whatever-youre-calling-your-branch-staging

With your newly created staging branch, you can run a git status to check and see that nothing else needs to be pushed up. Then it’s time to pull the Heroku Staging site and make sure it’s synched up with your code.

git pull staging master

If you have merge conflicts, see the Fix Merge Conflicts section, otherwise you can now push your branch up to the Heroku Staging site.

git push staging whatever-youre-calling-your-branch-staging:master

Check and make sure your features are working correctly, see Common Errors and Issues if they aren’t.

Pushing To Production

If you had to make any changes to the staging branch, you will have to go back and insert them into the branch you’re planning on pushing to production via adding, committing, and pushing (like you did in Pushing To Git). When pushing to production it’s important to make sure you’re pushing the original branch you were working on and not the staging branch. If you’re still on staging, switch back to your original branch.

git checkout whatever-youre-calling-your-branch

Next pull your master branch and make sure there aren’t any conflicts.

git pull origin master

If there are no merge conflicts, make sure your branch has been pushed up to Gitub, and then, go to the website’s repository on Github.

  1. Click on the green button to the right side of the screen that says “Compare & Pull Request.”
  2. Click the “Create Pull Request” Button
  3. Check over the code on the commit that’s being pushed up, or have someone else check it.
  4. Click on the green button that says “Merge Pull Request”
  5. Click on the green button that says “Confirm Merge”

Back in the terminal switch to the master branch

git checkout master

Your master branch on Github is now ahead of the master branch on your terminal, so you will need to pull the master branch from Github in order to have access to your most recent merge.

git pull origin master

Now you will need to pull production from Heroku.

git pull production master

If there are no merge conflicts, push master up to production.

git push production master

Common Errors and Issues

Fixing Merge Conflicts

Anytime you pull a branch into another branch there’s the possibility for merge conflicts. If there are conflicts, the terminal will return them to you along with the files they can be found in. To fix a merge conflict go into the file(s) specified by the terminal and search for the lines of code that are conflicting. An example might look like this.

<<<<<<< HEAD:mergename
 This Line Was Originally In The Project I Am Merging To
 This Is The Line I Am Adding
 >>>>>>> 4e2b407f501b68f8588aa645acafffa0224b9b78:mergename

Remove the lines you don’t intend to keep in your website, and make sure to delete the head, tail, and equal signs. Once the conflict has been resolved, follow the Pushing To Git steps again, and commit with a message along the lines of “fixed merge conflict.”


If you change any files that fall in the asset pipeline (ie. JavaScript, CSS, etc), you will need to precompile before you push up to Heroku.

rake assets:precompile

In some cases you may need to execute a bundle exec in order to precompile

bundle exec rake assets:precompile

Setting Up The Database In Heroku

If you create a new table or add new fields to your database in development, you will need to reconfigure the database in Heroku for staging and production. After you’ve pushed your changes to Heroku, migrate your database fields, specify the name of your remote if you haven’t.  This example is specifically changing the staging remote.

heroku run rake db:migrate --remote staging 

After running a migrate the server needs to be restarted.

heroku restart --remote staging 

If you need to upload a seed file, use this rake command.

heroku run rake db:seed --remote staging

Restart the Heroku server after seeding.

.ENV Files In Heroku

If you have .ENV files in Rails and you need to pass some of that information over to Heroku, you will have to grab each individual line from that .ENV file and let Heroku have access to them.

heroku config:set API_TOKEN=46251 --remote staging

Rollbacks – aka You Broke The Website

If you push to staging or production and you want to revert back to a previous version of the website, you will need to find the previous version of the website you wish to go back to.

heroku releases --app name-of-app

Select the version number you wish to revert back to (for example v312), then rollback the site.

heroku rollback v312 --app name-of-app

Additional Git Commands

Branch Commands

In the future should you ever need to switch from one branch to another you can use:

git checkout whatever-youre-calling-your-branch

If you need to pull a branch from Github, you can use fetch. The name before the colon is the name of the branch you’re fetching on Github, the name after the colon is what you plan on calling the branch on your terminal.

git fetch origin whatever-youre-calling-your-branch:whatever-youre-calling-your-branch

If you need to delete your branch from the terminal, use:

git branch -D whatever-youre-calling-your-branch

Check Previous Commits

If you want to check your previous commits check the log, to exit type “q.”

git log

Merging From The Terminal

If you want to merge a branch from the terminal without using Github (not really recommended). First switch to the branch you want the branch to be merged into.

git checkout master

Then call for a merge of the branch you want.

git merge whatever-youre-calling-your-branch

Vim Escape

To get out of Vim first hit “Esc” key before typing the following in your terminal.


Reseting A Merge

Find the previous commit you wish to go back to.

git log

While being on the branch you wish to revert back on, make a copy of the branch, in case your reset doesn’t go as planned.

git branch whatever-youre-calling-your-branch-copyname

Then reset the branch to the commit you wish to go back to ie., paste in your own long commit number you found on the git log.

git reset --hard b4b5cdahj3j22431kjljjl21j

Add the branch

git add .

Commit the branch

git commit -m "your commit message"

Then force push the branch. Warning this will overwrite previous pushes make sure you’re pushing over the correct branches

git push origin master -f

If you need to push up to Heroku and you’re being asked for a pull request, this may cause you to pull down the original branch you were trying to overwrite. A force push to Heroku may be necessary.

git push --force production master

Additional Heroku Commands

App Info

Get info on your app, and Heroku plan

<span class="s1">heroku pg:info -a your-app-name

Creating an Admin With Heroku Console

Assuming you have a table called Admin, with fields for email and password, here’s how you would create a new admin using Heroku’s console.

heroku run rails console --remote heroku

After opening the console, create the new admin and set it to a variable named admin.

admin = "", password: "password")

If the admin is created successfully save the admin

If the admin isn’t successfully created check your error messages


Checking The Server

If you want to check what was previously run through your Heroku server.

heroku logs --remote staging

If you want to look at a live server on Heroku.

heroku logs --tail --remote staging

Database Backups

Create a database backup

heroku pg:backups:capture --app your-app-name

Cancel a database backup

<span class="function">heroku pg:backups:cancel --app your-app-name

Download a database backup

heroku pg:backups:download --app your-app-name

Delete a database backup

heroku pg:backups:delete b001 --app your-app-name

Get info on one of your database backups with an ID

<span class="s1">heroku pg:backups --app your-app-name

Get more detailed info on one of your database backups with an ID

heroku pg:backups:info b001 --app your-app-name



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s