1.3 Version Control with Git
Now that we have a fresh and working Rails application, we'll take a moment for a step that, while technically optional, would be viewed by many Rails developers as practically essential, namely, placing our application source code under version control. Version control systems allow us to track changes to our project's code, collaborate more easily, and roll back any inadvertent errors (such as accidentally deleting files). Knowing how to use a version control system is a required skill for every software developer.
There are many options for version control, but the Rails community has largely standardized on Git, a distributed version control system originally developed by Linus Torvalds to host the Linux kernel. Git is a large subject, and we'll only be scratching the surface in this book, but there are many good free resources online; I especially recommend Pro Git by Scott Chacon (Apress, 2009). Putting your source code under version control with Git is strongly recommended, not only because it's nearly a universal practice in the Rails world, but also because it will allow you to share your code more easily (Section 1.3.4) and deploy your application right here in the first chapter (Section 1.4).
1.3.1 Installation and Setup
The first step is to install Git if you haven't yet followed the steps in Section 1.2.2. (As noted in that section, this involves following the instructions in the Installing Git section of Pro Git.)
First-Time System Setup
After installing Git, you should perform a set of one-time setup steps. These are system setups, meaning you only have to do them once per computer:
$ git config --global user.name "Your Name" $ git config --global user.email email@example.com
I also like to use co in place of the more verbose checkout command, which we can arrange as follows:
$ git config --global alias.co checkout
This tutorial will usually use the full checkout command, which works for systems that don't have co configured, but in real life I nearly always use git co to check out a project.
As a final setup step, you can optionally set the editor Git will use for commit messages. If you use a graphical editor such as TextMate, gVim, or MacVim, you need to use a flag to make sure that the editor stays attached to the shell instead of detaching immediately:17
$ git config --global core.editor "mate -w"
Replace "mate -w" with "gvim -f" for gVim or "mvim -f" for MacVim.
First-Time Repository Setup
Now we come to some steps that are necessary each time you create a new repository (which only happens once in this book, but is likely to happen again some day). First navigate to the root directory of the first app and initialize a new repository:
$ git init Initialized empty Git repository in /Users/mhartl/rails_projects/first_app/.git/
The next step is to add the project files to the repository. There's a minor complication, though: by default Git tracks the changes of all the files, but there are some files we don't want to track. For example, Rails creates log files to record the behavior of the application; these files change frequently, and we don't want our version control system to have to update them constantly. Git has a simple mechanism to ignore such files: simply include a file called .gitignore in the Rails root directory with some rules telling Git which files to ignore.
Looking again at Table 1.1, we see that the rails command creates a default .gitignore file in the Rails root directory, as shown in Listing 1.4.
Listing 1.4. The default .gitignore created by the rails command.
.bundle db/*.sqlite3 log/*.log tmp/**/*
Listing 1.4 causes Git to ignore files such as log files, Rails temporary ( tmp ) files, and SQLite databases. (For example, to ignore log files, which live in the log/ directory, we use log/*.log to ignore all files that end in .log .) Most of these ignored files change frequently and automatically, so including them under version control is inconvenient; moreover, when collaborating with others they can cause frustrating and irrelevant conflicts.
The .gitignore file in Listing 1.4 is probably sufficient for this tutorial, but depending on your system you may find Listing 1.5 more convenient. This augmented .gitignore arranges to ignore Rails documentation files, Vim and Emacs swap files, and (for OS X users) the weird .DS_Store directories created by the Mac Finder application. If you want to use this broader set of ignored files, open up .gitignore in your favorite text editor and fill it with the contents of Listing 1.5.
Listing 1.5. An augmented .gitignore file.
.bundle db/*.sqlite3* log/*.log *.log tmp/**/* tmp/* doc/api doc/app *.swp *~ .DS_Store
1.3.2 Adding and Committing
Finally, we'll add the files in your new Rails project to Git and then commit the results. You can add all the files (apart from those that match the ignore patterns in .gitignore ) as follows:18
$ git add .
Here the dot '.' represents the current directory, and Git is smart enough to add the files recursively, so it automatically includes all the subdirectories. This command adds the project files to a staging area, which contains pending changes to your project; you can see which files are in the staging area using the status command:19
$ git status # On branch master # # Initial commit # # Changes to be committed: # (use "git rm --cached <file>..." to unstage) # # new file: README # new file: Rakefile . . .
(The results are long, so I've used vertical dots to indicate omitted output.)
To tell Git you want to keep the changes, use the commit command:
$ git commit -m "Initial commit" [master (root-commit) df0a62f] Initial commit 42 files changed, 8461 insertions(+), 0 deletions(-) create mode 100644 README create mode 100644 Rakefile . . .
The -m flag lets you add a message for the commit; if you omit -m, Git will open the editor you set in Section 1.3.1 and have you enter the message there.
It is important to note that Git commits are local, recorded only on the machine on which the commits occur. This is in contrast to the popular open-source version control system called Subversion, in which a commit necessarily makes changes on a remote repository. Git divides a Subversion-style commit into its two logical pieces: a local recording of the changes ( git commit ) and a push of the changes up to a remote repository ( git push ). We'll see an example of the push step in Section 1.3.5.
By the way, you can see a list of your commit messages using the log command:
$ git log commit df0a62f3f091e53ffa799309b3e32c27b0b38eb4 Author: Michael Hartl <firstname.lastname@example.org> Date: Thu Oct 15 11:36:21 2009 -0700 Initial commit
To exit git log, you may have to type q to quit.
1.3.3 What Good Does Git Do You?
It's probably not entirely clear at this point why putting your source under version control does you any good, so let me give just one example. (We'll see many others in the chapters ahead.) Suppose you've made some accidental changes, such as (D'oh!) deleting the critical app/controllers/ directory:
$ ls app/controllers/ application_controller.rb $ rm -rf app/controllers/ $ ls app/controllers/ ls: app/controllers/: No such file or directory
Here we're using the Unix ls command to list the contents of the app/controllers/ directory and the rm command to remove it. The -rf flag means "recursive force", which recursively removes all files, directories, subdirectories, and so on, without asking for explicit confirmation of each deletion.
Let's check the status to see what's up:
$ git status # On branch master # Changed but not updated: # (use "git add/rm <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # deleted: app/controllers/application_controller.rb # no changes added to commit (use "git add" and/or "git commit -a")
We see here that a couple files have been deleted, but the changes are only on the "working tree"; they haven't been committed yet. This means we can still undo the changes easily by having Git check out the previous commit with the checkout command (and a -f flag to force overwriting the current changes):
$ git checkout -f $ git status # On branch master nothing to commit (working directory clean) $ ls app/controllers/ application_controller.rb
The missing directory and file are back. That's a relief!
Now that you've put your project under version control with Git, it's time to push your code up to GitHub, a social code site optimized for hosting and sharing Git repositories. Putting a copy of your Git repository at GitHub serves two purposes: it's a full backup of your code (including the full history of commits), and it makes any future collaboration much easier. This step is optional, but being a GitHub member will open the door to participating in a wide variety of Ruby and Rails projects (GitHub has high adoption rates in the Ruby and Rails communities, and in fact is itself written in Rails).
GitHub has a variety of paid plans, but for open source code their services are free, so sign up for a free GitHub account if you don't have one already. (You might have to read about SSH keys first.) After signing up, you'll see a page like the one in Figure 1.6. Click on create a repository and fill in the information as in Figure 1.7. After submitting the form, push up your first application as follows:
$ git remote add origin email@example.com:<username>/first_app.git $ git push origin master
Figure 1.6 The first GitHub page after account creation.
Figure 1.7 Creating the first app repository at GitHub.
These commands tell Git that you want to add GitHub as the origin for your main (master) branch and then push your repository up to GitHub. Of course, you should replace <username> with your actual username. For example, the command I ran for the railstutorial user was
$ git remote add origin firstname.lastname@example.org:railstutorial/first_app.git
The result is a page at GitHub for the first application repository, with file browsing, full commit history, and lots of other goodies (Figure 1.8).
Figure 1.8 A GitHub repository page.
1.3.5 Branch, Edit, Commit, Merge
If you've followed the steps in Section 1.3.4, you might notice that GitHub automatically shows the contents of the README file on the main repository page. In our case, since the project is a Rails application generated using the rails command, the README file is the one that comes with Rails (Figure 1.9). This isn't very helpful, so in this section we'll make our first edit by changing the README to describe our project rather than the Rails framework itself. In the process, we'll see a first example of the branch, edit, commit, merge workflow that I recommend using with Git.
Figure 1.9 The initial (rather useless) README file for our project at GitHub. (full size)
Git is incredibly good at making branches, which are effectively copies of a repository where we can make (possibly experimental) changes without modifying the parent files. In most cases, the parent repository is the master branch, and we can create a new topic branch by using checkout with the -b flag:
$ git checkout -b modify-README Switched to a new branch 'modify-README' $ git branch master * modify-README
Here the second command, git branch, just lists all the local branches, and the asterisk * identifies which branch we're currently on. Note that git checkout -b modify-README both creates a new branch and switches to it, as indicated by the asterisk in front of the modify-README branch. (If you set up the co alias in Section 1.3, you can use git co -b modify-README instead.)
The full value of branching only becomes clear when working on a project with multiple developers,20 but branches are helpful even for a single-developer tutorial such as this one. In particular, the master branch is insulated from any changes we make to the topic branch, so even if we really screw things up we can always abandon the changes by checking out the master branch and deleting the topic branch. We'll see how to do this at the end of the section.
By the way, for a change as small as this one I wouldn't normally bother with a new branch, but it's never too early to start practicing good habits.
After creating the topic branch, we'll edit it to make it a little more descriptive. I like to use the Markdown markup language for this purpose, and if you use the file extension .markdown then GitHub will automatically format it nicely for you. So, first we'll use Git's version of the Unix mv ("move") command to change the name, and then fill it in with the contents of Listing 1.6:
$ git mv README README.markdown $ mate README.markdown
Listing 1.6. The new README file, README.markdown.
# Ruby on Rails Tutorial: first application This is the first application for [*Ruby on Rails Tutorial: Learn Rails by Example*](http://railstutorial.org/) by [Michael Hartl](http://michaelhartl.com/).
With the changes made, we can take a look at the status of our branch:
$ git status # On branch modify-README # Changes to be committed: # (use "git reset HEAD <file>..." to unstage) # # renamed: README -> README.markdown # # Changed but not updated: # (use "git add <file>..." to update what will be committed) # (use "git checkout -- <file>..." to discard changes in working directory) # # modified: README.markdown #
At this point, we could use git add . as in Section 1.3.2, but Git provides the -a flag as a shortcut for the (very common) case of committing all modifications to existing files (or files created using git mv, which don't count as new files to Git):
$ git commit -a -m "Improved the README file" 2 files changed, 5 insertions(+), 243 deletions(-) delete mode 100644 README create mode 100644 README.markdown
Be careful about using the -a flag improperly; if you have added any new files to the project since the last commit, you still have to tell Git about them using git add first.
Now that we've finished making our changes, we're ready to merge the results back into our master branch:21
$ git checkout master Switched to branch 'master' $ git merge modify-README Updating 34f06b7..2c92bef Fast forward README | 243 ------------------------------------------------------- README.markdown | 5 + 2 files changed, 5 insertions(+), 243 deletions(-) delete mode 100644 README create mode 100644 README.markdown
Note that the Git output frequently includes things like 34f06b7, which are related to Git's internal representation of repositories. Your exact results will differ in these details, but otherwise should essentially match the output shown above.
After you've merged in the changes, you can tidy up your branches by deleting the topic branch using git branch -d if you're done with it:
$ git branch -d modify-README Deleted branch modify-README (was 2c92bef).
This step is optional, and in fact it's quite common to leave the topic branch intact. This way you can switch back and forth between the topic and master branches, merging in changes every time you reach a natural stopping point.
As mentioned above, it's also possible to abandon your topic branch changes, in this case with git branch -D:
# For illustration only; don't do this unless you mess up a branch $ git checkout -b topic-branch $ <really screw up the branch> $ git add . $ git commit -a -m "Screwed up" $ git checkout master $ git branch -D topic-branch
Unlike the -d flag, the -D flag will delete the branch even though we haven't merged in the changes.
Now that we've updated the README, we can push the changes up to GitHub to see the result:22
$ git push
As promised, GitHub nicely formats the new file using Markdown (Figure 1.10).
Figure 1.10 The improved README file formatted with Markdown. (full size)