---
# System prepended metadata

title: 'Git, Heroku'
tags: [heroku login, 'heroku app:create', git add ., heroku open, heroku, git init, git, Atom, git commit]

---

###### tags: `git` `git init` `git add .` `git commit` `heroku` `heroku login` `heroku login` `heroku app:create` `heroku open` `Atom`


# Git, Heroku 

---

## Repository
**directory versus Git repository** A directory is simply a place inside a file systems hierarchy or folders. It's just a generalized bucket or work space to dump files. Like a windows MyDocuments folder. It doesn't care what gets thrown in it, and doesn't pay much attention to how many others you dump in it, or if there's new versions (as it just saves over it).

A Git repository is a virtual storage of your project. It allows you to save versions of your code, which you can access when needed. 

---

## Git commmands
`git init` creates (i.e., initialises) a new repository. A new `.git` subdirectory will be created in your current working directory. [Setting up a repository](https://www.atlassian.com/git/tutorials/setting-up-a-repository)

`git add` tells Git that you want to include updates to a particular file in the next commit. However, `git add` doesn't really affect the repository in any significant way— changes are not actually recorded until you run `git commit`. You edit your files in the working directory. When you’re ready to save a copy of the current state of the project, you stage changes with `git add`.

`git commit` captures a snapshot of the project's currently staged changes. Committed snapshots can be thought of as “safe” versions of a project—Git will never change them unless you explicitly ask it to. Prior to the execution of git commit, The git add command is used to promote or 'stage' changes to the project that will be stored in a commit. [Git commit](https://www.atlassian.com/git/tutorials/saving-changes/git-commit)

`git commit -m "a message"` The `-m` flag is for the message (“added my github name”) on the commit - every commit needs a commit message [Git on the commandline](https://dont-be-afraid-to-commit.readthedocs.io/en/latest/git/commandlinegit.html).

`git push` is utilized to send the committed changes to a remote repository.

```bash!
# When you’re starting a new project, you create an initial commit of the current directory (specified by a dot) using the following two commands:
git add .
git commit

# Once you’ve got your project up-and-running, new files can be added by passing the path to git add:
git add newfile.py
git commit

# The command above simply changes one file. If you want to git add the entire project folder
cd  project-root-directory
git add .
git commit -am "My first revision"
git push heroku master
```

---

## Connect GitHub with ATOM

Create a new repository 'username/repository-name' (e.g., luenhchang/PhD_polygenic_risk_score_analysis) in GitHub

Create a new folder in a local computer (e.g., `D:\git\PhD-polygenic-risk-score-analysis`)

Copy the [link of the repo](https://github.com/luenhchang/PhD_polygenic_risk_score_analysis.git)
![](https://i.imgur.com/DbJWx80.png)


In Atom, bring up Command Palette in ATOM by  Ctrl+Shift+p and select `GitHub:Clone` [git-clone atom editor plugin](https://atom.io/packages/git-clone)

Add the repo link to **Clone from**

Add the folder link to **To directory** Keep this folder as empty until the Atom-Gitgun link is established.
![Git:Clone, outdated example](https://i.imgur.com/dHPcIKJ.png)

Hit Clone. This creates a `.git` folder in the local folder.

Right-click the folder> New File> Add a new file testing_git.py to the folder. The local folder and the newly added file are shown in green, indicating they are recognised by Atom as a GitHub repository

![recognised as a GitHub repository](https://i.imgur.com/PiiUzAn.png)

If your project panel is not shown up on the left sidebar, it maybe disabled by installed package [Toggle tree view](https://discuss.atom.io/t/how-to-reopen-the-sidebar-on-atom-sorry-for-stupid-question/15524/4)

Go to [GitHub for Atom login](https://github.atom.io/login). Log into GitHub and copy the token

In Atom, hit the GitHub icon. Paste the token. Note firewall (e.g., QIMR staff network) can block the authentication and give an error of `Failed to fetch`

![](https://i.imgur.com/K1cukw4.png)

Change the file testing_git.py and save it

Hit the 'Git' icon. You will see this file under 'Unstaged Changes' tab. Use the dropdown list to make sure the correct repository is selected.

Hit 'Stage All'

Enter text in the commit message box. The text reveals the main change or task (e.g. Testing Git)

Hit 'Commit to master' tab

Hit 'Push' tab. This tab will show 'Pushing', meaning the testing file is being pushed to the github repository

Check the files that have been pushed to the [repo](https://github.com/luenhchang/PhD_polygenic_risk_score_analysis). You should see 2 files there- README.md, testing_git.py

Add a new folder of files to the main folder and repeat the steps (1) Stage All type (2)'Adding references folder' to commit message box (3) commit to master (4) Push

Add another new folder using the same approach

reference: [How to connect Github with Atom - Easiest Way!](https://www.youtube.com/watch?v=6HsZMl-qV5k&t=317s)

---

## Clone a github repository
`git clone git_repository_URL` copies a respository from Github to your local folder  
[Copying a GitHub Repository to Your Local Computer](https://youtu.be/O72FWNeO-xY)
[Clone the Repository Using the Command Line](https://services.github.com/on-demand/github-cli/clone-repo-cli)

```bash!
# Launch Git Bash
# Change directory to a folder to receive the copy. Here the folder is D:\git
cd /d/git
# Copy a repository by URL
git clone https://github.com/plotly/dash-sample-apps
```

![git clone](https://i.imgur.com/fcz5i9x.png)


---

## Heroku commands
`heroku login` allows you to log into Heroku. A webpage will pop up with a button to click.
`heroku create` creates a new empty application on Heroku, along with an associated empty Git repository. If you run this command from your app’s root directory, the empty Heroku Git repository is automatically set as a remote for your local repository [Deploying with Git](https://devcenter.heroku.com/articles/git).

`heroku run` runs a different command-line (e.g., python shell, bash)
```bash!
# Run manage.py using python shell
heroku run python manage.py shell
#Python 3.7.3
#[GCC 7.3.0] on linux
#Type "help", "copyright", "credits" or "license" for more information.
#(InteractiveConsole)
#>>>

# Exit python shell
exit()

# Run bash
heroku run bash

# Exit bash
exit
```

`heroku config:set variable=value` creates configuration variable (config vars). Heroku lets you externalise configuration - storing data such as encryption keys or external resource addresses in config vars.

At runtime, config vars are exposed as environment variables to the application.  
```bash!
# Create a new config var called TIMES and assign value of 10 to it
heroku config:set TIMES=10

# View the created config var
heroku config
# === guarded-caverns-79024 Config Vars
# DATABASE_URL: postgres: //...
# TIMES: 10

# Repeat git add, git commit, git push and see the effect on deployed app
git add .
git commit -am "2nd revision"
git push heroku master
```

`heroku pg` shows postgreSQL database information. The example app you deployed already has database functionality, which you should be able to reach by visiting your app’s URL and appending /db. For example, if your app was deployed to https://wonderful-app-287.herokuapp.com/ then visit https://wonderful-app-287.herokuapp.com/db.

`heroku pg:psql` connects to the remote database and see all the rows:

`The local psql command could not be located` is because postgreSQL is not installed locally and the path hasn't been added to the Path environmental variable [The local psql command could not be located](https://stackoverflow.com/questions/15576064/the-local-psql-command-could-not-be-located). Installed postgreSQL locally following steps at [3.1 Installing With Stack Builder or StackBuilder Plus](https://www.enterprisedb.com/edb-docs/d/edb-postgres-replication-server/user-guides/user-guide/6.0/EDB_Postgres_Replication_Server_Users_Guide.1.13.html)
Added this to the Path variable
C:\Program Files\PostgreSQL\12

`heroku logs --tail --app your_app_name` checks errors from a particular app

---

## Heroku dyno
The Heroku Platform uses the container model to run and scale all Heroku apps. The containers used at Heroku are called “dynos.” Dynos are isolated, virtualized Linux containers that are designed to execute code based on a user-specified command. Your app can scale to any specified number of dynos based on its resource demands. Heroku’s container management capabilities provide you with an easy way to scale and manage the number, size, and type of dynos your app may need at any given time.

Dynos are the building blocks that power any Heroku app, from simple to sophisticated. Deploying to dynos, and relying on Heroku's dyno management, makes it easy for you to build and run flexible, scalable apps - freeing you from managing infrastructure, so you can focus on building and running great apps [Dynos: the heart of the Heroku platform](https://www.heroku.com/dynos).

---

## Heroku stack
A **stack** is an operating system image that is curated and maintained by Heroku. Stacks are typically based on an existing open-source Linux distribution, such as Ubuntu. Heroku applications target a specific stack, and buildpacks are responsible for transforming an app’s source code into an executable package that is compatible with that stack [Stacks](https://devcenter.heroku.com/articles/stack).

[Cedar is the Default Heroku Stack](https://blog.heroku.com/cedar_is_the_default_heroku_stack)

---

### Heroku errors

`'heroku' does not appear to be a git repository` was seen after running `git push heroku master` ['heroku' does not appear to be a git repository](https://stackoverflow.com/questions/18406721/heroku-does-not-appear-to-be-a-git-repository)

`ModuleNotFoundError: No module named 'run'` I have't resolved this issue but it seems that Procfile was the cause [Flask and Heroku - ModuleNotFoundError: No module named 'app'](https://stackoverflow.com/questions/48881293/flask-and-heroku-modulenotfounderror-no-module-named-app)

`Method Not Allowed` This error was seen after the app [chang-dashboard-pccrg-v2](https://git.heroku.com/chang-dashboard-pccrg-v2.git) was deployed. Rather than using the URL provided in the commandline, access the link "https://your-app-name.herokuapp.com/" So the correct link is https://chang-dashboard-pccrg-v2.herokuapp.com/

`remote: ERROR Could not find a version that satisfies the requirement iexfinance.stocks...` When this occurs, you will manually delete the python package that causes the error from the file requirements.txt [Heroku Upload - Could not find a version that satisfies the requirement anaconda-client==1.4.0](https://stackoverflow.com/questions/47304291/heroku-upload-could-not-find-a-version-that-satisfies-the-requirement-anaconda/48231679#48231679)

`remote: ! No such app as xxxx. fatal: repository 'http://git.heroku.com/cryptic-sierra-89062.git/' not found` See the problem description at [Pushing To Heroku Fails - No Such App Fatal](https://stackoverflow.com/questions/34379130/pushing-to-heroku-fails-no-such-app-fatal) and the solution at [Heroku error message no Cedar-supported app detected](https://stackoverflow.com/questions/31330587/heroku-error-message-no-cedar-supported-app-detected). The issue is resolved by 
```bash!
# Remove the .git folder in the current directory in cmd
rmdir .git /s
# Are you sure (Y/N)? type y

# Repeat the following
git init
# Initialized empty Git repository in D:/googleDrive/python-getting-started/.git

git add .

git commit -am "Reinitialize"
# [master (root-commit) 2a648fa] Reinitialise 
# 25 files changed, 491 insertions(+)

heroku create --stack cedar
# Creating app... done ? guarded-caverns-79024, stack is heroku-18 http://guarded-caverns-79024.herokuapp.com/ | https://git.heroku.com/guarded-caverns-79024.git

git push heroku master
# Enumerating objects: 31, done.
# ...
# remote: http://guarded-caverns-79024.herokuapp.com/ deployed to Heroku
# remote: Verifying deploy... done.
# To https://git.heroku.com/guarded-caverns-79024.git

# Copy this URL to a browser to see your app http://guarded-caverns-79024.herokuapp.com/
```

---

### Deploy a single HTML file to Heroku
This method works for plotly graphs that are saved as a single html file. Don't save a webpage from http://127.0.0.1:8050/. The html file created in that way is incompletely saved. It will just show 'loading.'
[How to Deploy a Static Site to Heroku](https://blog.teamtreehouse.com/deploy-static-site-heroku)
[Error: GitHub can’t show files that are this big right now - but the file is only 1.06 MB](https://stackoverflow.com/questions/48054238/error-github-can-t-show-files-that-are-this-big-right-now-but-the-file-is-onl)

launch `cmd.exe`
`heroku login`

![You are logged in on Heroku](https://i.imgur.com/jRmV4e3.png)

Change directory to the project folder. Make sure all the files are closed before you `git add .`

In the project folder, create an empty file. Copy `<?php header( 'Location: /boxplots.html' ) ;  ?>` to this file. Save the file as index.php. This php file will redirect heroku to open boxplots.html, which contains the plots to deploy to Heroku.

`git init` create a `.git` subfolder

`git add .` 

`git commit -m "deploying a html file to heroku"`

`heroku apps:create unique-app-name-across-heroku-platform` create an app and give it a name. The name must be (1) different from any existing app on Heroku, (2) all lower case letters. I have named this app chang-pccrg-dashboard-boxplots. Don't put the app name in quotes.

`git push heroku master` deploys the index.html file to heroku

`heroku open -a your-app-name` opens a webpage for your index.html file
The app [chang-pccrg-dashboard-boxplots](https://chang-pccrg-dashboard-boxplots.herokuapp.com/boxplots.html) was deployed and viewed by another user.

---

### Deploy a Python app to Heroku

**Procfile** Heroku apps include a Procfile that specifies the commands that are used when starting the app on Heroku. You can use a [Procfile to declare a variety of process types](https://devcenter.heroku.com/articles/procfile). 

Note `gunicorn` doesn't work on Windows so you'll want a Procfile.windows that will locally host your app in a way that doesn't require gunicorn (such as the way you would normally do it)[how to create Heroku procfile for windows?](https://stackoverflow.com/questions/25129958/how-to-create-heroku-procfile-for-windows).
```bash!
web: gunicorn gettingstarted.wsgi --log-file -
```
This declares a single process type, `web`, and the command needed to run it. The name web is important here. It declares that this process type will be attached to the HTTP routing stack of Heroku, and receive web traffic when deployed.

**Procfile.windows** The sample app has an additional Procfile for local development on Microsoft Windows, located in the file Procfile.windows. It starts a different web server, one that is compatible with Windows.
```bash!
web: python manage.py runserver 0.0.0.0:5000
```

**requirements.txt** file lists the app dependencies together. When an app is deployed, Heroku reads this file and installs the appropriate Python dependencies using the `pip install -r` command.

I haven't tried this [A gentle intro to Dash development](https://towardsdatascience.com/a-gentle-introduction-to-dash-development-and-deployment-f8b91990d3bd)

![workflow to develop the GitHub tutorial and above Heroku app](https://i.imgur.com/42dWbKY.png)

---


