Git & Heroku
Git
Although our app is still simple, we'll push it to a live website. To do so, you'll need to learn about git.
Git is version control software, which is what programmers use to track of changes in their code and collaborate with other developers. While git has many powerful features for collaborating on code, we're just going to cover a couple of git commands here.
Setting Git Up
The first time you use git, you should set up your name and email. By default, Cloud9 will set it to the name and email you provided when you signed up. If you want to change it, enter the following in the terminal:
git config --global user.name "Jim Jones"
git config --global user.email [email protected]
Replace "Jim Jones" with your name, and [email protected] with your email.
To initialize git in a folder for the first time, you would usually enter git init
. However, since you cloned this project, git has already been initialized.
To track (almost) everything with git, enter the following command:
git add .
Using git
We'll just cover 2 commands so you can use git to keep track of your changes. Whenever you complete coding a feature, enter these 2 commands:
git add .
- This will add all your changes to git.git commit -m "message about feature"
- This will commit your changes to git, along with a message that describes the feature. By committing to git, you're creating a permanent record of the code at this point, which you can later go back to.
We're assuming you're using a cloud IDE and not collaborating with anyone on this project, so right now you can just use git to track changes in your project. Later, you can learn about hosting your code on Github or Bitbucket and practice collaborating with other developers.
Heroku Setup
Heroku is web hosting service that is very easy to use with Rails applications. Follow these simple steps to get set up:
- Sign up for an account on Heroku.com.
- Enter
heroku login
in your terminal to login into Heroku. Enter your Heroku email and passwword to log on from the terminal.
Rails comes with the SQLite database, which doesn't require any special installation. Heroku uses a production-grade database known as PostgreSQL, so you need a special gem to connect your Rails app with Heroku. The project you cloned already came with the gems, but if you were starting a new project, you would need to add the following to the bottom of your gemfile:
group :production do
gem 'pg'
gem 'rails_12factor'
end
(You would also need to make sure that the line gem 'sqlite3'
was inside the development group of gems.)
After changing your gems, run bundle install
in the terminal.
- To create the heroku site, enter:
heroku create
This will create a Heroku application and return its name, such as https://mysterious-refuge-7866.herokuapp.com/
You won't find much if you go there, since your app hasn't been pushed yet.
Pushing to Heroku
When you finish working on a feature, you can commit it to git as above. Then to push the new application to Heroku, just enter the following command:
git push heroku master
This will push your application to Heroku where it will be setup to run on a live website. Heroku will automatically run bundle install
and generate production-versions of your CSS and Javascript files.
However, if you've created new database migrations, you'll need to run them on Heroku to update the production database. Once Heroku finishes setting up your app, enter the following command:
heroku run rake db:migrate
This will update the database so your site works. You can now open your site on your internet, and you can share it with the world!
More Heroku
Heroku provides a free tier for apps, though they recently added stricter limits to it. However, it's still good for getting started with.
Q: To enter my local development console, I just enter rails console
. How do I enter the production console on Heroku, so I can read or update the actual website data?
A: Enter heroku run console
in the terminal. You'll be able to do the same things you did in your terminal earlier, but with live website data.
Heroku also makes it easy to install add-ons and integrate them with your app. For example, you can install pgstudio to make it easy to browser through your app's database tables.
Challenge
Jim finished developing a new comment capability on his site and entered git add .
to add the changes to git. What command should he enter next, before he pushes his code to Heroku?
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
After entering the above command, Jim enters git push heroku master
to push his changes to Heroku. This seems to work well, but now Jim's production site is broken on pages that should allow comments. To fix the site, what should Jim likely enter?
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.