-
Notifications
You must be signed in to change notification settings - Fork 3
rails git
Last updated 3 June 2012
Using GitHub with Rails. If you are building a throw-away app for your own education, you may decide not to set up git, but sooner or later you’ll need to learn to use git to participate in the open source community. When you are creating an app for deployment into production, collaborating with others, or simply need to roll back code changes as needed, git provides a source control repository.
This is a guide for developers using the example apps from the Rails Apps repository. Others may find it helpful as well.
If you arew new to git, you can read a free online version of the book Pro Git by Scott Chacon.
Check that git is installed on your computer:
$ git version
If you need to install git, refer to the section Installing Git from Scott Chacon’s book. Also see First-Time Git Setup from the book.
Michael Hartl’s Rails Tutorial book also covers Version Control with Git.
When you use the rails new
command, Rails creates a .gitignore file for you. You may want to modify it:
.bundle db/*.sqlite3 log/*.log tmp/ .DS_Store
The RailsApps project has a more extensive example .gitignore file you might want to examine.
You’ll need to do this if you have created a new Rails application. If you’ve created a Rails application using an application template from the Rails Apps project, the application template 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'
You can check your commit status at any time with:
$ ssh -T -p 443 git@ssh.github.com
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.
Check that your GitHub account is set up properly:
$ ssh git@github.com
Go to GitHub and create a new empty repository (http://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 -m "some helpful comment"
and then push it to the remote repository:
$ git push origin master
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.