Rails with Git and GitHub

by Daniel Kehoe

Last updated 4 September 2013

Using Git and GitHub with Rails. Git provides a source control repository. Use Git to roll back code changes as needed, when you are collaborating with others, and when you must deploy an app for hosting with a service such as Heroku. Get a GitHub account for remote backup and collaboration.

If you are building a throw-away app for your own education, you may decide not to use Git, but sooner or later you will need to learn to use Git to participate in the open source community. Git is not easy to understand or learn (see a discussion) but it is a worthwhile and essential tool for software development.

If You Are New to Rails

If you’re new to Rails, see What is Ruby on Rails?, the book Learn Ruby on Rails, and recommendations for a Rails tutorial.

Join RailsApps

What is the RailsApps Project?

This is an article from the RailsApps project. The RailsApps project provides example applications that developers use as starter apps. Hundreds of developers use the apps, report problems as they arise, and propose solutions. Rails changes frequently; each application is known to work and serves as your personal “reference implementation.” Each application is accompanied by a tutorial so there is no mystery code. Support for the project comes from subscribers. Please accept our invitation to join the RailsApps project.

Git Explained

You’ll use Git to record the changes you make to your project over time. In Git, these changes are called “commits.”

Commits contain a change a user has made to the code and some additional useful metadata. This metadata includes the time the change was made, your name, and a message you add to describe the change. Git itself is a specialized database that stores these commits. Git provides a complex set of commands that allow more than one developer to make changes and stay in sync; maintain and access different versions of a project; and retrieve past versions of a project.

Learning Git

If you are new to Git, you can read a free online version of the book Pro Git by Scott Chacon.

GitHub offers a quick interactive tutorial at try.github.com.

The best Git tutorial is Git Immersion from EdgeCase.

I recommend reading Charles Duan’s article Understanding Git Conceptually with any other tutorial.

Michael Hartl’s Rails Tutorial book covers Version Control with Git for typical Rails projects.

Got Git?

Check that Git is installed on your computer:

$ git version

Install Git

Here are instructions if you need to install Git.

Install Git on Mac OS X

The easiest way to install Git is with GitHub for Mac.

Before you download GitHub for Mac, get a free GitHub account. When you register, use your real name. Your GitHub account is as important as a résumé or a business card so use the name by which you are known professionally.

Download the GitHub for Mac application. Drag the application into your Applications folder. Launch the GitHub application to begin installation. You’ll be asked for your GitHub account username and password. The GitHub application will generate and install a public key. The public key is used to encrypt connections between your computer and GitHub. You’ll get an email message from GitHub informing you that a new public key was added to your GitHub account.

In the GitHub application, you’ll see this screen next:

Installing Git

Before continuing, click the button to “Install Command Line Tools.”

The application will install the Git executable into the hidden /usr/local/bin/ system folder. You’ll be prompted for your Mac login password to make the addition to your system files (the button is labeled “Install Helper”). The application will inform you that installation is complete. Clicking “Continue” will give you an option to identify local repositories (folders on your laptop that you want to back up to GitHub). Skip this step and select “Done.” You’ll see the GitHub for Mac main window.

You can quit GitHub for Mac. It is a graphical front-end for Git that you can use to become familiar with Git. Very few Rails developers use the GitHub for Mac application; since you will be using the Terminal to build Rails applications, it is easier to use Git from the command line. Installing GitHub for Mac is just the easiest way to install the Git executable.

Install Git on Ubuntu Linux

If Git isn’t installed on your Ubuntu system, use the apt-get package manager to install it:

$ sudo apt-get install git-core

You’ll need privileges as system administrator.

Install Git on Windows

Use the application from the msysGit project.

It provides both a command line and GUI version.

Git Config

Check your Git configuration parameters:

$ git config -l --global
user.name=Daniel Kehoe

The email address will identify you to any services that use your Git repo, such as GitHub and Heroku.

Life will be easier if you use the same email address for all services where you use Git (so sign up for GitHub and Heroku using the same address).

Ignore Files

When you use the rails new command, Rails creates a .gitignore file for you in your application root directory. You may want to modify it:


The RailsApps project has a more extensive example .gitignore file you might want to examine.

Initialize Git For Your Rails Application

You’ll need to do this if you have created a new Rails application. If you’ve created a Rails application using the Rails Composer tool (“like the ‘rails new’ command on steroids”), the program will have done this for you already.

Be sure you are in your application’s root directory.

Initialize Git and check in your first commit:

$ git init
$ git add .
$ git commit -m 'initial commit'

The -m 'initial commit' argument attaches a comment (“m” for “message”) to the commit. You should include a comment with every commit.

You can check your commit status at any time with:

$ git status

Get a GitHub Account

Use a GitHub repository if you want an offsite copy of your work or you plan to share your work with others.

Get a free GitHub account if you don’t already have one. It’s advisable to register using the email address that you’ve used to configure git locally (see “Git Config” above).

Check that your GitHub account is set up properly:

$ ssh -T -p 443 git@ssh.github.com

If your account is set up correctly, you’ll see a message:

Hi ...! You've successfully authenticated, but GitHub does not provide shell access.

The first time you connect, you will be warned (by ssh) that you have not previously connected with the host at ssh.github.com:

The authenticity of host '[ssh.github.com]:443 ...' can't be established.
Are you sure you want to continue connecting (yes/no)?

Unless you are the victim of a particularly insidious and unlikely exploit, you can safely answer “yes.”

If you see an error message such as Host key verification failed, review GitHub’s document setting up Git and SSH keys or the guide troubleshoot common SSH Problems.

Save it to GitHub

Go to GitHub and create a new empty repository (https://github.com/repositories/new) into which you can push your local git repo.

Add GitHub as a remote repository for your project and push your local project to the remote repository:

$ git remote add origin git@github.com:YOUR_GITHUB_ACCOUNT/YOUR_PROJECT_NAME.git
$ git push origin master

At each stage of completion, you should check your code into your local repository:

$ git commit -am "some helpful comment"

Note the -am argument. Use -m to add a commit message. Use -a to automatically remove any files you may have marked for deletion (otherwise you have to use “git rm,” adding an additional step). You can combine -m and -a as -am.

Then push your changes to the remote repository:

$ git push origin master

Git Commit Messages

If you’re working on a team, your colleagues will appreciate if you follow convention for the style of your commit messages:

  • Limit the first line of the commit message to 50 characters.
  • Use the imperative tense for the commit message (“fix” not “fixed”).

See an explanation and other suggestions: Writing Git commit messages and A Note About Git Commit Messages.

Git Workflow

When you are using Git for version control, you can commit every time you save a file, even for the tiniest typo fixes. If only you will ever see your Git commits, no one will care. But if you are working on a team, either commercially or as part of an open source project, you will drive your fellow programmers crazy if they try to follow your work and see such “granular” commits. Instead, get in the habit of creating a Git branch each time you begin work to implement a feature. When your new feature is complete, merge the branch and “squash” the commits so your comrades see just one commit for the entire feature.

Here’s how to create a new Git branch for a feature named “login”:

$ git checkout -b login

The command creates a new branch named “login” and switches to it, analogous to copying all your files to a new directory and moving to work in the new directory (though that is not really what happens with Git).

When the new feature is complete, merge the working branch to “master” and squash the commits so you have just one commit for the entire feature:

$ git checkout master
$ git merge --squash login
$ git commit -am "implement 'login' feature"

You can delete the working branch when you’re done:

$ git branch -D login

Experienced Git user? Have a recommendation? Leave a comment below.


Is this helpful? Your encouragement fuels the project. Please tweet or add a comment. Couldn't get something to work? For the example apps and tutorials, it's best to open an issue on GitHub so we can help you.

comments powered by Disqus