69

So I understand that heroku functions as a git repository, but let's say i want to use github as well as a repository. How do I set it up such that I have two repositories and both are in sync?

0

6 Answers 6

101

You can have multiple remotes on a git installation. You would have a github remote, and a heroku remote.

Assuming you already have github setup, then you probably push to github with something like:

git push origin master

origin is your remote, and master is your branch.

Follow the instructions in getting started with Heroku choose your desired language and continue through the tutorial. This tutorial assumes that you already have github setup, and will show you how to create your heroku remote - via heroku create.

You then push to github as normal, and push to heroku via:

git push heroku master

The same format applies - heroku is your remote, and master is your branch. You are not overwriting your Github remote here, you are adding another, so you can still do both pushes via one commit with workflow such as:

git add .
git commit -m "Going to push to Heroku and Git"
git push origin master -- push to Github Master branch
git push heroku master -- push to Heroku
4
  • 11
    It's not "twice the work". It's two pushes accomplishing two completely separate actions. One pushes to Github and the other to Heroku. If you only need to push to one or the other, then you only need to make one push.
    – Dan Hoerst
    Commented Nov 8, 2013 at 16:53
  • 3
    @DanHoerst is it possible to have just one repository? I mean can we push to the heroku repo and have the github repo update itself? As in can we have a copy of the heroku repo on github?
    – aandis
    Commented Jun 5, 2014 at 19:28
  • 3
    @aandis Heroku now supports Github integration, allowing you to use only your Github repository if that is what you want: devcenter.heroku.com/articles/github-integration Commented Aug 2, 2016 at 22:11
  • @Annan Maybe you want to push to GitHub to have you repository ready to clone on another computer and keep working, but still is not ready to push to Heroku
    – fedeteka
    Commented Apr 19, 2019 at 12:57
41

If you want to be able to push and pull to multiple remotes:

First add them:

git remote add origin <github repo>
git remote add heroku [email protected]:<app name>.git

Then push

git push origin master
git push heroku master

If you want to push to both remotes at the same time:

Edit you configuration file so origin points to both heroku and github:

git config -e

Add/Replace:

[remote "origin"]
    url = [email protected]:username/somerepo.git
    url = ssh://[email protected]/username/somerepo.git

Since you are using github you can integrate with heroku by navigating to:

https://dashboard.heroku.com/apps/<app name>/settings#github-repo

and adding your repository's name.

github integration

If you want to automatically push to heroku after committing to GitHub:

you will need to use a continuous integration platform like TravisCI.

Here are the steps to make this work. Be careful what you push to production, make sure it works before it gets deployed. Each method has its pros and cons.

0
23

I think this is actually the recommended case; the Heroku git repository function is really for deployment and not code management.

Just use github to manage your code as usual, but additionally push to the Heroku git repository when you are ready to deploy. There is no need to keep them in sync with automated tools etc., because you want to be able to push to your github repository without deploying, for instance so that you can back up or collaborate on unfinished features or maintain separate staging and production environments.

2
  • 2
    You can use && as well to chain the commands i.e. git push origin master && git push heroku master. This will enable you to push on both platforms easily. Commented Jun 9, 2018 at 21:57
  • @Andrew Gorcester Nice explain. My case is the same, I work on 2 computers, so I push to GitHub and clone on another computer. Don't need to push to Heroku until something new is added to the app
    – fedeteka
    Commented Apr 19, 2019 at 13:00
17

I do this quite often. I create a site for Heroku but I want to keep my source in Github for archival purposes. I set up to remotes:

git remote add origin <github repo>

and

git remote add heroku <heroku repo>

Then you can just git push origin master and then git push heroku master. Heroku also allows you to associate a github repo for the purposes of seeing commit diffs.

3
  • 1
    shouldn't it be git remote add instead of git add remote? Commented Jan 3, 2014 at 21:02
  • 1
    I set up two remotes? Commented Dec 12, 2014 at 17:42
  • @charlesross yes, because one is for backup, sync and working (GitHub). Heroku is just for the app running
    – fedeteka
    Commented Apr 19, 2019 at 13:02
13

Since nobody mentioned that before. Git allows you now to add multiple urls to each remote. Just do it like this:

this will add fetch and push from github:

git remote add origin [email protected]:yourName/yourGithubRepo.git

this will override github push with heroku push:

git remote set-url origin --push --add [email protected]:yourHerokuRepo.git

this will re-add github push:

git remote set-url origin --push --add [email protected]:yourName/yourGithubRepo.git

and that's a final output:

$ git remote -v
origin  [email protected]:yourName/yourGithubRepo.git (fetch)
origin  [email protected]:yourHerokuRepo.git (push)
origin  [email protected]:yourName/yourGithubRepo.git (push)

After that just run:

git push

If, instead of working, it's saying sth about setting upstream, then type this first:

git push --set-upstream origin master 
2

If you don't want to manage two repositories and just the one at Github, here's how you can do that (Assuming that you have already created an Heroku app).

1) First, clone the Heroku repository to your local.

2) Then create a Github repo and push this local there.

3) Once that is done, use wercker

4) Goto "Add an Application" and fill in the details. They are fairly simple. Use the Github repository you just created.

5) After adding the application goto Setting and add a Deploy Target. Select heroku from the list. And then select the Heroku app you had created eariler and branches you would like to push.

That's it! You're done. Now your Github repository is in sync with your Heroku application. Anything you push to the Github repo using

git push origin master

will be automatically deployed to your Heroku application. This way you have your repository on Github to manage and you only have one repo to deal with. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.