Deploy Git branch to Heroku


This article goes over how to create and deploy a Git branch to Heroku via the CLI.

Prerequisites

Create

Let’s say you want to create a Heroku app named my-app.

Create it in Heroku dashboard or via the CLI:

heroku apps:create my-app # --team my-team

Optionally, you can add it to an existing pipeline:

heroku pipelines:add my-pipeline --app my-app # --stage development

Remote

Set the Git remote for your app:

heroku git:remote --app my-app

By default, the Heroku remote is named heroku:

git remote -v
heroku  https://git.heroku.com/my-app.git (fetch)
heroku  https://git.heroku.com/my-app.git (push)

If you’re adding multiple remotes, it’s good practice to rename the remote:

git remote rename heroku heroku-my-app

Config Vars

Save the config vars or environment variables from another Heroku app other-app:

heroku config --shell --app other-app > .env.heroku

Set the config vars to my-app:

cat .env.heroku | tr '\n' ' ' | xargs heroku config:set --app my-app

Deploy

Deploy by pushing your branch to the Heroku remote:

git push <remote> <branch>:master

For example:

git push heroku-my-app my-branch:master

Alternatively, you can replace master with main:

git push heroku-my-app my-branch:main

Destroy

After you’re done, delete the app from the dashboard or via the CLI:

heroku apps:destroy my-app --confirm my-app

Then remove the remote:

git remote rm heroku-my-app

Resources



Please support this site and join our Discord!