-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrails-git.html
More file actions
276 lines (276 loc) · 22 KB
/
rails-git.html
File metadata and controls
276 lines (276 loc) · 22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Rails with Git and GitHub · RailsApps</title>
<meta name="viewport" content="width=device-width">
<link href="https://plus.google.com/117374718581973393536" rel="publisher">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.2.2/css/normalize.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.2.2/css/foundation.min.css">
<link rel="stylesheet" href="http://railsapps.github.io/css/railsapps.css" />
<link rel="stylesheet" href="http://railsapps.github.io/css/syntax.css" />
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/modernizr/2.6.2/modernizr.min.js"></script>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="http://platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
<script type="text/javascript">
window.analytics=window.analytics||[],window.analytics.methods=["identify","group","track","page","pageview","alias","ready","on","once","off","trackLink","trackForm","trackClick","trackSubmit"],window.analytics.factory=function(t){return function(){var a=Array.prototype.slice.call(arguments);return a.unshift(t),window.analytics.push(a),window.analytics}};for(var i=0;i<window.analytics.methods.length;i++){var key=window.analytics.methods[i];window.analytics[key]=window.analytics.factory(key)}window.analytics.load=function(t){if(!document.getElementById("analytics-js")){var a=document.createElement("script");a.type="text/javascript",a.id="analytics-js",a.async=!0,a.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.io/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(a,n)}},window.analytics.SNIPPET_VERSION="2.0.9",
window.analytics.load("l1stqfqqbf");
window.analytics.page();
window.analytics.ready(function () {
ga('require', 'linker');
ga('linker:autoLink', ['railscomposer.com','learn-rails.com','blog.railsapps.org','tutorials.railsapps.org']);
});
</script>
</head>
<body>
<div class="fixed">
<nav class="top-bar" data-topbar>
<ul class="title-area">
<li class="name">
<a href="http://railsapps.github.io/" class="brand">RailsApps Project</a>
</li>
<li class="toggle-topbar menu-icon"><a href="#"><span>Menu</span></a></li>
</ul>
<section class="top-bar-section">
<ul class="right">
<li><a href="https://tutorials.railsapps.org/" class="google">Tutorials</a></li>
<li><a href="http://twitter.com/rails_apps" class="twitter">Twitter</a></li>
<li><a href="http://blog.railsapps.org/" class="twitter">Blog</a></li>
<li><a href="https://github.com/RailsApps" class="github">GitHub Repository</a></li>
</ul>
</section>
</nav>
</div>
<div class="row">
<div class="large-12 columns">
<div class="content wikistyle gollum textile">
<h1>Rails with Git and GitHub</h1>
<h4>by Daniel Kehoe</h4>
<p><em>Last updated 4 September 2013</em></p>
<p>Using Git and 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. Get a <a href="https://github.com/">GitHub</a> account for remote backup and collaboration.</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>
<h4>If You Are New to Rails</h4>
<p>If you’re new to Rails, see <a href="http://railsapps.github.io/what-is-ruby-rails.html">What is Ruby on Rails?</a>, the book <a href="http://learn-rails.com/learn-ruby-on-rails.html">Learn Ruby on Rails</a>, and recommendations for a <a href="https://tutorials.railsapps.org/rails-tutorial">Rails tutorial</a>.</p>
<h4><a href="http://railsapps.github.io/"><img src="http://railsapps.github.io/images/join/join-railsapps.png" title="Join RailsApps" alt="Join RailsApps"></a></h4>
<h4>What is the RailsApps Project?</h4>
<p>This is an article from the RailsApps project. The <a href="http://railsapps.github.io/">RailsApps project</a> 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 <a href="http://railsapps.github.io/">join the RailsApps project</a>.</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>
<h2>Install Git</h2>
<p>Here are instructions if you need to install Git.</p>
<h3>Install Git on Mac OS X</h3>
<p>The easiest way to install Git is with <a href="http://mac.github.com/">GitHub for Mac</a>.</p>
<p>Before you download GitHub for Mac, get a <a href="https://github.com/signup/free">free GitHub account</a>. 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.</p>
<p>Download the <a href="http://mac.github.com/">GitHub for Mac</a> 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 <em>public key</em>. 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.</p>
<p>In the GitHub application, you’ll see this screen next:</p>
<p><img src="http://railsapps.github.io/images/installing-git.png" title="Installing Git" alt="Installing Git"></p>
<p>Before continuing, click the button to “Install Command Line Tools.”</p>
<p>The application will install the Git executable into the hidden <strong>/usr/local/bin/</strong> 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.</p>
<p>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.</p>
<h3>Install Git on Ubuntu Linux</h3>
<p>If Git isn’t installed on your Ubuntu system, use the apt-get package manager to install it:</p>
<pre>
$ sudo apt-get install git-core
</pre>
<p>You’ll need privileges as system administrator.</p>
<h3>Install Git on Windows</h3>
<p>Use the application from the <a href="http://msysgit.github.com/">msysGit project</a>.</p>
<p>It provides both a command line and <span class="caps">GUI</span> version.</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/rails-composer/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 the <a href="http://railsapps.github.io/rails-composer/">Rails Composer</a> tool (“like the ‘rails new’ command on steroids”), the program 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="https://github.com/repositories/new">https://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>
<div class="comments" id="comments">
<div class="content wikistyle gollum">
<h2>Comments</h2>
</div>
<p>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.</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="columns" -->
</div><!-- class="row" -->
<footer class="row">
<div class="large-12 columns">
<div class="row">
<div class="medium-4 large-4 columns">
<dl class="footer_nav">
<dt>RailsApps · Getting Started</dt>
<dd><a href="http://railsapps.github.io/ruby-and-rails.html">Ruby on Rails</a></dd>
<dd><a href="http://railsapps.github.io/what-is-ruby-rails.html">What is Ruby on Rails?</a></dd>
<dd><a href="http://learn-rails.com/learn-ruby-on-rails.html">Learn Ruby on Rails</a></dd>
<dd><a href="https://tutorials.railsapps.org/rails-tutorial">Rails Tutorial</a></dd>
<dd><a href="http://learn-rails.com/ruby-on-rails-tutorial-for-beginners">Ruby on Rails Tutorial for Beginners</a></dd>
<dd><a href="http://railsapps.github.io/installing-rails.html">Install Ruby on Rails</a></dd>
<dd><a href="http://railsapps.github.io/installrubyonrails-mac.html">Install Ruby on Rails - Mac OS X</a></dd>
<dd><a href="http://railsapps.github.io/installrubyonrails-ubuntu.html">Install Ruby on Rails - Ubuntu</a></dd>
<dd><a href="http://railsapps.github.io/rubyonrails-nitrous-io.html">Ruby on Rails - Nitrous.io</a></dd>
<dd><a href="http://railsapps.github.io/updating-rails.html">Update Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-composer/">Rails Composer</a></dd>
<dd><a href="http://railsapps.github.io/">Rails Examples</a></dd>
<dd><a href="http://railsapps.github.io/rails-examples-tutorials.html">Rails Starter Apps</a></dd>
</dl>
</div>
<div class="medium-4 large-4 columns">
<dl class="footer_nav">
<dt>RailsApps · Articles</dt>
<dd><a href="http://railsapps.github.io/rails-authorization.html">Rails Authorization</a></dd>
<dd><a href="http://railsapps.github.io/rails-google-analytics.html">Analytics for Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-heroku-tutorial.html">Heroku and Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-javascript-include-external.html">JavaScript and Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-environment-variables.html">Rails Environment Variables</a></dd>
<dd><a href="http://railsapps.github.io/rails-git.html">Git and Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-github.html">Rails GitHub</a></dd>
<dd><a href="http://railsapps.github.io/rails-send-email.html">Send Email with Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-haml.html">Haml and Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-default-application-layout.html">Rails Application Layout</a></dd>
<dd><a href="http://railsapps.github.io/rails-html5-boilerplate.html">HTML5 Boilerplate for Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-3-2-example-gemfile.html">Example Gemfiles for Rails</a></dd>
<dd><a href="http://railsapps.github.io/rails-application-templates.html">Rails Application Templates</a></dd>
<dd><a href="http://railsapps.github.io/rails-product-planning.html">Rails Product Planning</a></dd>
<dd><a href="http://railsapps.github.io/rails-project-management.html">Rails Project Management</a></dd>
</dl>
</div>
<div class="medium-4 large-4 columns">
<dl class="footer_nav">
<dt>RailsApps · Tutorials</dt>
<dd><a href="http://railsapps.github.io/twitter-bootstrap-rails.html">Rails Bootstrap</a></dd>
<dd><a href="http://railsapps.github.io/rails-foundation.html">Rails Foundation</a></dd>
<dd><a href="http://railsapps.github.io/rails-omniauth/">OmniAuth Tutorial</a></dd>
<dd><a href="http://railsapps.github.io/tutorial-rails-devise.html">Rails Devise Tutorial</a></dd>
<dd><a href="http://railsapps.github.io/tutorial-rails-devise-rspec-cucumber.html">Devise RSpec</a></dd>
<dd><a href="http://railsapps.github.io/tutorial-rails-bootstrap-devise-cancan.html">Devise Bootstrap</a></dd>
<dd><a href="http://railsapps.github.io/rails-devise-roles">Role-Based Authorization</a></dd>
<dd><a href="http://railsapps.github.io/rails-devise-pundit">Rails Authorization with Pundit</a></dd>
<dd><a href="https://tutorials.railsapps.org/rails-stripe-membership-saas">Rails Membership Site with Stripe</a></dd>
<dd><a href="https://tutorials.railsapps.org/rails-recurly-subscription-saas">Rails Subscription Site with Recurly</a></dd>
<dd><a href="https://tutorials.railsapps.org/rails-prelaunch-signup">Startup Prelaunch Signup Application</a></dd>
</dl>
<dl class="footer_nav">
<dt>RailsApps Profile</dt>
<dd><a href="https://plus.google.com/108039160165742774777?rel=author">Google</a></dd>
<dd><a href="https://plus.google.com/117374718581973393536" rel="publisher">Find us on Google+</a></dd>
</dl>
</div>
</div>
</div>
</footer>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/foundation/5.2.2/js/foundation.min.js"></script>
<script>
$(document).foundation();
</script>
</body>
</html>