Skip to content

Instantly share code, notes, and snippets.

@Disha-Shah
Created January 3, 2017 06:53
Show Gist options
  • Save Disha-Shah/99d4d7aefe80bc6d355b419037823e19 to your computer and use it in GitHub Desktop.
Save Disha-Shah/99d4d7aefe80bc6d355b419037823e19 to your computer and use it in GitHub Desktop.
Create 2 environments - Staging and Production from same App (Heroku and Git)

NOTICE: When I say "appname" it means enter your own! It's not a command or name native to git or Heroku. By default, your one and only remote is called "heroku". This is why you do things like $ git push heroku master, you're pushing to the master branch of heroku. What we want is two remotes: one staging, one production. You can view your remotes by typing...

$ heroku apps:create staging-appname
$ git remote -v 
   // this should return:
   // heroku	git@heroku.com:appname.git (fetch)
   // heroku	git@heroku.com:appname.git (push)

So, lets rename that default heroku git remote to a staging, production one.

$ git remote rename heroku production

Now, create a new repo. This is essentially splitting your local git repo to be able to push to two different remotes. Also, from now on, when you run things like $ heroku run rake db:migrate, it's going to ask you to specify which app you want to do it to. Lets add our new remote to the local repo, that points to the 2nd app you made.

$ git remote add staging git@heroku.com:staging-appname.git

Now lets run that remote command again! $ git remote -v:

production	git@heroku.com:appname.git (fetch)
production	git@heroku.com:appname.git (push)
staging	git@heroku.com:staging-appname.git (fetch)
staging	git@heroku.com:staging-appname.git (push)

Awesome; it worked. Note: to push, you must now specify which remote instead of just saying $ git push heroku master

Example workflow:

// Add files you've been working on
$ git add .
// Commit to your local repo
$ git commit -m "Made some changes"

// Push to `staging`
git push staging master

// Everything look ok on your staging app? Cool.
git push production master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment