Skip to content
This repository
Browse code

[mk] == Import from 'en' folder as a starting point for 'mk' folder ==

Signed-off-by: Viktor Mladenovski <>
  • Loading branch information...
commit a18bfc5535b03e8e7aa39fed49d8a57a21e8a20e 1 parent 2d9296e
Viktor Mladenovski mviktor authored
257 mk/01-introduction/01-chapter1.markdown
Source Rendered
... ... @@ -0,0 +1,257 @@
  1 +# Getting Started #
  2 +
  3 +This chapter will be about getting started with Git. We will begin at the beginning by explaining some background on version control tools, then move on to how to get Git running on your system and finally how to get it setup to start working with. At the end of this chapter you should understand why Git is around, why you should use it and you should be all setup to do so.
  4 +
  5 +## About Version Control ##
  6 +
  7 +What is version control, and why should you care? Version control is a system that records changes to a file or set of files over time so that you can recall specific versions later. For the examples in this book you will use software source code as the files being version controlled, though in reality you can do this with nearly any type of file on a computer.
  8 +
  9 +If you are a graphic or web designer and want to keep every version of an image or layout (which you would most certainly want to), a Version Control System (VCS) is a very wise thing to use. It allows you to revert files back to a previous state, revert the entire project back to a previous state, compare changes over time, see who last modified something that might be causing a problem, who introduced an issue and when, and more. Using a VCS also generally means that if you screw things up or lose files, you can easily recover. In addition, you get all this for very little overhead.
  10 +
  11 +### Local Version Control Systems ###
  12 +
  13 +Many people’s version-control method of choice is to copy files into another directory (perhaps a time-stamped directory, if they’re clever). This approach is very common because it is so simple, but it is also incredibly error prone. It is easy to forget which directory you’re in and accidentally write to the wrong file or copy over files you don’t mean to.
  14 +
  15 +To deal with this issue, programmers long ago developed local VCSs that had a simple database that kept all the changes to files under revision control (see Figure 1-1).
  16 +
  17 +Insert 18333fig0101.png
  18 +Figure 1-1. Local version control diagram.
  19 +
  20 +One of the more popular VCS tools was a system called rcs, which is still distributed with many computers today. Even the popular Mac OS X operating system includes the rcs command when you install the Developer Tools. This tool basically works by keeping patch sets (that is, the differences between files) from one change to another in a special format on disk; it can then re-create what any file looked like at any point in time by adding up all the patches.
  21 +
  22 +### Centralized Version Control Systems ###
  23 +
  24 +The next major issue that people encounter is that they need to collaborate with developers on other systems. To deal with this problem, Centralized Version Control Systems (CVCSs) were developed. These systems, such as CVS, Subversion, and Perforce, have a single server that contains all the versioned files, and a number of clients that check out files from that central place. For many years, this has been the standard for version control (see Figure 1-2).
  25 +
  26 +Insert 18333fig0102.png
  27 +Figure 1-2. Centralized version control diagram.
  28 +
  29 +This setup offers many advantages, especially over local VCSs. For example, everyone knows to a certain degree what everyone else on the project is doing. Administrators have fine-grained control over who can do what; and it’s far easier to administer a CVCS than it is to deal with local databases on every client.
  30 +
  31 +However, this setup also has some serious downsides. The most obvious is the single point of failure that the centralized server represents. If that server goes down for an hour, then during that hour nobody can collaborate at all or save versioned changes to anything they’re working on. If the hard disk the central database is on becomes corrupted, and proper backups haven’t been kept, you lose absolutely everything—the entire history of the project except whatever single snapshots people happen to have on their local machines. Local VCS systems suffer from this same problem—whenever you have the entire history of the project in a single place, you risk losing everything.
  32 +
  33 +### Distributed Version Control Systems ###
  34 +
  35 +This is where Distributed Version Control Systems (DVCSs) step in. In a DVCS (such as Git, Mercurial, Bazaar or Darcs), clients don’t just check out the latest snapshot of the files: they fully mirror the repository. Thus if any server dies, and these systems were collaborating via it, any of the client repositories can be copied back up to the server to restore it. Every checkout is really a full backup of all the data (see Figure 1-3).
  36 +
  37 +Insert 18333fig0103.png
  38 +Figure 1-3. Distributed version control diagram.
  39 +
  40 +Furthermore, many of these systems deal pretty well with having several remote repositories they can work with, so you can collaborate with different groups of people in different ways simultaneously within the same project. This allows you to set up several types of workflows that aren’t possible in centralized systems, such as hierarchical models.
  41 +
  42 +## A Short History of Git ##
  43 +
  44 +As with many great things in life, Git began with a bit of creative destruction and fiery controversy. The Linux kernel is an open source software project of fairly large scope. For most of the lifetime of the Linux kernel maintenance (1991–2002), changes to the software were passed around as patches and archived files. In 2002, the Linux kernel project began using a proprietary DVCS system called BitKeeper.
  45 +
  46 +In 2005, the relationship between the community that developed the Linux kernel and the commercial company that developed BitKeeper broke down, and the tool’s free-of-charge status was revoked. This prompted the Linux development community (and in particular Linus Torvalds, the creator of Linux) to develop their own tool based on some of the lessons they learned while using BitKeeper. Some of the goals of the new system were as follows:
  47 +
  48 +* Speed
  49 +* Simple design
  50 +* Strong support for non-linear development (thousands of parallel branches)
  51 +* Fully distributed
  52 +* Able to handle large projects like the Linux kernel efficiently (speed and data size)
  53 +
  54 +Since its birth in 2005, Git has evolved and matured to be easy to use and yet retain these initial qualities. It’s incredibly fast, it’s very efficient with large projects, and it has an incredible branching system for non-linear development (See Chapter 3).
  55 +
  56 +## Git Basics ##
  57 +
  58 +So, what is Git in a nutshell? This is an important section to absorb, because if you understand what Git is and the fundamentals of how it works, then using Git effectively will probably be much easier for you. As you learn Git, try to clear your mind of the things you may know about other VCSs, such as Subversion and Perforce; doing so will help you avoid subtle confusion when using the tool. Git stores and thinks about information much differently than these other systems, even though the user interface is fairly similar; understanding those differences will help prevent you from becoming confused while using it.
  59 +
  60 +### Snapshots, Not Differences ###
  61 +
  62 +The major difference between Git and any other VCS (Subversion and friends included) is the way Git thinks about its data. Conceptually, most other systems store information as a list of file-based changes. These systems (CVS, Subversion, Perforce, Bazaar, and so on) think of the information they keep as a set of files and the changes made to each file over time, as illustrated in Figure 1-4.
  63 +
  64 +Insert 18333fig0104.png
  65 +Figure 1-4. Other systems tend to store data as changes to a base version of each file.
  66 +
  67 +Git doesn’t think of or store its data this way. Instead, Git thinks of its data more like a set of snapshots of a mini filesystem. Every time you commit, or save the state of your project in Git, it basically takes a picture of what all your files look like at that moment and stores a reference to that snapshot. To be efficient, if files have not changed, Git doesn’t store the file again—just a link to the previous identical file it has already stored. Git thinks about its data more like Figure 1-5.
  68 +
  69 +Insert 18333fig0105.png
  70 +Figure 1-5. Git stores data as snapshots of the project over time.
  71 +
  72 +This is an important distinction between Git and nearly all other VCSs. It makes Git reconsider almost every aspect of version control that most other systems copied from the previous generation. This makes Git more like a mini filesystem with some incredibly powerful tools built on top of it, rather than simply a VCS. We’ll explore some of the benefits you gain by thinking of your data this way when we cover Git branching in Chapter 3.
  73 +
  74 +### Nearly Every Operation Is Local ###
  75 +
  76 +Most operations in Git only need local files and resources to operate – generally no information is needed from another computer on your network. If you’re used to a CVCS where most operations have that network latency overhead, this aspect of Git will make you think that the gods of speed have blessed Git with unworldly powers. Because you have the entire history of the project right there on your local disk, most operations seem almost instantaneous.
  77 +
  78 +For example, to browse the history of the project, Git doesn’t need to go out to the server to get the history and display it for you—it simply reads it directly from your local database. This means you see the project history almost instantly. If you want to see the changes introduced between the current version of a file and the file a month ago, Git can look up the file a month ago and do a local difference calculation, instead of having to either ask a remote server to do it or pull an older version of the file from the remote server to do it locally.
  79 +
  80 +This also means that there is very little you can’t do if you’re offline or off VPN. If you get on an airplane or a train and want to do a little work, you can commit happily until you get to a network connection to upload. If you go home and can’t get your VPN client working properly, you can still work. In many other systems, doing so is either impossible or painful. In Perforce, for example, you can’t do much when you aren’t connected to the server; and in Subversion and CVS, you can edit files, but you can’t commit changes to your database (because your database is offline). This may not seem like a huge deal, but you may be surprised what a big difference it can make.
  81 +
  82 +### Git Has Integrity ###
  83 +
  84 +Everything in Git is check-summed before it is stored and is then referred to by that checksum. This means it’s impossible to change the contents of any file or directory without Git knowing about it. This functionality is built into Git at the lowest levels and is integral to its philosophy. You can’t lose information in transit or get file corruption without Git being able to detect it.
  85 +
  86 +The mechanism that Git uses for this checksumming is called a SHA-1 hash. This is a 40-character string composed of hexadecimal characters (0–9 and a–f) and calculated based on the contents of a file or directory structure in Git. A SHA-1 hash looks something like this:
  87 +
  88 + 24b9da6552252987aa493b52f8696cd6d3b00373
  89 +
  90 +You will see these hash values all over the place in Git because it uses them so much. In fact, Git stores everything not by file name but in the Git database addressable by the hash value of its contents.
  91 +
  92 +### Git Generally Only Adds Data ###
  93 +
  94 +When you do actions in Git, nearly all of them only add data to the Git database. It is very difficult to get the system to do anything that is not undoable or to make it erase data in any way. As in any VCS, you can lose or mess up changes you haven’t committed yet; but after you commit a snapshot into Git, it is very difficult to lose, especially if you regularly push your database to another repository.
  95 +
  96 +This makes using Git a joy because we know we can experiment without the danger of severely screwing things up. For a more in-depth look at how Git stores its data and how you can recover data that seems lost, see “Under the Covers” in Chapter 9.
  97 +
  98 +### The Three States ###
  99 +
  100 +Now, pay attention. This is the main thing to remember about Git if you want the rest of your learning process to go smoothly. Git has three main states that your files can reside in: committed, modified, and staged. Committed means that the data is safely stored in your local database. Modified means that you have changed the file but have not committed it to your database yet. Staged means that you have marked a modified file in its current version to go into your next commit snapshot.
  101 +
  102 +This leads us to the three main sections of a Git project: the Git directory, the working directory, and the staging area.
  103 +
  104 +Insert 18333fig0106.png
  105 +Figure 1-6. Working directory, staging area, and git directory.
  106 +
  107 +The Git directory is where Git stores the metadata and object database for your project. This is the most important part of Git, and it is what is copied when you clone a repository from another computer.
  108 +
  109 +The working directory is a single checkout of one version of the project. These files are pulled out of the compressed database in the Git directory and placed on disk for you to use or modify.
  110 +
  111 +The staging area is a simple file, generally contained in your Git directory, that stores information about what will go into your next commit. It’s sometimes referred to as the index, but it’s becoming standard to refer to it as the staging area.
  112 +
  113 +The basic Git workflow goes something like this:
  114 +
  115 +1. You modify files in your working directory.
  116 +2. You stage the files, adding snapshots of them to your staging area.
  117 +3. You do a commit, which takes the files as they are in the staging area and stores that snapshot permanently to your Git directory.
  118 +
  119 +If a particular version of a file is in the git directory, it’s considered committed. If it’s modified but has been added to the staging area, it is staged. And if it was changed since it was checked out but has not been staged, it is modified. In Chapter 2, you’ll learn more about these states and how you can either take advantage of them or skip the staged part entirely.
  120 +
  121 +## Installing Git ##
  122 +
  123 +Let’s get into using some Git. First things first—you have to install it. You can get it a number of ways; the two major ones are to install it from source or to install an existing package for your platform.
  124 +
  125 +### Installing from Source ###
  126 +
  127 +If you can, it’s generally useful to install Git from source, because you’ll get the most recent version. Each version of Git tends to include useful UI enhancements, so getting the latest version is often the best route if you feel comfortable compiling software from source. It is also the case that many Linux distributions contain very old packages; so unless you’re on a very up-to-date distro or are using backports, installing from source may be the best bet.
  128 +
  129 +To install Git, you need to have the following libraries that Git depends on: curl, zlib, openssl, expat, and libiconv. For example, if you’re on a system that has yum (such as Fedora) or apt-get (such as a Debian based system), you can use one of these commands to install all of the dependencies:
  130 +
  131 + $ yum install curl-devel expat-devel gettext-devel \
  132 + openssl-devel zlib-devel
  133 +
  134 + $ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
  135 + libz-dev
  136 +
  137 +When you have all the necessary dependencies, you can go ahead and grab the latest snapshot from the Git web site:
  138 +
  139 +
  140 +
  141 +Then, compile and install:
  142 +
  143 + $ tar -zxf git-
  144 + $ cd git-
  145 + $ make prefix=/usr/local all
  146 + $ sudo make prefix=/usr/local install
  147 +
  148 +After this is done, you can also get Git via Git itself for updates:
  149 +
  150 + $ git clone git://
  151 +
  152 +### Installing on Linux ###
  153 +
  154 +If you want to install Git on Linux via a binary installer, you can generally do so through the basic package-management tool that comes with your distribution. If you’re on Fedora, you can use yum:
  155 +
  156 + $ yum install git-core
  157 +
  158 +Or if you’re on a Debian-based distribution like Ubuntu, try apt-get:
  159 +
  160 + $ apt-get install git-core
  161 +
  162 +### Installing on Mac ###
  163 +
  164 +There are two easy ways to install Git on a Mac. The easiest is to use the graphical Git installer, which you can download from the Google Code page (see Figure 1-7):
  165 +
  166 +
  167 +
  168 +Insert 18333fig0107.png
  169 +Figure 1-7. Git OS X installer.
  170 +
  171 +The other major way is to install Git via MacPorts (``). If you have MacPorts installed, install Git via
  172 +
  173 + $ sudo port install git-core +svn +doc +bash_completion +gitweb
  174 +
  175 +You don’t have to add all the extras, but you’ll probably want to include +svn in case you ever have to use Git with Subversion repositories (see Chapter 8).
  176 +
  177 +### Installing on Windows ###
  178 +
  179 +Installing Git on Windows is very easy. The msysGit project has one of the easier installation procedures. Simply download the installer exe file from the Google Code page, and run it:
  180 +
  181 +
  182 +
  183 +After it’s installed, you have both a command-line version (including an SSH client that will come in handy later) and the standard GUI.
  184 +
  185 +## First-Time Git Setup ##
  186 +
  187 +Now that you have Git on your system, you’ll want to do a few things to customize your Git environment. You should have to do these things only once; they’ll stick around between upgrades. You can also change them at any time by running through the commands again.
  188 +
  189 +Git comes with a tool called git config that lets you get and set configuration variables that control all aspects of how Git looks and operates. These variables can be stored in three different places:
  190 +
  191 +* `/etc/gitconfig` file: Contains values for every user on the system and all their repositories. If you pass the option` --system` to `git config`, it reads and writes from this file specifically.
  192 +* `~/.gitconfig` file: Specific to your user. You can make Git read and write to this file specifically by passing the `--global` option.
  193 +* config file in the git directory (that is, `.git/config`) of whatever repository you’re currently using: Specific to that single repository. Each level overrides values in the previous level, so values in `.git/config` trump those in `/etc/gitconfig`.
  194 +
  195 +On Windows systems, Git looks for the `.gitconfig` file in the `$HOME` directory (`C:\Documents and Settings\$USER` for most people). It also still looks for /etc/gitconfig, although it’s relative to the MSys root, which is wherever you decide to install Git on your Windows system when you run the installer.
  196 +
  197 +### Your Identity ###
  198 +
  199 +The first thing you should do when you install Git is to set your user name and e-mail address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you pass around:
  200 +
  201 + $ git config --global "John Doe"
  202 + $ git config --global
  203 +
  204 +Again, you need to do this only once if you pass the `--global` option, because then Git will always use that information for anything you do on that system. If you want to override this with a different name or e-mail address for specific projects, you can run the command without the `--global` option when you’re in that project.
  205 +
  206 +### Your Editor ###
  207 +
  208 +Now that your identity is set up, you can configure the default text editor that will be used when Git needs you to type in a message. By default, Git uses your system’s default editor, which is generally Vi or Vim. If you want to use a different text editor, such as Emacs, you can do the following:
  209 +
  210 + $ git config --global core.editor emacs
  211 +
  212 +### Your Diff Tool ###
  213 +
  214 +Another useful option you may want to configure is the default diff tool to use to resolve merge conflicts. Say you want to use vimdiff:
  215 +
  216 + $ git config --global merge.tool vimdiff
  217 +
  218 +Git accepts kdiff3, tkdiff, meld, xxdiff, emerge, vimdiff, gvimdiff, ecmerge, and opendiff as valid merge tools. You can also set up a custom tool; see Chapter 7 for more information about doing that.
  219 +
  220 +### Checking Your Settings ###
  221 +
  222 +If you want to check your settings, you can use the `git config --list` command to list all the settings Git can find at that point:
  223 +
  224 + $ git config --list
  225 + Chacon
  226 +
  227 + color.status=auto
  228 + color.branch=auto
  229 + color.interactive=auto
  230 + color.diff=auto
  231 + ...
  232 +
  233 +You may see keys more than once, because Git reads the same key from different files (`/etc/gitconfig` and `~/.gitconfig`, for example). In this case, Git uses the last value for each unique key it sees.
  234 +
  235 +You can also check what Git thinks a specific key’s value is by typing `git config {key}`:
  236 +
  237 + $ git config
  238 + Scott Chacon
  239 +
  240 +## Getting Help ##
  241 +
  242 +If you ever need help while using Git, there are three ways to get the manual page (manpage) help for any of the Git commands:
  243 +
  244 + $ git help <verb>
  245 + $ git <verb> --help
  246 + $ man git-<verb>
  247 +
  248 +For example, you can get the manpage help for the config command by running
  249 +
  250 + $ git help config
  251 +
  252 +These commands are nice because you can access them anywhere, even offline.
  253 +If the manpages and this book aren’t enough and you need in-person help, you can try the `#git` or `#github` channel on the Freenode IRC server ( These channels are regularly filled with hundreds of people who are all very knowledgeable about Git and are often willing to help.
  254 +
  255 +## Summary ##
  256 +
  257 +You should have a basic understanding of what Git is and how it’s different from the CVCS you may have been using. You should also now have a working version of Git on your system that’s set up with your personal identity. It’s now time to learn some Git basics.
1,121 mk/02-git-basics/01-chapter2.markdown
Source Rendered
... ... @@ -0,0 +1,1121 @@
  1 +# Git Basics #
  2 +
  3 +If you can read only one chapter to get going with Git, this is it. This chapter covers every basic command you need to do the vast majority of the things you’ll eventually spend your time doing with Git. By the end of the chapter, you should be able to configure and initialize a repository, begin and stop tracking files, and stage and commit changes. We’ll also show you how to set up Git to ignore certain files and file patterns, how to undo mistakes quickly and easily, how to browse the history of your project and view changes between commits, and how to push and pull from remote repositories.
  4 +
  5 +## Getting a Git Repository ##
  6 +
  7 +You can get a Git project using two main approaches. The first takes an existing project or directory and imports it into Git. The second clones an existing Git repository from another server.
  8 +
  9 +### Initializing a Repository in an Existing Directory ###
  10 +
  11 +If you’re starting to track an existing project in Git, you need to go to the project’s directory and type
  12 +
  13 + $ git init
  14 +
  15 +This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet. (See Chapter 9 for more information about exactly what files are contained in the `.git` directory you just created.)
  16 +
  17 +If you want to start version-controlling existing files (as opposed to an empty directory), you should probably begin tracking those files and do an initial commit. You can accomplish that with a few git add commands that specify the files you want to track, followed by a commit:
  18 +
  19 + $ git add *.c
  20 + $ git add README
  21 + $ git commit –m 'initial project version'
  22 +
  23 +We’ll go over what these commands do in just a minute. At this point, you have a Git repository with tracked files and an initial commit.
  24 +
  25 +### Cloning an Existing Repository ###
  26 +
  27 +If you want to get a copy of an existing Git repository — for example, a project you’d like to contribute to — the command you need is git clone. If you’re familiar with other VCS systems such as Subversion, you’ll notice that the command is clone and not checkout. This is an important distinction — Git receives a copy of nearly all data that the server has. Every version of every file for the history of the project is pulled down when you run `git clone`. In fact, if your server disk gets corrupted, you can use any of the clones on any client to set the server back to the state it was in when it was cloned (you may lose some server-side hooks and such, but all the versioned data would be there—see Chapter 4 for more details).
  28 +
  29 +You clone a repository with `git clone [url]`. For example, if you want to clone the Ruby Git library called Grit, you can do so like this:
  30 +
  31 + $ git clone git://
  32 +
  33 +That creates a directory named "grit", initializes a `.git` directory inside it, pulls down all the data for that repository, and checks out a working copy of the latest version. If you go into the new `grit` directory, you’ll see the project files in there, ready to be worked on or used. If you want to clone the repository into a directory named something other than grit, you can specify that as the next command-line option:
  34 +
  35 + $ git clone git:// mygrit
  36 +
  37 +That command does the same thing as the previous one, but the target directory is called mygrit.
  38 +
  39 +Git has a number of different transfer protocols you can use. The previous example uses the `git://` protocol, but you may also see `http(s)://` or `user@server:/path.git`, which uses the SSH transfer protocol. Chapter 4 will introduce all of the available options the server can set up to access your Git repository and the pros and cons of each.
  40 +
  41 +## Recording Changes to the Repository ##
  42 +
  43 +You have a bona fide Git repository and a checkout or working copy of the files for that project. You need to make some changes and commit snapshots of those changes into your repository each time the project reaches a state you want to record.
  44 +
  45 +Remember that each file in your working directory can be in one of two states: tracked or untracked. Tracked files are files that were in the last snapshot; they can be unmodified, modified, or staged. Untracked files are everything else - any files in your working directory that were not in your last snapshot and are not in your staging area. When you first clone a repository, all of your files will be tracked and unmodified because you just checked them out and haven’t edited anything.
  46 +
  47 +As you edit files, Git sees them as modified, because you’ve changed them since your last commit. You stage these modified files and then commit all your staged changes, and the cycle repeats. This lifecycle is illustrated in Figure 2-1.
  48 +
  49 +Insert 18333fig0201.png
  50 +Figure 2-1. The lifecycle of the status of your files.
  51 +
  52 +### Checking the Status of Your Files ###
  53 +
  54 +The main tool you use to determine which files are in which state is the git status command. If you run this command directly after a clone, you should see something like this:
  55 +
  56 + $ git status
  57 + # On branch master
  58 + nothing to commit (working directory clean)
  59 +
  60 +This means you have a clean working directory—in other words, there are no tracked and modified files. Git also doesn’t see any untracked files, or they would be listed here. Finally, the command tells you which branch you’re on. For now, that is always master, which is the default; you won’t worry about it here. The next chapter will go over branches and references in detail.
  61 +
  62 +Let’s say you add a new file to your project, a simple README file. If the file didn’t exist before, and you run `git status`, you see your untracked file like so:
  63 +
  64 + $ vim README
  65 + $ git status
  66 + # On branch master
  67 + # Untracked files:
  68 + # (use "git add <file>..." to include in what will be committed)
  69 + #
  70 + # README
  71 + nothing added to commit but untracked files present (use "git add" to track)
  72 +
  73 +You can see that your new README file is untracked, because it’s under the “Untracked files” heading in your status output. Untracked basically means that Git sees a file you didn’t have in the previous snapshot (commit); Git won’t start including it in your commit snapshots until you explicitly tell it to do so. It does this so you don’t accidentally begin including generated binary files or other files that you did not mean to include. You do want to start including README, so let’s start tracking the file.
  74 +
  75 +### Tracking New Files ###
  76 +
  77 +In order to begin tracking a new file, you use the command `git add`. To begin tracking the README file, you can run this:
  78 +
  79 + $ git add README
  80 +
  81 +If you run your status command again, you can see that your README file is now tracked and staged:
  82 +
  83 + $ git status
  84 + # On branch master
  85 + # Changes to be committed:
  86 + # (use "git reset HEAD <file>..." to unstage)
  87 + #
  88 + # new file: README
  89 + #
  90 +
  91 +You can tell that it’s staged because it’s under the “Changes to be committed” heading. If you commit at this point, the version of the file at the time you ran git add is what will be in the historical snapshot. You may recall that when you ran git init earlier, you then ran git add (files) — that was to begin tracking files in your directory. The git add command takes a path name for either a file or a directory; if it’s a directory, the command adds all the files in that directory recursively.
  92 +
  93 +### Staging Modified Files ###
  94 +
  95 +Let’s change a file that was already tracked. If you change a previously tracked file called `benchmarks.rb` and then run your `status` command again, you get something that looks like this:
  96 +
  97 + $ git status
  98 + # On branch master
  99 + # Changes to be committed:
  100 + # (use "git reset HEAD <file>..." to unstage)
  101 + #
  102 + # new file: README
  103 + #
  104 + # Changed but not updated:
  105 + # (use "git add <file>..." to update what will be committed)
  106 + #
  107 + # modified: benchmarks.rb
  108 + #
  109 +
  110 +The benchmarks.rb file appears under a section named “Changed but not updated” — which means that a file that is tracked has been modified in the working directory but not yet staged. To stage it, you run the `git add` command (it’s a multipurpose command — you use it to begin tracking new files, to stage files, and to do other things like marking merge-conflicted files as resolved). Let’s run `git add` now to stage the benchmarks.rb file, and then run `git status` again:
  111 +
  112 + $ git add benchmarks.rb
  113 + $ git status
  114 + # On branch master
  115 + # Changes to be committed:
  116 + # (use "git reset HEAD <file>..." to unstage)
  117 + #
  118 + # new file: README
  119 + # modified: benchmarks.rb
  120 + #
  121 +
  122 +Both files are staged and will go into your next commit. At this point, suppose you remember one little change that you want to make in benchmarks.rb before you commit it. You open it again and make that change, and you’re ready to commit. However, let’s run `git status` one more time:
  123 +
  124 + $ vim benchmarks.rb
  125 + $ git status
  126 + # On branch master
  127 + # Changes to be committed:
  128 + # (use "git reset HEAD <file>..." to unstage)
  129 + #
  130 + # new file: README
  131 + # modified: benchmarks.rb
  132 + #
  133 + # Changed but not updated:
  134 + # (use "git add <file>..." to update what will be committed)
  135 + #
  136 + # modified: benchmarks.rb
  137 + #
  138 +
  139 +What the heck? Now benchmarks.rb is listed as both staged and unstaged. How is that possible? It turns out that Git stages a file exactly as it is when you run the git add command. If you commit now, the version of benchmarks.rb as it was when you last ran the git add command is how it will go into the commit, not the version of the file as it looks in your working directory when you run git commit. If you modify a file after you run `git add`, you have to run `git add` again to stage the latest version of the file:
  140 +
  141 + $ git add benchmarks.rb
  142 + $ git status
  143 + # On branch master
  144 + # Changes to be committed:
  145 + # (use "git reset HEAD <file>..." to unstage)
  146 + #
  147 + # new file: README
  148 + # modified: benchmarks.rb
  149 + #
  150 +
  151 +### Ignoring Files ###
  152 +
  153 +Often, you’ll have a class of files that you don’t want Git to automatically add or even show you as being untracked. These are generally automatically generated files such as log files or files produced by your build system. In such cases, you can create a file listing patterns to match them named .gitignore. Here is an example .gitignore file:
  154 +
  155 + $ cat .gitignore
  156 + *.[oa]
  157 + *~
  158 +
  159 +The first line tells Git to ignore any files ending in .o or .a — object and archive files that may be the product of building your code. The second line tells Git to ignore all files that end with a tilde (`~`), which is used by many text editors such as Emacs to mark temporary files. You may also include a log, tmp, or pid directory; automatically generated documentation; and so on. Setting up a .gitignore file before you get going is generally a good idea so you don’t accidentally commit files that you really don’t want in your Git repository.
  160 +
  161 +The rules for the patterns you can put in the .gitignore file are as follows:
  162 +
  163 +* Blank lines or lines starting with # are ignored.
  164 +* Standard glob patterns work.
  165 +* You can end patterns with a forward slash (`/`) to specify a directory.
  166 +* You can negate a pattern by starting it with an exclamation point (`!`).
  167 +
  168 +Glob patterns are like simplified regular expressions that shells use. An asterisk (`*`) matches zero or more characters; `[abc]` matches any character inside the brackets (in this case a, b, or c); a question mark (`?`) matches a single character; and brackets enclosing characters separated by a hyphen(`[0-9]`) matches any character between them (in this case 0 through 9) .
  169 +
  170 +Here is another example .gitignore file:
  171 +
  172 + # a comment – this is ignored
  173 + *.a # no .a files
  174 + !lib.a # but do track lib.a, even though you're ignoring .a files above
  175 + /TODO # only ignore the root TODO file, not subdir/TODO
  176 + build/ # ignore all files in the build/ directory
  177 + doc/*.txt # ignore doc/notes.txt, but not doc/server/arch.txt
  178 +
  179 +### Viewing Your Staged and Unstaged Changes ###
  180 +
  181 +If the `git status` command is too vague for you — you want to know exactly what you changed, not just which files were changed — you can use the `git diff` command. We’ll cover `git diff` in more detail later; but you’ll probably use it most often to answer these two questions: What have you changed but not yet staged? And what have you staged that you are about to commit? Although `git status` answers those questions very generally, `git diff` shows you the exact lines added and removed — the patch, as it were.
  182 +
  183 +Let’s say you edit and stage the README file again and then edit the benchmarks.rb file without staging it. If you run your `status` command, you once again see something like this:
  184 +
  185 + $ git status
  186 + # On branch master
  187 + # Changes to be committed:
  188 + # (use "git reset HEAD <file>..." to unstage)
  189 + #
  190 + # new file: README
  191 + #
  192 + # Changed but not updated:
  193 + # (use "git add <file>..." to update what will be committed)
  194 + #
  195 + # modified: benchmarks.rb
  196 + #
  197 +
  198 +To see what you’ve changed but not yet staged, type `git diff` with no other arguments:
  199 +
  200 + $ git diff
  201 + diff --git a/benchmarks.rb b/benchmarks.rb
  202 + index 3cb747f..da65585 100644
  203 + --- a/benchmarks.rb
  204 + +++ b/benchmarks.rb
  205 + @@ -36,6 +36,10 @@ def main
  206 + @commit.parents[0].parents[0].parents[0]
  207 + end
  208 +
  209 + + run_code(x, 'commits 1') do
  210 + + git.commits.size
  211 + + end
  212 + +
  213 + run_code(x, 'commits 2') do
  214 + log = git.commits('master', 15)
  215 + log.size
  216 +
  217 +That command compares what is in your working directory with what is in your staging area. The result tells you the changes you’ve made that you haven’t yet staged.
  218 +
  219 +If you want to see what you’ve staged that will go into your next commit, you can use `git diff –-cached`. (In Git versions 1.6.1 and later, you can also use `git diff –-staged`, which may be easier to remember.) This command compares your staged changes to your last commit:
  220 +
  221 + $ git diff --cached
  222 + diff --git a/README b/README
  223 + new file mode 100644
  224 + index 0000000..03902a1
  225 + --- /dev/null
  226 + +++ b/README2
  227 + @@ -0,0 +1,5 @@
  228 + +grit
  229 + + by Tom Preston-Werner, Chris Wanstrath
  230 + +
  231 + +
  232 + +Grit is a Ruby library for extracting information from a Git repository
  233 +
  234 +It’s important to note that `git diff` by itself doesn’t show all changes made since your last commit — only changes that are still unstaged. This can be confusing, because if you’ve staged all of your changes, `git diff` will give you no output.
  235 +
  236 +For another example, if you stage the benchmarks.rb file and then edit it, you can use `git diff` to see the changes in the file that are staged and the changes that are unstaged:
  237 +
  238 + $ git add benchmarks.rb
  239 + $ echo '# test line' >> benchmarks.rb
  240 + $ git status
  241 + # On branch master
  242 + #
  243 + # Changes to be committed:
  244 + #
  245 + # modified: benchmarks.rb
  246 + #
  247 + # Changed but not updated:
  248 + #
  249 + # modified: benchmarks.rb
  250 + #
  251 +
  252 +Now you can use `git diff` to see what is still unstaged
  253 +
  254 + $ git diff
  255 + diff --git a/benchmarks.rb b/benchmarks.rb
  256 + index e445e28..86b2f7c 100644
  257 + --- a/benchmarks.rb
  258 + +++ b/benchmarks.rb
  259 + @@ -127,3 +127,4 @@ end
  260 + main()
  261 +
  262 + ##pp Grit::GitRuby.cache_client.stats
  263 + +# test line
  264 +
  265 +and `git diff --cached` to see what you’ve staged so far:
  266 +
  267 + $ git diff --cached
  268 + diff --git a/benchmarks.rb b/benchmarks.rb
  269 + index 3cb747f..e445e28 100644
  270 + --- a/benchmarks.rb
  271 + +++ b/benchmarks.rb
  272 + @@ -36,6 +36,10 @@ def main
  273 + @commit.parents[0].parents[0].parents[0]
  274 + end
  275 +
  276 + + run_code(x, 'commits 1') do
  277 + + git.commits.size
  278 + + end
  279 + +
  280 + run_code(x, 'commits 2') do
  281 + log = git.commits('master', 15)
  282 + log.size
  283 +
  284 +### Committing Your Changes ###
  285 +
  286 +Now that your staging area is set up the way you want it, you can commit your changes. Remember that anything that is still unstaged — any files you have created or modified that you haven’t run `git add` on since you edited them — won’t go into this commit. They will stay as modified files on your disk.
  287 +In this case, the last time you ran `git status`, you saw that everything was staged, so you’re ready to commit your changes. The simplest way to commit is to type `git commit`:
  288 +
  289 + $ git commit
  290 +
  291 +Doing so launches your editor of choice. (This is set by your shell’s `$EDITOR` environment variable — usually vim or emacs, although you can configure it with whatever you want using the `git config --global core.editor` command as you saw in Chapter 1).
  292 +
  293 +The editor displays the following text (this example is a Vim screen):
  294 +
  295 + # Please enter the commit message for your changes. Lines starting
  296 + # with '#' will be ignored, and an empty message aborts the commit.
  297 + # On branch master
  298 + # Changes to be committed:
  299 + # (use "git reset HEAD <file>..." to unstage)
  300 + #
  301 + # new file: README
  302 + # modified: benchmarks.rb
  303 + ~
  304 + ~
  305 + ~
  306 + ".git/COMMIT_EDITMSG" 10L, 283C
  307 +
  308 +You can see that the default commit message contains the latest output of the `git status` command commented out and one empty line on top. You can remove these comments and type your commit message, or you can leave them there to help you remember what you’re committing. (For an even more explicit reminder of what you’ve modified, you can pass the `-v` option to `git commit`. Doing so also puts the diff of your change in the editor so you can see exactly what you did.) When you exit the editor, Git creates your commit with that commit message (with the comments and diff stripped out).
  309 +
  310 +Alternatively, you can type your commit message inline with the `commit` command by specifying it after a -m flag, like this:
  311 +
  312 + $ git commit -m "Story 182: Fix benchmarks for speed"
  313 + [master]: created 463dc4f: "Fix benchmarks for speed"
  314 + 2 files changed, 3 insertions(+), 0 deletions(-)
  315 + create mode 100644 README
  316 +
  317 +Now you’ve created your first commit! You can see that the commit has given you some output about itself: which branch you committed to (master), what SHA-1 checksum the commit has (`463dc4f`), how many files were changed, and statistics about lines added and removed in the commit.
  318 +
  319 +Remember that the commit records the snapshot you set up in your staging area. Anything you didn’t stage is still sitting there modified; you can do another commit to add it to your history. Every time you perform a commit, you’re recording a snapshot of your project that you can revert to or compare to later.
  320 +
  321 +### Skipping the Staging Area ###
  322 +
  323 +Although it can be amazingly useful for crafting commits exactly how you want them, the staging area is sometimes a bit more complex than you need in your workflow. If you want to skip the staging area, Git provides a simple shortcut. Providing the `-a` option to the `git commit` command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the `git add` part:
  324 +
  325 + $ git status
  326 + # On branch master
  327 + #
  328 + # Changed but not updated:
  329 + #
  330 + # modified: benchmarks.rb
  331 + #
  332 + $ git commit -a -m 'added new benchmarks'
  333 + [master 83e38c7] added new benchmarks
  334 + 1 files changed, 5 insertions(+), 0 deletions(-)
  335 +
  336 +Notice how you don’t have to run `git add` on the benchmarks.rb file in this case before you commit.
  337 +
  338 +### Removing Files ###
  339 +
  340 +To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The `git rm` command does that and also removes the file from your working directory so you don’t see it as an untracked file next time around.
  341 +
  342 +If you simply remove the file from your working directory, it shows up under the “Changed but not updated” (that is, _unstaged_) area of your `git status` output:
  343 +
  344 + $ rm grit.gemspec
  345 + $ git status
  346 + # On branch master
  347 + #
  348 + # Changed but not updated:
  349 + # (use "git add/rm <file>..." to update what will be committed)
  350 + #
  351 + # deleted: grit.gemspec
  352 + #
  353 +
  354 +Then, if you run `git rm`, it stages the file’s removal:
  355 +
  356 + $ git rm grit.gemspec
  357 + rm 'grit.gemspec'
  358 + $ git status
  359 + # On branch master
  360 + #
  361 + # Changes to be committed:
  362 + # (use "git reset HEAD <file>..." to unstage)
  363 + #
  364 + # deleted: grit.gemspec
  365 + #
  366 +
  367 +The next time you commit, the file will be gone and no longer tracked. If you modified the file and added it to the index already, you must force the removal with the `-f` option. This is a safety feature to prevent accidental removal of data that hasn’t yet been recorded in a snapshot and that can’t be recovered from Git.
  368 +
  369 +Another useful thing you may want to do is to keep the file in your working tree but remove it from your staging area. In other words, you may want to keep the file on your hard drive but not have Git track it anymore. This is particularly useful if you forgot to add something to your `.gitignore` file and accidentally added it, like a large log file or a bunch of `.a` compiled files. To do this, use the `--cached` option:
  370 +
  371 + $ git rm --cached readme.txt
  372 +
  373 +You can pass files, directories, and file-glob patterns to the `git rm` command. That means you can do things such as
  374 +
  375 + $ git rm log/\*.log
  376 +
  377 +Note the backslash (`\`) in front of the `*`. This is necessary because Git does its own filename expansion in addition to your shell’s filename expansion. This command removes all files that have the `.log` extension in the `log/` directory. Or, you can do something like this:
  378 +
  379 + $ git rm \*~
  380 +
  381 +This command removes all files that end with `~`.
  382 +
  383 +### Moving Files ###
  384 +
  385 +Unlike many other VCS systems, Git doesn’t explicitly track file movement. If you rename a file in Git, no metadata is stored in Git that tells it you renamed the file. However, Git is pretty smart about figuring that out after the fact — we’ll deal with detecting file movement a bit later.
  386 +
  387 +Thus it’s a bit confusing that Git has a `mv` command. If you want to rename a file in Git, you can run something like
  388 +
  389 + $ git mv file_from file_to
  390 +
  391 +and it works fine. In fact, if you run something like this and look at the status, you’ll see that Git considers it a renamed file:
  392 +
  393 + $ git mv README.txt README
  394 + $ git status
  395 + # On branch master
  396 + # Your branch is ahead of 'origin/master' by 1 commit.
  397 + #
  398 + # Changes to be committed:
  399 + # (use "git reset HEAD <file>..." to unstage)
  400 + #
  401 + # renamed: README.txt -> README
  402 + #
  403 +
  404 +However, this is equivalent to running something like this:
  405 +
  406 + $ mv README.txt README
  407 + $ git rm README.txt
  408 + $ git add README
  409 +
  410 +Git figures out that it’s a rename implicitly, so it doesn’t matter if you rename a file that way or with the `mv` command. The only real difference is that `mv` is one command instead of three — it’s a convenience function. More important, you can use any tool you like to rename a file, and address the add/rm later, before you commit.
  411 +
  412 +## Viewing the Commit History ##
  413 +
  414 +After you have created several commits, or if you have cloned a repository with an existing commit history, you’ll probably want to look back to see what has happened. The most basic and powerful tool to do this is the `git log` command.
  415 +
  416 +These examples use a very simple project called simplegit that I often use for demonstrations. To get the project, run
  417 +
  418 + git clone git://
  419 +
  420 +When you run `git log` in this project, you should get output that looks something like this:
  421 +
  422 + $ git log
  423 + commit ca82a6dff817ec66f44342007202690a93763949
  424 + Author: Scott Chacon <>
  425 + Date: Mon Mar 17 21:52:11 2008 -0700
  426 +
  427 + changed the version number
  428 +
  429 + commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
  430 + Author: Scott Chacon <>
  431 + Date: Sat Mar 15 16:40:33 2008 -0700
  432 +
  433 + removed unnecessary test code
  434 +
  435 + commit a11bef06a3f659402fe7563abf99ad00de2209e6
  436 + Author: Scott Chacon <>
  437 + Date: Sat Mar 15 10:31:28 2008 -0700
  438 +
  439 + first commit
  440 +
  441 +By default, with no arguments, `git log` lists the commits made in that repository in reverse chronological order. That is, the most recent commits show up first. As you can see, this command lists each commit with its SHA-1 checksum, the author’s name and e-mail, the date written, and the commit message.
  442 +
  443 +A huge number and variety of options to the `git log` command are available to show you exactly what you’re looking for. Here, we’ll show you some of the most-used options.
  444 +
  445 +One of the more helpful options is `-p`, which shows the diff introduced in each commit. You can also use `-2`, which limits the output to only the last two entries:
  446 +
  447 + $ git log –p -2
  448 + commit ca82a6dff817ec66f44342007202690a93763949
  449 + Author: Scott Chacon <>
  450 + Date: Mon Mar 17 21:52:11 2008 -0700
  451 +
  452 + changed the version number
  453 +
  454 + diff --git a/Rakefile b/Rakefile
  455 + index a874b73..8f94139 100644
  456 + --- a/Rakefile
  457 + +++ b/Rakefile
  458 + @@ -5,7 +5,7 @@ require 'rake/gempackagetask'
  459 + spec = do |s|
  460 + - s.version = "0.1.0"
  461 + + s.version = "0.1.1"
  462 + = "Scott Chacon"
  463 +
  464 + commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
  465 + Author: Scott Chacon <>
  466 + Date: Sat Mar 15 16:40:33 2008 -0700
  467 +
  468 + removed unnecessary test code
  469 +
  470 + diff --git a/lib/simplegit.rb b/lib/simplegit.rb
  471 + index a0a60ae..47c6340 100644
  472 + --- a/lib/simplegit.rb
  473 + +++ b/lib/simplegit.rb
  474 + @@ -18,8 +18,3 @@ class SimpleGit
  475 + end
  476 +
  477 + end
  478 + -
  479 + -if $0 == __FILE__
  480 + - git =
  481 + - puts
  482 + -end
  483 + \ No newline at end of file
  484 +
  485 +This option displays the same information but with a diff directly following each entry. This is very helpful for code review or to quickly browse what happened during a series of commits that a collaborator has added.
  486 +You can also use a series of summarizing options with `git log`. For example, if you want to see some abbreviated stats for each commit, you can use the `--stat` option:
  487 +
  488 + $ git log --stat
  489 + commit ca82a6dff817ec66f44342007202690a93763949
  490 + Author: Scott Chacon <>
  491 + Date: Mon Mar 17 21:52:11 2008 -0700
  492 +
  493 + changed the version number
  494 +
  495 + Rakefile | 2 +-
  496 + 1 files changed, 1 insertions(+), 1 deletions(-)
  497 +
  498 + commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
  499 + Author: Scott Chacon <>
  500 + Date: Sat Mar 15 16:40:33 2008 -0700
  501 +
  502 + removed unnecessary test code
  503 +
  504 + lib/simplegit.rb | 5 -----
  505 + 1 files changed, 0 insertions(+), 5 deletions(-)
  506 +
  507 + commit a11bef06a3f659402fe7563abf99ad00de2209e6
  508 + Author: Scott Chacon <>
  509 + Date: Sat Mar 15 10:31:28 2008 -0700
  510 +
  511 + first commit
  512 +
  513 + README | 6 ++++++
  514 + Rakefile | 23 +++++++++++++++++++++++
  515 + lib/simplegit.rb | 25 +++++++++++++++++++++++++
  516 + 3 files changed, 54 insertions(+), 0 deletions(-)
  517 +
  518 +As you can see, the `--stat` option prints below each commit entry a list of modified files, how many files were changed, and how many lines in those files were added and removed. It also puts a summary of the information at the end.
  519 +Another really useful option is `--pretty`. This option changes the log output to formats other than the default. A few prebuilt options are available for you to use. The oneline option prints each commit on a single line, which is useful if you’re looking at a lot of commits. In addition, the `short`, `full`, and `fuller` options show the output in roughly the same format but with less or more information, respectively:
  520 +
  521 + $ git log --pretty=oneline
  522 + ca82a6dff817ec66f44342007202690a93763949 changed the version number
  523 + 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test code
  524 + a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
  525 +
  526 +The most interesting option is `format`, which allows you to specify your own log output format. This is especially useful when you’re generating output for machine parsing — because you specify the format explicitly, you know it won’t change with updates to Git:
  527 +
  528 + $ git log --pretty=format:"%h - %an, %ar : %s"
  529 + ca82a6d - Scott Chacon, 11 months ago : changed the version number
  530 + 085bb3b - Scott Chacon, 11 months ago : removed unnecessary test code
  531 + a11bef0 - Scott Chacon, 11 months ago : first commit
  532 +
  533 +Table 2-1 lists some of the more useful options that format takes.
  534 +
  535 + Option Description of Output
  536 + %H Commit hash
  537 + %h Abbreviated commit hash
  538 + %T Tree hash
  539 + %t Abbreviated tree hash
  540 + %P Parent hashes
  541 + %p Abbreviated parent hashes
  542 + %an Author name
  543 + %ae Author e-mail
  544 + %ad Author date (format respects the –date= option)
  545 + %ar Author date, relative
  546 + %cn Committer name
  547 + %ce Committer email
  548 + %cd Committer date
  549 + %cr Committer date, relative
  550 + %s Subject
  551 +
  552 +You may be wondering what the difference is between _author_ and _committer_. The author is the person who originally wrote the work, whereas the committer is the person who last applied the work. So, if you send in a patch to a project and one of the core members applies the patch, both of you get credit — you as the author and the core member as the committer. We’ll cover this distinction a bit more in Chapter 5.
  553 +
  554 +The oneline and format options are particularly useful with another `log` option called `--graph`. This option adds a nice little ASCII graph showing your branch and merge history, which we can see our copy of the Grit project repository:
  555 +
  556 + $ git log --pretty=format:"%h %s" --graph
  557 + * 2d3acf9 ignore errors from SIGCHLD on trap
  558 + * 5e3ee11 Merge branch 'master' of git://
  559 + |\
  560 + | * 420eac9 Added a method for getting the current branch.
  561 + * | 30e367c timeout code and tests
  562 + * | 5a09431 add timeout protection to grit
  563 + * | e1193f8 support for heads with slashes in them
  564 + |/
  565 + * d6016bc require time for xmlschema
  566 + * 11d191e Merge branch 'defunkt' into local
  567 +
  568 +Those are only some simple output-formatting options to `git log` — there are many more. Table 2-2 lists the options we’ve covered so far and some other common formatting options that may be useful, along with how they change the output of the log command.
  569 +
  570 + Option Description
  571 + -p Show the patch introduced with each commit.
  572 + --stat Show statistics for files modified in each commit.
  573 + --shortstat Display only the changed/insertions/deletions line from the --stat command.
  574 + --name-only Show the list of files modified after the commit information.
  575 + --name-status Show the list of files affected with added/modified/deleted information as well.
  576 + --abbrev-commit Show only the first few characters of the SHA-1 checksum instead of all 40.
  577 + --relative-date Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format.
  578 + --graph Display an ASCII graph of the branch and merge history beside the log output.
  579 + --pretty Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format).
  580 +
  581 +### Limiting Log Output ###
  582 +
  583 +In addition to output-formatting options, git log takes a number of useful limiting options — that is, options that let you show only a subset of commits. You’ve seen one such option already — the `-2` option, which show only the last two commits. In fact, you can do `-<n>`, where `n` is any integer to show the last `n` commits. In reality, you’re unlikely to use that often, because Git by default pipes all output through a pager so you see only one page of log output at a time.
  584 +
  585 +However, the time-limiting options such as `--since` and `--until` are very useful. For example, this command gets the list of commits made in the last two weeks:
  586 +
  587 + $ git log --since=2.weeks
  588 +
  589 +This command works with lots of formats — you can specify a specific date (“2008-01-15”) or a relative date such as “2 years 1 day 3 minutes ago”.
  590 +
  591 +You can also filter the list to commits that match some search criteria. The `--author` option allows you to filter on a specific author, and the `--grep` option lets you search for keywords in the commit messages. (Note that if you want to specify both author and grep options, you have to add `--all-match` or the command will match commits with either.)
  592 +
  593 +The last really useful option to pass to `git log` as a filter is a path. If you specify a directory or file name, you can limit the log output to commits that introduced a change to those files. This is always the last option and is generally preceded by double dashes (`--`) to separate the paths from the options.
  594 +
  595 +In Table 2-3 we’ll list these and a few other common options for your reference.
  596 +
  597 + Option Description
  598 + -(n) Show only the last n commits
  599 + --since, --after Limit the commits to those made after the specified date.
  600 + --until, --before Limit the commits to those made before the specified date.
  601 + --author Only show commits in which the author entry matches the specified string.
  602 + --committer Only show commits in which the committer entry matches the specified string.
  603 +
  604 +For example, if you want to see which commits modifying test files in the Git source code history were committed by Junio Hamano and were not merges in the month of October 2008, you can run something like this:
  605 +
  606 + $ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
  607 + --before="2008-11-01" --no-merges -- t/
  608 + 5610e3b - Fix testcase failure when extended attribute
  609 + acd3b9e - Enhance hold_lock_file_for_{update,append}()
  610 + f563754 - demonstrate breakage of detached checkout wi
  611 + d1a43f2 - reset --hard/read-tree --reset -u: remove un
  612 + 51a94af - Fix "checkout --track -b newbranch" on detac
  613 + b0ad11e - pull: allow "git pull origin $something:$cur
  614 +
  615 +Of the nearly 20,000 commits in the Git source code history, this command shows the 6 that match those criteria.
  616 +
  617 +### Using a GUI to Visualize History ###
  618 +
  619 +If you like to use a more graphical tool to visualize your commit history, you may want to take a look at a Tcl/Tk program called gitk that is distributed with Git. Gitk is basically a visual `git log` tool, and it accepts nearly all the filtering options that `git log` does. If you type gitk on the command line in your project, you should see something like Figure 2-2.
  620 +
  621 +Insert 18333fig0202.png
  622 +Figure 2-2. The gitk history visualizer.
  623 +
  624 +You can see the commit history in the top half of the window along with a nice ancestry graph. The diff viewer in the bottom half of the window shows you the changes introduced at any commit you click.
  625 +
  626 +## Undoing Things ##
  627 +
  628 +At any stage, you may want to undo something. Here, we’ll review a few basic tools for undoing changes that you’ve made. Be careful, because you can’t always undo some of these undos. This is one of the few areas in Git where you may lose some work if you do it wrong.
  629 +
  630 +### Changing Your Last Commit ###
  631 +
  632 +One of the common undos takes place when you commit too early and possibly forget to add some files, or you mess up your commit message. If you want to try that commit again, you can run commit with the `--amend` option:
  633 +
  634 + $ git commit --amend
  635 +
  636 +This command takes your staging area and uses it for the commit. If you’ve have made no changes since your last commit (for instance, you run this command immediately after your previous commit), then your snapshot will look exactly the same and all you’ll change is your commit message.
  637 +
  638 +The same commit-message editor fires up, but it already contains the message of your previous commit. You can edit the message the same as always, but it overwrites your previous commit.
  639 +
  640 +As an example, if you commit and then realize you forgot to stage the changes in a file you wanted to add to this commit, you can do something like this:
  641 +
  642 + $ git commit -m 'initial commit'
  643 + $ git add forgotten_file
  644 + $ git commit --amend
  645 +
  646 +All three of these commands end up with a single commit — the second commit replaces the results of the first.
  647 +
  648 +### Unstaging a Staged File ###
  649 +
  650 +The next two sections demonstrate how to wrangle your staging area and working directory changes. The nice part is that the command you use to determine the state of those two areas also reminds you how to undo changes to them. For example, let’s say you’ve changed two files and want to commit them as two separate changes, but you accidentally type `git add *` and stage them both. How can you unstage one of the two? The `git status` command reminds you:
  651 +
  652 + $ git add .
  653 + $ git status
  654 + # On branch master
  655 + # Changes to be committed:
  656 + # (use "git reset HEAD <file>..." to unstage)
  657 + #
  658 + # modified: README.txt
  659 + # modified: benchmarks.rb
  660 + #
  661 +
  662 +Right below the “Changes to be committed” text, it says use `git reset HEAD <file>...` to unstage. So, let’s use that advice to unstage the benchmarks.rb file:
  663 +
  664 + $ git reset HEAD benchmarks.rb
  665 + benchmarks.rb: locally modified
  666 + $ git status
  667 + # On branch master
  668 + # Changes to be committed:
  669 + # (use "git reset HEAD <file>..." to unstage)
  670 + #
  671 + # modified: README.txt
  672 + #
  673 + # Changed but not updated:
  674 + # (use "git add <file>..." to update what will be committed)
  675 + # (use "git checkout -- <file>..." to discard changes in working directory)
  676 + #
  677 + # modified: benchmarks.rb
  678 + #
  679 +
  680 +The command is a bit strange, but it works. The benchmarks.rb file is modified but once again unstaged.
  681 +
  682 +### Unmodifying a Modified File ###
  683 +
  684 +What if you realize that you don’t want to keep your changes to the benchmarks.rb file? How can you easily unmodify it — revert it back to what it looked like when you last committed (or initially cloned, or however you got it into your working directory)? Luckily, `git status` tells you how to do that, too. In the last example output, the unstaged area looks like this:
  685 +
  686 + # Changed but not updated:
  687 + # (use "git add <file>..." to update what will be committed)
  688 + # (use "git checkout -- <file>..." to discard changes in working directory)
  689 + #
  690 + # modified: benchmarks.rb
  691 + #
  692 +
  693 +It tells you pretty explicitly how to discard the changes you’ve made (at least, the newer versions of Git, 1.6.1 and later, do this — if you have an older version, we highly recommend upgrading it to get some of these nicer usability features). Let’s do what it says:
  694 +
  695 + $ git checkout -- benchmarks.rb
  696 + $ git status
  697 + # On branch master
  698 + # Changes to be committed:
  699 + # (use "git reset HEAD <file>..." to unstage)
  700 + #
  701 + # modified: README.txt
  702 + #
  703 +
  704 +You can see that the changes have been reverted. You should also realize that this is a dangerous command: any changes you made to that file are gone — you just copied another file over it. Don’t ever use this command unless you absolutely know that you don’t want the file. If you just need to get it out of the way, we’ll go over stashing and branching in the next chapter; these are generally better ways to go.
  705 +
  706 +Remember, anything that is committed in Git can almost always be recovered. Even commits that were on branches that were deleted or commits that were overwritten with an `--amend` commit can be recovered (see Chapter 9 for data recovery). However, anything you lose that was never committed is likely never to be seen again.
  707 +
  708 +## Working with Remotes ##
  709 +
  710 +To be able to collaborate on any Git project, you need to know how to manage your remote repositories. Remote repositories are versions of your project that are hosted on the Internet or network somewhere. You can have several of them, each of which generally is either read-only or read/write for you. Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work.
  711 +Managing remote repositories includes knowing how to add remote repositories, remove remotes that are no longer valid, manage various remote branches and define them as being tracked or not, and more. In this section, we’ll cover these remote-management skills.
  712 +
  713 +### Showing Your Remotes ###
  714 +
  715 +To see which remote servers you have configured, you can run the git remote command. It lists the shortnames of each remote handle you’ve specified. If you’ve cloned your repository, you should at least see origin — that is the default name Git gives to the server you cloned from:
  716 +
  717 + $ git clone git://
  718 + Initialized empty Git repository in /private/tmp/ticgit/.git/
  719 + remote: Counting objects: 595, done.
  720 + remote: Compressing objects: 100% (269/269), done.
  721 + remote: Total 595 (delta 255), reused 589 (delta 253)
  722 + Receiving objects: 100% (595/595), 73.31 KiB | 1 KiB/s, done.
  723 + Resolving deltas: 100% (255/255), done.
  724 + $ cd ticgit
  725 + $ git remote
  726 + origin
  727 +
  728 +You can also specify `-v`, which shows you the URL that Git has stored for the shortname to be expanded to:
  729 +
  730 + $ git remote -v
  731 + origin git://
  732 +
  733 +If you have more than one remote, the command lists them all. For example, my Grit repository looks something like this.
  734 +
  735 + $ cd grit
  736 + $ git remote -v
  737 + bakkdoor git://
  738 + cho45 git://
  739 + defunkt git://
  740 + koke git://
  741 + origin
  742 +
  743 +This means we can pull contributions from any of these users pretty easily. But notice that only the origin remote is an SSH URL, so it’s the only one I can push to (we’ll cover why this is in Chapter 4).
  744 +
  745 +### Adding Remote Repositories ###
  746 +
  747 +I’ve mentioned and given some demonstrations of adding remote repositories in previous sections, but here is how to do it explicitly. To add a new remote Git repository as a shortname you can reference easily, run `git remote add [shortname] [url]`:
  748 +
  749 + $ git remote
  750 + origin
  751 + $ git remote add pb git://
  752 + $ git remote -v
  753 + origin git://
  754 + pb git://
  755 +
  756 +Now you can use the string pb on the command line in lieu of the whole URL. For example, if you want to fetch all the information that Paul has but that you don’t yet have in your repository, you can run git fetch pb:
  757 +
  758 + $ git fetch pb
  759 + remote: Counting objects: 58, done.
  760 + remote: Compressing objects: 100% (41/41), done.
  761 + remote: Total 44 (delta 24), reused 1 (delta 0)
  762 + Unpacking objects: 100% (44/44), done.
  763 + From git://
  764 + * [new branch] master -> pb/master
  765 + * [new branch] ticgit -> pb/ticgit
  766 +
  767 +Paul’s master branch is accessible locally as `pb/master` — you can merge it into one of your branches, or you can check out a local branch at that point if you want to inspect it.
  768 +
  769 +### Fetching and Pulling from Your Remotes ###
  770 +
  771 +As you just saw, to get data from your remote projects, you can run:
  772 +
  773 + $ git fetch [remote-name]
  774 +
  775 +The command goes out to that remote project and pulls down all the data from that remote project that you don’t have yet. After you do this, you should have references to all the branches from that remote, which you can merge in or inspect at any time. (We’ll go over what branches are and how to use them in much more detail in Chapter 3.)
  776 +
  777 +If you clone a repository, the command automatically adds that remote repository under the name origin. So, `git fetch origin` fetches any new work that has been pushed to that server since you cloned (or last fetched from) it. It’s important to note that the fetch command pulls the data to your local repository — it doesn’t automatically merge it with any of your work or modify what you’re currently working on. You have to merge it manually into your work when you’re ready.
  778 +
  779 +If you have a branch set up to track a remote branch (see the next section and Chapter 3 for more information), you can use the `git pull` command to automatically fetch and then merge a remote branch into your current branch. This may be an easier or more comfortable workflow for you; and by default, the `git clone` command automatically sets up your local master branch to track the remote master branch on the server you cloned from (assuming the remote has a master branch). Running `git pull` generally fetches data from the server you originally cloned from and automatically tries to merge it into the code you’re currently working on.
  780 +
  781 +### Pushing to Your Remotes ###
  782 +
  783 +When you have your project at a point that you want to share, you have to push it upstream. The command for this is simple: `git push [remote-name] [branch-name]`. If you want to push your master branch to your `origin` server (again, cloning generally sets up both of those names for you automatically), then you can run this to push your work back up to the server:
  784 +
  785 + $ git push origin master
  786 +
  787 +This command works only if you cloned from a server to which you have write access and if nobody has pushed in the meantime. If you and someone else clone at the same time and they push upstream and then you push upstream, your push will rightly be rejected. You’ll have to pull down their work first and incorporate it into yours before you’ll be allowed to push. See Chapter 3 for more detailed information on how to push to remote servers.
  788 +
  789 +### Inspecting a Remote ###
  790 +
  791 +If you want to see more information about a particular remote, you can use the `git remote show [remote-name]` command. If you run this command with a particular shortname, such as `origin`, you get something like this:
  792 +
  793 + $ git remote show origin
  794 + * remote origin
  795 + URL: git://
  796 + Remote branch merged with 'git pull' while on branch master
  797 + master
  798 + Tracked remote branches
  799 + master
  800 + ticgit
  801 +
  802 +It lists the URL for the remote repository as well as the tracking branch information. The command helpfully tells you that if you’re on the master branch and you run `git pull`, it will automatically merge in the master branch on the remote after it fetches all the remote references. It also lists all the remote references it has pulled down.
  803 +
  804 +That is a simple example you’re likely to encounter. When you’re using Git more heavily, however, you may see much more information from `git remote show`:
  805 +
  806 + $ git remote show origin
  807 + * remote origin
  808 + URL:
  809 + Remote branch merged with 'git pull' while on branch issues
  810 + issues
  811 + Remote branch merged with 'git pull' while on branch master
  812 + master
  813 + New remote branches (next fetch will store in remotes/origin)
  814 + caching
  815 + Stale tracking branches (use 'git remote prune')
  816 + libwalker
  817 + walker2
  818 + Tracked remote branches
  819 + acl
  820 + apiv2
  821 + dashboard2
  822 + issues
  823 + master
  824 + postgres
  825 + Local branch pushed with 'git push'
  826 + master:master
  827 +
  828 +This command shows which branch is automatically pushed when you run `git push` on certain branches. It also shows you which remote branches on the server you don’t yet have, which remote branches you have that have been removed from the server, and multiple branches that are automatically merged when you run `git pull`.
  829 +
  830 +### Removing and Renaming Remotes ###
  831 +
  832 +If you want to rename a reference, in newer versions of Git you can run `git remote rename` to change a remote’s shortname. For instance, if you want to rename `pb` to `paul`, you can do so with `git remote rename`:
  833 +
  834 + $ git remote rename pb paul
  835 + $ git remote
  836 + origin
  837 + paul
  838 +
  839 +It’s worth mentioning that this changes your remote branch names, too. What used to be referenced at `pb/master` is now at `paul/master`.
  840 +
  841 +If you want to remove a reference for some reason — you’ve moved the server or are no longer using a particular mirror, or perhaps a contributor isn’t contributing anymore — you can use `git remote rm`:
  842 +
  843 + $ git remote rm paul
  844 + $ git remote
  845 + origin
  846 +
  847 +## Tagging ##
  848 +
  849 +Like most VCSs, Git has the ability to tag specific points in history as being important. Generally, people use this functionality to mark release points (v1.0, and so on). In this section, you’ll learn how to list the available tags, how to create new tags, and what the different types of tags are.
  850 +
  851 +### Listing Your Tags ###
  852 +
  853 +Listing the available tags in Git is straightforward. Just type `git tag`:
  854 +
  855 + $ git tag
  856 + v0.1