Recently I purchased an app project that uses the Heroku.com deployment and hosting services. I’m not a hardcore developer and have never used anything like this, or github, etc… So you can consider me a super-noob in this area. I tell you this because just like you, seasoned developer reading this, I found myself with nothing but bad code on my local machine and discovered that the original developer did not use any repository service and deployed using Dropbox instead, somewhere along the way his code developed a syntax error so all of his code on his local machine had the error included. There was only one plausible solution to this dilemma, I needed the last known good code from my Heroku based app.
Using Google, Bing, and DuckDuckGo I was unable to find a satisfactory answer to this problem. In fact many posts on StackOverflow and other forums either offered conflicting advice, were out right wrong, or claimed that doing this was impossible. Heroku’s help center wasn’t much better as I was unable to find an answer to this, I’m guessing because Heroku is not a git repository and therefore relies on the developer and/or new owner of an app (hi there!) to use a git repository or save good local copies of working code. I reached out to their support team and they were very helpful in responding, but spoke in a language I found hard to understand, eventually we got on the same page and I was able to get my code. I’ve decided to share a thorough and hopefully easier to understand guide for those of you seasoned and non-seasoned developers alike attempting to do this very same thing either because you lost your code or you’re the new owner of an app and do not have access to a good version of your app’s code.
Article Features
- ELI5 explanations included
- tl;dr at the bottom
- Note: the following instructions are for Windows 7+
How to Guide w/ ELI5: Getting an Older Version of Good Code For Your Heroku App
1. Download Heroku Toolbelt
Go to this link and download the Heroku Toolbelt client. This will allow you to run Heroku commands on your machine.
ELI5: Download this and you’ll be able to type commands into your command prompt window (that black box with text). You absolutely need this before moving on. Once you’ve downloaded and installed this go on to the next step.
2. Open command prompt (run as admin)
Find your command prompt launch icon or search for it. You might need to run as admin on Windows 7 or newer machine.
ELI5: You’re opening the black box you type text into as mentioned in step 1. In Windows versions through Windows 7 this is most likely found on your windows start menu. Click the Windows icon in your lower left hand corner or hit the windows key on your keyboard just to the left of your space bar often nestled between the Control and Alt keys. Find the search box, it’s probably the only box you can type text into on this bar.
In Windows 8 and up you can find this by navigating to the Start Screen by hitting the Windows key on your laptop. Then move your mouse up to the right hand corner where you see the magnifying glass icon.
Now that you have found the search box, the following instructions should match for most Windows versions: Now type into it “cmd” without quotations, You should see an icon of a small black screen that has the words “cmd” or “command” next to it. Right click on this and select “Run as Administrator” If you are logged in as an Admin the box will open up, if not it’ll prompt you to type in the Admin’s credentials and then open.
3. Ignore the money sign in the commands
Psst – If you know how to use the Heroku Toolbelt in the command prompt you don’t need to read this.
ELI5: If you checked out Heroku’s documentation or found something on a website like StackOverflow you might have seen code that looks like this:
$ heroku git:clone
The money sign is the terminal prompt in UNIX-based systems such as Mac and Linux, i.e. on those systems you have to start with that symbol or nothing will happen. Windoows, however, is not Unix based and therefore you can ignore this part of any command lines you find for the Heroku Toolbelt.
4. Switch to a directory where you want your code
Now that you’re in the command prompt you want to be in a directory where you want to download your app’s code to from Heroku.
cd\Folder1\Folder2\AppFolder
ELI5: The above command should take you to the folder where you want your code to go. Ideally you should pick an empty directory that is unique for this download. If you’re using Dropbox for Heroku deployment it’s probably a good idea to NOT put this code in there where you probably already have bad code or other stuff. Instead maybe create a directory in your Downloads or Documents folder. For example you could create a folder called “Heroku Apps” in your Downloads folder and place an empty folder in that one called “Working Code” where you’ll download your code to. If you have more than one app at the moment on Heroku you might want to name that second folder something else. Now you can actually copy paste this out of the folder window if you want to or just type it out. In this example you would type:
cd\Users\[Your-UserID]\Downloads\Heroku Apps\Working Code
5. Type this command replacing [my-app] with your app name
heroku git:clone -a [app-name]
ELI5: Type the above code into your command prompt, making sure you are in the directory where you want your last known good code to be. When you type the command make sure to replace [app-name] with the name of your app. You can find your apps official name in your Heroku dashboard, it most likely has no spaces in it so if you see a version with spaces and one with dashes, us the one with dashes. Before you execute the command make sure the command is typed correctly, the should be no brackets or spaces in the name of the app. Take one last glance to make sure the directory you are in is where you want your code. If everything looks good are ready to go. Once you type this command into your command prompt you must hit the ‘Enter’ key on your keyboard. This will take a little bit to work depending on the size of the app files and it will download the latest version of your app, but not the last known good version. That’s ok, we’re going to get that version of the app soon.
6. Go to Your Heroku Dashboard and Select Your App
Go to your Heroku Dashboard then select the app you’re trying to get the rollback code from.
ELI5: Now that you have the version of the code currently running on your Heroku account, we want to replace it with the last known good code. To do this we need to go back to the dashboard and navigate to the app. If you haven’t figured out how to do this by now I’m not sure how to help you. Maybe try logging out of Heroku and logging back in. You’ll know you’re at the app’s page when you see it’s name at the top of your screen with a star (yellow or grayed out) to the right and some navigation icons below it reading: Resources, Deploy, Metrics, Activity, Access, and Settings. Are you there? is this the right page? Good, go to step #7.
7. Go to Your App’s Activity Log
Click on “Activity” in the navigation at the top. This log shows you all of versions of the code that have been deployed, add-ons made, and rollbacks done on your app. We need to find a specific number here. If you know what a git ID is and you can find that here easily then skip to step #9
ELI5: Heroku is not like any host you’re probably used to with cPanel, Plesk, or FTP access. There is no way to simply or easily download your code. But they do keep cached versions of your old code commits and we can retrieve those. This is a stream of activity on versions of code that were deployed (meaning they became the official live version), items added onto the app’s account (think of them almost like WordPress plugins to extend the functionality of Heroku / your app), and rollbacks (this is when the new live code is not functioning and a developer wants to redeploy an older version). We’ll use the data in this stream to get our last known good code and download it.
8. Scroll to Find Last Known Good Version
ELI5:Hopefully you know or can figure out which one of your deployments has all of your good code in working order. If you do you should be able to see 2 identifiers for each code, a version number (i.e. v5) and a git id number (i.e. 7a9ie2 – I actually think this is called a commit or commit i.d. but not really sure). Note: Rollbacks don’t appear to impact the database, if you rollback too far or get code that is too old and the database structure no longer matches it could be a problem. I do not know how or discuss exporting the database on Heroku, if you need help with that open a ticket here on their support page.
Find the version that you want and then the associated git id or commit id number. The screenshots below should help identify the number you’re looking for. We’ll use it in the next step.
9. Now Type this Command
Type the code below to hard reset the code to the version you’re specifying with the commit id from the app’s activity stream on Heroku.
git reset --hard [get#]
ELI5: This is the moment you’ve been waiting for. This code will replace the code you downloaded earlier and update any files that changed to the working version you specified in the command using the git id number or commit id, whatever it’s called. If you’ve closed the command prompt window for some reason you’ll need to follow the instructions in steps #2, #3, and #4 to open it back up and get back to the directory where the app code is your cloned earlier.
10. The code in [your folder location] should now show an updated time and date stamp of the current time and date
You can tell this works by checking the difference in time / date stamps on the files in the folder. Some files will have the time stamp from when you ran the code to clone the files, others will have a time stamp from when you ran the previous command to perform a hard reset.
ELI5: If you did the above and did not get an error message in your command prompt box (that black box with text) close your command prompt box by clicking the red x in the upper right hand corner and open a directory window inside Windows. Now navigate to the folder where you cloned the code and then downloaded the working version to. You should see files with time updated of the same time you ran the command in step #9 and some with a different time that most likely correlates to when you ran the clone command in step #5. This is an indicator that your last command worked and updated all of the files it needed to to match the most recent version of your code that works. Congrats, you know have good working code to edit. If you’ve been reading the ELI5 portion of this, like me, you’ll probably want to take a few programming courses now to edit your app and make it better 🙂
tl;dr – Looking for just the bullet points? here you go
- Download the Heroku Toolbelt and install it on your computer
- Open a command prompt as an administrator
- Navigate in command prompt to a directory where you want to download the code
- Type this command: “heroku git:clone -a [app-name]” replacing [app-name] with your app’s name
- Login to Heroku and go to your apps dashboard
- Go to the Activity stream from your apps dashboard
- Find the commit id for the version of code you want to download
- Go back to your command prompt and make sure you’re in the right directory, the one where you ran the earlier clone command in step #4
- Type this command: “git reset –hard [git#]” replacing [git#] with the commit id of the code version you want
- Go to the directory you downloaded the code to inside of Windows and check to make sure the files downloaded correctly