Deploying to Heroku using a Git branch

A simple method of deploying a webapp to Heroku without being afraid of publishing your database credentials

If you're making a webapp to host on Heroku (or other similar host) but also want to publish your code as open source you may have wondered how to store your database credentials or other private config within Git to push to the host but not to push to your chosen open source project host.

A lot of the private settings you need to store for your app can be set as environment variables within Heroku, and things like OAuth keys are recommended to be stored in that way, but when you first create your application you are given credentials for their PostgreSQL database and you are instructed to put them into your config/database.yml file.

I'm not claiming to have all the answers, this is just the approach I've taken so far as I couldn't see a better way. I'd love to hear from others that are achieving this more easily.

Traditional deployments

If you were deploying your app with Capistrano onto a normal (non-Git host) you could do this by renaming your empty database.yml file to database.template.yml, and commit that to Git, then adding database.yml to your .gitignore file. This would mean your credentials wouldn't be shared when you push to your public remote, but can be deployed to your app host.

The problem with this approach is that you're not dependent on your development machine in order to deploy, and your credentials and other private config aren't backed up in Git.

Using a branch

The way I've found works for me is to have a 'production-heroku' branch, and put any dedicated config onto that branch. The way I've done it is as follows:

  1. Add the Heroku repo as a remote: git remote add heroku
  2. Create the branch: git branch production-heroku
  3. Checkout the branch: git checkout production-heroku
  4. Add your credentials and other config
  5. Push the branch to create the remote master branch: git push heroku production-heroku:master
  6. set your branch's up-stream to Heroku: git branch --set-upstream production-heroku heroku/master

Now for future deployments you can deploy using the following few commands:

git checkout production-heroku 
git merge master
git push heroku production-heroku:master
git checkout master

This will merge cleanly for most deployments, and will allow you to push your master branch to your public repo for people to fork.

You can of course chain these into a single line with && and add it as an alias in your chosen shell config. You can also change the merge command to merge any particular commit from your master branch in case you don't want to deploy your most recent commits.

This leaves you with a bit better off than the traditional deployment scenario, but you can easily push your branch to a private repo (BitBucket offers free private repos).

Just don't push the production branch to your public repo. It shouldn't push normally since the upstream is set, but it won't stop mishaps completely.

If you don't have a Google+ account you can comment using the normal comment form below

About the author

Portrait of the author

On weekdays I'm a Technical Lead at, having previously been a Solution Architect at Nokia & Nokia Siemens Networks, creating creative software solutions for mobile operators around the world.

In my spare time I'm an avid new technology fan, and constantly strive to find innovative uses for the new gadgets I manage to get my hands on. Most recently I've been investigating Mobile Codes, RFID and Home automation (mainly Z-Wave). With a keen eye for usability I'm attempting to create some cost-effective, DIY technology solutions which would rival even high-end retail products. The software I develop is usually released as Open Source.

I have a Finnish geek partner, so have begun the difficult task of learning Finnish.

Add me to your circles on Google+

The blog
November 2022

Zap the link below with your qrcode enabled mobile to send this page to it
Mobile Code for this page
What's this?