Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
203 lines (193 sloc) 13.3 KB
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Rails with Git and GitHub &#183; RailsApps</title>
<link href="https://plus.google.com/u/0/b/117374718581973393536/117374718581973393536/posts/" rel="publisher" />
<link rel="stylesheet" href="http://railsapps.github.com/css/bootstrap.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/screen.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/gollum.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/site.css" type="text/css" charset="utf-8" />
<link rel="stylesheet" href="http://railsapps.github.com/css/syntax.css" type="text/css" charset="utf-8" />
<script src="http://code.jquery.com/jquery-1.6.min.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/jquery.text_selection-1.0.0.min.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/jquery.previewable_comment_form.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/jquery.tabs.js" type="text/javascript"></script>
<script src="http://railsapps.github.com/javascript/gollum.js" type="text/javascript"></script>
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-5109366-14']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="navbar navbar-fixed-top">
<div class="navbar-inner">
<div class="container">
<a href="http://railsapps.github.com/" class="brand">RailsApps Project</a>
<ul class="pull-right nav">
<li><a href="http://blog.railsapps.org/" class="twitter">Blog</a></li>
<li><a href="http://twitter.com/rails_apps" class="twitter">Twitter</a></li>
<li><a href="https://plus.google.com/117374718581973393536" class="google">Google +</a></li>
<li><a href="https://github.com/RailsApps" class="github">GitHub Repository</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<div class="content wikistyle gollum textile">
<h1>Rails with Git and GitHub</h1>
<h4>by Daniel Kehoe</h4>
<p><em>Last updated 5 July 2012</em></p>
<p>Using GitHub with Rails. <a href="http://git-scm.com/">Git</a> 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.</p>
<p>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 <a href="http://news.ycombinator.com/item?id=4199772">discussion</a>) but it is a worthwhile and essential tool for software development.</p>
<p>This is a guide for developers using the example apps from the <a href="http://railsapps.github.com/">Rails Apps</a> repository. Others may find it helpful as well.</p>
<h2>Git Explained</h2>
<p>You’ll use git to record the changes you make to your project over time. In git, these changes are called “commits.”</p>
<p>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.</p>
<h2>Learning Git</h2>
<p>If you are new to git, you can read a free online version of the book <a href="http://git-scm.com/book">Pro Git</a> by Scott Chacon.</p>
<p>GitHub offers a quick interactive tutorial at <a href="http://try.github.com/">try.github.com</a>.</p>
<p>The best git tutorial is <a href="http://gitimmersion.com/">Git Immersion</a> from EdgeCase.</p>
<p>I recommend reading Charles Duan’s article <a href="http://www.sbf5.com/~cduan/technical/git/">Understanding Git Conceptually</a> with any other tutorial.</p>
<p>Michael Hartl’s Rails Tutorial book covers <a href="http://ruby.railstutorial.org/ruby-on-rails-tutorial-book?version=3.2#sec:version_control">Version Control with Git</a> for typical Rails projects.</p>
<h2>Got Git?</h2>
<p>Check that git is installed on your computer:</p>
<pre>
$ git version
</pre>
<p>If you need to install git, refer to the section <a href="http://git-scm.com/book/en/Getting-Started-Installing-Git">Installing Git</a> from Scott Chacon’s book. Also see <a href="http://git-scm.com/book/en/Getting-Started-First-Time-Git-Setup">First-Time Git Setup</a> from the book.</p>
<h2>Git Config</h2>
<p>Check your git configuration parameters:</p>
<pre>
$ git config -l --global
user.name=Daniel Kehoe
user.email=daniel@danielkehoe.com
</pre>
<p>The email address will identify you to any services that use your git repo, such as GitHub and Heroku.</p>
<p>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).</p>
<h2>Ignore Files</h2>
<p>When you use the <code>rails new</code> command, Rails creates a <strong>.gitignore</strong> file for you in your application root directory. You may want to modify it:</p>
<pre>
.bundle
db/*.sqlite3
log/*.log
tmp/
.DS_Store
</pre>
<p>The RailsApps project has a more extensive <a href="https://github.com/RailsApps/rails3-application-templates/blob/master/files/gitignore.txt">example <strong>.gitignore</strong> file</a> you might want to examine.</p>
<h2>Initialize Git For Your Rails Application</h2>
<p>You’ll need to do this if you have created a new Rails application. If you’ve created a Rails application using an <a href="https://github.com/RailsApps/rails3-application-templates">application template</a> from the Rails Apps project, the application template will have done this for you already.</p>
<p>Be sure you are in your application’s root directory.</p>
<p>Initialize git and check in your first commit:</p>
<pre>
$ git init
$ git add .
$ git commit -m 'initial commit'
</pre>
<p>The <code>-m 'initial commit'</code> argument attaches a comment (“m” for “message”) to the commit. You should include a comment with every commit.</p>
<p>You can check your commit status at any time with:</p>
<pre>
$ git status
</pre>
<h2>Get a GitHub Account</h2>
<p>Use a GitHub repository if you want an offsite copy of your work or you plan to share your work with others.</p>
<p>Get a <a href="https://github.com/signup/free">free GitHub account</a> 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).</p>
<p>Check that your GitHub account is set up properly:</p>
<pre>
$ ssh -T -p 443 git@ssh.github.com
</pre>
<p>If your account is set up correctly, you’ll see a message:</p>
<pre>
Hi ...! You've successfully authenticated, but GitHub does not provide shell access.
</pre>
<p>The first time you connect, you will be warned (by ssh) that you have not previously connected with the host at ssh.github.com:</p>
<pre>
The authenticity of host '[ssh.github.com]:443 ...' can't be established.
Are you sure you want to continue connecting (yes/no)?
</pre>
<p>Unless you are the victim of a particularly insidious and unlikely exploit, you can safely answer “yes.”</p>
<p>If you see an error message such as <code>Host key verification failed</code>, review GitHub’s document <a href="http://help.github.com/set-up-git-redirect/">setting up Git and <span class="caps">SSH</span> keys</a> or the guide <a href="http://help.github.com/ssh-issues/">troubleshoot common <span class="caps">SSH</span> Problems</a>.</p>
<h2>Save it to GitHub</h2>
<p>Go to GitHub and create a new empty repository (<a href="http://github.com/repositories/new">http://github.com/repositories/new</a>) into which you can push your local git repo.</p>
<p>Add GitHub as a remote repository for your project and push your local project to the remote repository:</p>
<pre>
$ git remote add origin git@github.com:YOUR_GITHUB_ACCOUNT/YOUR_PROJECT_NAME.git
$ git push origin master
</pre>
<p>At each stage of completion, you should check your code into your local repository:</p>
<pre>
$ git commit -am "some helpful comment"
</pre>
<p>Note the <code>-am</code> argument. Use <code>-m</code> to add a commit message. Use <code>-a</code> 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 <code>-m</code> and <code>-a</code> as <code>-am</code>.</p>
<p>Then push your changes to the remote repository:</p>
<pre>
$ git push origin master
</pre>
<h2>Git Commit Messages</h2>
<p>If you’re working on a team, your colleagues will appreciate if you follow convention for the style of your commit messages:</p>
<ul>
<li>Limit the first line of the commit message to 50 characters.</li>
<li>Use the imperative tense for the commit message (“fix” not “fixed”).</li>
</ul><p>See an explanation and other suggestions: <a href="http://365git.tumblr.com/post/3308646748/writing-git-commit-messages">Writing Git commit messages</a> and <a href="http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html">A Note About Git Commit Messages</a>.</p>
<h2>Git Workflow</h2>
<p>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.</p>
<p>Here’s how to create a new git branch for a feature named “login”:</p>
<pre>
$ git checkout -b login
</pre>
<p>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).</p>
<p>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:</p>
<pre>
$ git checkout master
$ git merge --squash login
$ git commit -am "implement 'login' feature"
</pre>
<p>You can delete the working branch when you’re done:</p>
<pre>
$ git branch -D login
</pre>
<p>Experienced git user? Have a recommendation? Leave a comment below.</p>
</div><!-- class="content" -->
<div class="comments">
<div class="content wikistyle gollum">
<h2>Comments and Issues</h2>
</div>
<p>Is this helpful? Please add a comment below. Your encouragement fuels the project.</p>
<p>Did you find an error? Or couldn't get something to work? For the example apps and tutorials, please create a GitHub issue in the repository for the example app. Creating a GitHub issue is the best way to make sure a problem is investigated and fixed.</p>
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'railsapps'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = 'http://' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div><!-- class="comments" -->
<div class="footer row">
<div class="span4">
<h3>Credits</h3>
<p><a href="http://danielkehoe.com/">Daniel Kehoe</a> initiated the <a href="http://railsapps.github.com/">RailsApps Project</a>. Thanks to all the users and contributors.</p>
</div>
<div class="span4">
<h3>Wiki</h3>
<p>Corrections? Additions? You can edit this page <a href="https://github.com/RailsApps/railsapps.github.com/wiki/_pages">on the wiki</a>.</p>
</div>
<div class="span4">
<h3>Last edit</h3>
<p>by <b>Daniel Kehoe</b>, 2012-07-20 23:08:42</p>
</div>
</div>
</div>
</body>
</html>