Let’s say you have an existing app named foo. It’s already setup on Heroku (with the name foo), and you want to create a staging environment for this app. Using Heroku’s CLI, it’s easy.
$ heroku fork foo-staging
That will create a Heroku fork of your app, database and all. It should even duplicate your add-ons, but there are some exceptions there; notably: Heroku Scheduler.
Now, you have to tell git that you have a new remote named ‘staging’
$ git remote add staging email@example.com:foo-staging.git
By default, heroku will still think this new app uses the ‘production’ environment (when I say ‘environment’, think config/environments), so you have to change this:
$ heroku config:set RACK_ENV=staging RAILS_ENV=staging —app foo-staging
That will require you to create an environments/staging.rb file and add a db config for staging.
That’s pretty much it, but now that you have two heroku apps tied to the same codebase, you can’t run heroku commands without specifying the app’s name:
$ heroku run rails console -a foo-staging
And to do a push to the staging environment:
git push staging master