Understanding Heroku Git deployments

Heroku is a PaaS (Platform as a Service) that allows to build and run web apps, jobs and APIs in 8 languages, while the platform takes care of things like routing, erosion or failures for you. When using Heroku, you can utilize add-ons that will make your life easier, to create and use databases, handle deployments, manage logging etc., among other features, making the tasks of building, deploying and running apps a much pleasant experience.

In the Heroku world, normally developers own deployments. This doesn’t mean that, you, as a developer, need to become an expert on devops and systems. However, you will need to understand how the platform works and how you can upload your code so that it is built and run.

There are several ways in which you can deploy to Heroku. For example, you can use Docker or use one of the integrations as the GitHub integration, to deploy from GitHub to Heroku directly. In this post I want to focus in the most common way to deploy to heroku and the easiest to get started with it: Git.

Git is the most extended distributed version control system, in which repos can be created locally or remotely, and are normally synchronized to push and pull changes to or from other developers.

The typical configuration when using git and heroku is to have a local repository in which you work, and then push your changes to a remote repository, which is your Heroku app, the one that lives in the heroku servers. When we bind a local and a remote repository, we say that we create a git remote for our local repo. In this case, our git remote will be the heroku app.

Setting up your local repository and heroku app

Depending if your heroku app already existed, of if you have cloned your repo from somewhere else (eg: github), there may be several flows to set up your repo:

A) Initialize a local repo and create a heroku app from scratch. In this case, neither of the repositories exist, so we will need to create them. We will execute:

git init – To initialize the local repository

heroku create – To create the Heroku app and link it to our local repo (creating a git remote called “heroku”)

B) Clone an existing repository from GitHub and create a heroku app. In this case, we initialize our local repository making a copy of a remote repo stored in github, but the heroku app doesn’t exist, so we create it. We will execute:

git clone remoteRepoUrl – To create a local repo cloning it from an existing one. Note this adds a remote to our repo called “origin”.

heroku create – To create the Heroku app and link it to our local repo (creating a git remote called “heroku”)

You have read correctly. In this case, our local repo is linked to two remotes, “origin”, the one from which we cloned the repo, and “heroku”, the one in the heroku servers.

Note: This has nothing to do with the GitHub integration. In the GitHub integration, you don’t interact with the heroku app directly using Git, but with a remote GitHub repo that is the one that synchronizes with the heroku app. I will write a post about this soon.

C) The app exists in heroku (the remote repo), but we don’t have a local repo for it (option 1). In this case, we can clone the heroku app.

heroku git:clone myApp – Clone the heroku app git repo. We could do it using git clone directly, but normally we use the heroku command, which avoids us to write the full heroku app git repo url.

D) The app exists in heroku (the remote repo), but we don’t have a local repo for it (option 2). Another option in this case is to initialize the local git repository and add a remote to the existing heroku repo.

git init – To initialize the local repository

heroku git:remote myApp – Add a remote to the heroku app git repo. We could do it using git remote directly, but normally we use the heroku command, which avoids us to write the full heroku app git repo url.

E) Clone an existing repository from GitHub and add a remote to an existing heroku app. In this case, we initialize our local repository making a copy of a remote repo stored in github, but the heroku app exists, so we create just the remote. We will execute:

git clone remoteRepoUrl – To create a local repo cloning it from an existing one. Note this adds a remote to our repo called “origin”.

heroku git:remote myApp – Add a remote to the heroku app git repo.

You may have notice we have two remotes again, as in B).

Pushing your code to the Heroku app

Git repositories have three areas, the working area, the staging area (index) and the repo area. When you clone a repo from GitHub, for example, everything will be in the repo area already.

However, when making local changes, before being able to push to a remote repo, changes need to be passed to the staging area first (git add) and then to the repo area (git commit). All this process happens locally.

Then, you will be able to send your changes to the heroku repo using the git push command.

Some things that we have to take into account when pushing to Heroku are:

  • We need to specify the remote, “heroku“.
  • We need to specify the remote branch, which will always be “master“.
  • We can deploy from our “master” branch to the heroku “master” branch, or from any other local branch. Look at this example:

Finally, I want to add that if you are working with several remotes, you can control to which remote you push indicating the remote name, as in the next example:

If you want more information, check the official Heroku documentation, which is fantastic. Enjoy deploying!

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

Blog at WordPress.com.

Up ↑

%d bloggers like this: