How can I push a part of my git repo to Heroku?
Asked Answered
A

4

68

I have a multi-module app that is already on Github. It is comprised of two modules, one of them an Android app and the other a Rails based Web app. So my project's directory structure is in the form of:

ProjectRoot
|
+-- web
|
+-- android
|
+-- .git

As such I cannot simply cd into ProjectRoot and push my app to Heroku as the root folder of the Rails app is ProjectRoot/web. Is there a way to push the web folder to Heroku? If I turn web into a git sub module, it should be easy to accomplish this, but then I only have 5 private repos on Git and I prefer to consume only 1 repo for my whole app.

Alejandrinaalejandro answered 12/5, 2011 at 11:27 Comment(1)
Possible duplicate of How can I deploy/push only a subdirectory of my git repo to Heroku?Ibidem
T
92

You can use git subtree push. It will generate a new commit tree with your directory as root, and push it.

git subtree push --prefix web heroku master

Full documentation is here.

Taylor answered 12/5, 2011 at 11:49 Comment(3)
This works great. What if you want to push a specific tag or branch in combination with subtree? Like 'git push heroku yourbranch:master'?Ziegfeld
In this case you cannot use the subtree push shortcut and have to chain commands youself. For example: git push heroku `git subtree split --prefix web yourbranch`:masterCastrate
FYI, git subtree is now part of core git (no need to install anything as long as you're git version is current)Dejadeject
I
21

The git subtree command (built in, now) is a good way to do this. If you want to push a subtree of a branch to become your master, you can use something like:

git push --force heroku `git subtree split --prefix web HEAD`:master

Inhumation answered 19/1, 2016 at 8:18 Comment(0)
S
4

You could also use git branches instead of subfolders. If you have git 1.7.2 or newer, you can simply do git checkout --orphan android to create a branch that is disconnected from your master branch (assumed here to be the web folder). Once you have checked out the orphan branch, run git rm -rf . to remove existing files before copying in your android-specific files to the now empty root directory.

If you want to use separate folders for each module, you can clone the repository twice and use this structure:

ProjectRoot
├── android
│   └── .git
└── web
    └── .git
Sachasachem answered 12/5, 2011 at 12:28 Comment(0)
A
0

This was a great find!!! I am doing it like this because the Heroku repo already existed.

git subtree split --prefix web -b web-branch; git push staging web-branch:main 

Also, the first time I had to do a forced push because the heroku had a different very outdated version.

Note: The temporal web-branch does not need to be removed before doing the split. Basically the split overwrites the branch. So it is great if you continue working on your normal repo and what to re-push again in the future.

Adame answered 29/5 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.