Introduction

In working with Heroku I need to use some commands very frequently – e.g. to check which app I am working with, pull down remote code, push changes out etc. I always forget these useful commands and then have to hunt around for the syntax. I have listed these commands here so that, it’s a convenient quick reference for you when you want to work with your Heroku environment.

Get Started with Heroku

Create a free account in Heroku

https://signup.heroku.com/identity

Create an app using Heroku

https://dashboard.heroku.com/new

Add-on MySQL database your app

https://addons.heroku.com/cleardb

Install the Heroku Toolbelt on your local machine

https://toolbelt.heroku.com/

Login on your local machine using the email, password you chose while creating your account

c:>heroku login

Create the SSH keys

SSH ensures that traffic from your local machine to Heroku will be encrypted.
c:>ssh-keygen -t rsa
c:>heroku keys:add


How to Prepare a Local Staging Directory

Initialize git in the staging directory

Create an empty directory (e.g. c:wkdirgit) on your laptop & identify it as your local git repository.
On command line navigate to this directory and initialize git in this new folder.
c:wkdirgit>git init

Add your remote app to your local git repository

c:wkdirgit>heroku git:remote – a myapp

c:wkdirgit>git pull heroku master


How To Deploy To Heroku

The three most useful Heroku commands

1. Set up Git

Identify all files that got changed in your staging directory
c:wkdirgit
c:wkdirgit>git add –A

2. Commit changes

Commit those changes to the change set.
c:wkdirgit>git commit -m “commit for deploy to heroku”

3. Push it out

Now, push them to Heroku remote
c:wkdirgit>git push -f heroku master


Quick Test With a Simple PHP Pg

Just for fun (and to test), create a simple PHP/HTML5 page and copy it to the folder (c:wkdirgit) & push it to remote.

Here’s link to create the simplest PHP page:
http://php.net/manual/en/tutorial.firstpage.php

Then go to the following link to view your new page:
http://myappname.herokuapp.com/mypagename


More Useful Commands

Show all apps you have on Heroku

c:wkdirgit>heroku apps

What apps in my git repository

c:wkdirgit>git remote –v

Open a telnet like command connection to remote

Remember this gives a very convenient way to view your remote files. But any changes you make in the remote app through unix commands (rm, vi etc.) will not be persistent because of the ephemeral nature of Heroku storage. You will need to pull down the remote and delete the files locally and then push this change back.

Get a bash shell on Heroku
c:wkdirgit>heroku run bash –app my_remote_app
To view files in remote
c:wkdirgit>ls

Here’s how you can delete files on the remote

c:wkdirgit>git pull heroku master

This command pulls remote app content to local directory.
Now git has copied heroku remote app’s content to local folder – now you can delete all this content and push back the empty directory to heroku.

c:wkdirgit>git add -A
c:wkdirgit>git commit -m “commit for deploy to heroku”
c:wkdirgit>git push -f heroku master –force
Thus the above commands synced your empty local directory to remote app – now remote app is empty. This is the only way to delete the remote app directory.

Heroku Command Line vs. GUI Dashboard

You can also use commands to include add-ons like the MySQL database to your Heroku app. However, it’s easier to do it through the GUI on Heroku DX. Heroku DX is the developer experience dashboard you are presented with when you log into Heroku.