How does "git push heroku master" know where to push to and how to push to a different repo?

GitHeroku

Git Problem Overview


When pushing to a repository hosted on Heroku one must execute the following command:

git push heroku master

What do heroku and master indicate in this command? How does git know where to push to? (the git path)

Also, I didn't know I can use heroku rename to rename an app, so before, say I was using the app name trytryheroku and now I use heroku create real-thing but if I push, it still pushes to trytryheroku... is there a way to push to real-thing instead?

Git Solutions


Solution 1 - Git

The 'heroku' part is the name of the remote that you have setup - when you create a heroku app the first time it creates a git remote call 'heroku' pointing at your application - if you type 'git remote' within your project it will show you the remote endpoints. There's nothing locking you into using 'heroku' as the name of the remote - if you have multiple environments for your application you may have remotes named production or staging for example.

The 'master' part is the local branch you wish to push to the remote. If you develop in a feature branch for example named 'myfeature' and you want to deploy that to heroku you would do;

git push heroku myfeature:master

the additional :master here is saying push my local myfeature branch into the master branch on the remote - note: heroku can only deploy from the master branch.

If you rename an app the heroku git remote url will change - do a git remote -v which will show you the git repo your app is using, you will probably need to delete your old heroku origin and add the new one, git remote rm heroku then git remote add heroku git@newgitpathfromcontrolpanel

To learn more about Git I would recommend this book

Solution 2 - Git

PART 1 : "How does git know where to push to?"

Before executing the above mentioned command:

$ git push heroku master

There are always few other steps to execute: Installing Git and Heroku, creating a local Git repo, signing-up to heroku, log-in heroku via command-line, creating heroku handle to hosting point (explained in PART 2)

1. A local Git repository:

    $ git init
    Initialized empty Git repository in .git/
    $ git add .
    $ git commit -m "my first commit"
    Created initial commit 5df2d09: my first commit
     44 files changed, 8393 insertions(+), 0 deletions(-)
     create mode 100644 README
     create mode 100644 Procfile
     create mode 100644 app/controllers/source_file
    ...

2. Have sign-up(ed) for Heroku and logged-in via command-line:

$ heroku login
Enter your Heroku credentials.
Email: [email protected]
Password:
Could not find an existing public key.
Would you like to generate one? [Yn]
Generating new SSH public key.
Uploading ssh public key /Users/adam/.ssh/id_rsa.pub

So by running $ git push heroku master you have pushed the code/app to Heroku.


PART 2: but what does heroku and master indicate?

It is more of a Git question than Heroku - Heroku is a hosting platform, which depends on Git (Distributed Version Control System) for deployment.

The basic concept of 'push' is pushing some thing (file, app, ..) we have locally (in our working machine) to somewhere else, in this case to a remote repository (remote machine).

In Git before using 'push' we create a remote (handle) which acts as a reference to a remote repository (Complete URL), we do so using the following command:

$ git remote add <remote-name-of-our-choice> <URL-where-you-be-pushing-yourapp>

The basic structure of 'push' command is:

$ git push <remote-name> <branch>

So $ git push heroku master is actually pushing your code/app/file (from some local Git repo) to a remote repo 'heroku' .

wondering when this 'heroku' remote got created, it was added when you executed $ heroku create

$ heroku create
Creating stark-fog-398... done, stack is cedar
http://stark-fog-398.herokuapp.com/ | [email protected]:stark-fog-398.git
Git remote heroku added

Do notice the last line "Git remote heroku added".

to make it more clear, here's a Git command to check/output all the remotes: $ git remote -v will display something similar to the following

$ git remote -v
heroku     git@heroku.com:somerepo.git (fetch)
heroku     git@heroku.com:somerepo.git (push)

So we can assume that the following command was executed (implicitly) somewhere, when you did $ heroku create , hence creating the heroku remote to some heroku repo (url)*

$ git remote add heroku git@heroku.com:somerepo.git

Solution 3 - Git

heroku is required as part of the heroku gem to assist with the push, and master is simply the git branch you are pushing. The git knows where to push to because you create a heroku application the push is automatically setup, which you can see by typing

git remote -v

if you need to change that remove it with git remote rm heroku and then add yoru new application with git remote add heroku [email protected]:your-application-15.git

Solution 4 - Git

Other answers great for the first half of your question...

Here's the succinct answer to the second half.
via https://devcenter.heroku.com/articles/renaming-apps#updating-git-remotes</sub>

git remote rm heroku
heroku git:remote -a name-of-heroku-app

Solution 5 - Git

> but if I push, it still pushes to trytryheroku... is there a way to push to real-thing instead?

I found the answer you might be interested at heroku's:

https://dashboard.heroku.com/apps/NAMEOFYOURAPP/deploy/heroku-git

$ cd my-project/
$ git init
$ heroku git:remote -a nameofyourapp

$ git add .
$ git commit -am "make it better"
$ git push heroku master

That way, heroku will know where to push!

Solution 6 - Git

Just like you I also struggled to understand these nitty gritty of git and heroku and I was confused too. But now I have got bit of clarity to be able to answer your question in short.

Assuming you have git setup on your project directory. There exist a .git hidden folder in your project folder that contains a file named "config" this holds all information about remotes.

Remotes are your individual repositories named individually like origin, heroku, staging, prod etc..

In your command heroku stands for the repository you have mapped to heroku project. Open config file you will see the URL.

When you run

git push heroku master

you're telling git to push your current origin repository's master branch to heroku repository's master branch

Rest all details are already shared in other answers so donot want to repeat. So this is just a short answer per my understanding. Hope it helps.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSarah WongView Question on Stackoverflow
Solution 1 - GitJohn BeynonView Answer on Stackoverflow
Solution 2 - GitNabeel AhmedView Answer on Stackoverflow
Solution 3 - GitWill AydView Answer on Stackoverflow
Solution 4 - GitGollyJerView Answer on Stackoverflow
Solution 5 - GitiairView Answer on Stackoverflow
Solution 6 - GitSameerView Answer on Stackoverflow