<h1>Getting Started</h1>

<p>
  This chapter will be about getting started with Git.&emsp14;&emsp14;We will
  begin 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 set up to
  start working with.&emsp14;&emsp14;At the end of this chapter you should
  understand why Git is around, why you should use it and you should be all set
  up to do so.
</p>

<h2>About Version Control</h2>

<p>
  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. Even though the examples in this book show
  software source code as the files under version control, in reality any type
  of file on a computer can be placed under version control.
</p>

<p>
  If you are a graphic or web designer and want to keep every version of an
  image or layout (which you certainly would), it is very wise to use a Version
  Control System (VCS). A VCS allows you to: revert files back to a previous
  state, revert the entire project back to a previous state, review changes
  made 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 means
  that if you screw things up or lose files, you can generally recover easily.
  In addition, you get all this for very little overhead.
</p>

<h3>Local Version Control Systems</h3>

<p>
  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.
</p>

<p>
  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).
</p>

<p>Insert 18333fig0101.png Figure 1-1. Local version control diagram.</p>

<p>
  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 revision to another in a special format on disk; it can then
  recreate what any file looked like at any point in time by adding up all the
  patches.
</p>

<h3>Centralized Version Control Systems</h3>

<p>
  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).
</p>

<p>Insert 18333fig0102.png Figure 1-2. Centralized version control diagram.</p>

<p>
  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.
</p>

<p>
  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.
</p>

<h3>Distributed Version Control Systems</h3>

<p>
  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).
</p>

<p>Insert 18333fig0103.png Figure 1-3. Distributed version control diagram.</p>

<p>
  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.
</p>

<h2>A Short History of Git</h2>

<p>
  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.
</p>

<p>
  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:
</p>

<ul>
  <li>
    <p>Speed</p>
  </li>

  <li>
    <p>Simple design</p>
  </li>

  <li>
    <p>
      Strong support for non-linear development (thousands of parallel
      branches)
    </p>
  </li>

  <li>
    <p>Fully distributed</p>
  </li>

  <li>
    <p>
      Able to handle large projects like the Linux kernel efficiently (speed
      and data size)
    </p>
  </li>
</ul>

<p>
  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).
</p>

<h2>Git Basics</h2>

<p>
  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.
</p>

<h3>Snapshots, Not Differences</h3>

<p>
  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.
</p>

<p>
  Insert 18333fig0104.png Figure 1-4. Other systems tend to store data as
  changes to a base version of each file.
</p>

<p>
  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.
</p>

<p>
  Insert 18333fig0105.png Figure 1-5. Git stores data as snapshots of the
  project over time.
</p>

<p>
  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.
</p>

<h3>Nearly Every Operation Is Local</h3>

<p>
  Most operations in Git only need local files and resources to operate —
  generally no information is needed from another computer on your
  network.&emsp14;&emsp14;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.
</p>

<p>
  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.
</p>

<p>
  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.
</p>

<h3>Git Has Integrity</h3>

<p>
  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.
</p>

<p>
  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:
</p>

<pre>
24b9da6552252987aa493b52f8696cd6d3b00373
</pre>

<p>
  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.
</p>

<h3>Git Generally Only Adds Data</h3>

<p>
  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.
</p>

<p>
  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 Chapter 9.
</p>

<h3>The Three States</h3>

<p>
  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.
</p>

<p>
  This leads us to the three main sections of a Git project: the Git directory,
  the working directory, and the staging area.
</p>

<p>
  Insert 18333fig0106.png Figure 1-6. Working directory, staging area, and Git
  directory.
</p>

<p>
  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.
</p>

<p>
  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.
</p>

<p>
  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.
</p>

<p>The basic Git workflow goes something like this:</p>

<ol>
  <li>
    <p>You modify files in your working directory.</p>
  </li>

  <li>
    <p>You stage the files, adding snapshots of them to your staging area.</p>
  </li>

  <li>
    <p>
      You do a commit, which takes the files as they are in the staging area
      and stores that snapshot permanently to your Git directory.
    </p>
  </li>
</ol>

<p>
  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.
</p>

<h2>Installing Git</h2>

<p>
  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.
</p>

<h3>Installing from Source</h3>

<p>
  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.
</p>

<p>
  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:
</p>

<pre>
$ yum install curl-devel expat-devel gettext-devel \
  openssl-devel zlib-devel perl-devel asciidoc xmlto

$ apt-get install libcurl4-gnutls-dev libexpat1-dev gettext \
  libz-dev libssl-dev
</pre>

<p>
  When you have all the necessary dependencies, you can go ahead and grab the
  latest snapshot from the Git web site:
</p>

<pre>
http://git-scm.com/download
</pre>

<p>Then, compile and install:</p>

<pre>
$ tar -zxf git-1.7.2.2.tar.gz
$ cd git-1.7.2.2
$ make prefix=/usr/local all
$ sudo make prefix=/usr/local install
</pre>

<p>After this is done, you can also get Git via Git itself for updates:</p>

<pre>
$ git clone git://git.kernel.org/pub/scm/git/git.git
</pre>

<h3>Installing on Linux</h3>

<p>
  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:
</p>

<pre>
$ yum install git
</pre>

<p>Or if you’re on a Debian-based distribution like Ubuntu, try apt-get:</p>

<pre>
$ apt-get install git
</pre>

<h3>Installing on Mac</h3>

<p>
  There are three easy ways to install Git on a Mac. The easiest is to use the
  graphical Git installer, which you can download from the SourceForge page
  (see Figure 1-7):
</p>

<pre>
http://sourceforge.net/projects/git-osx-installer/
</pre>

<p>Insert 18333fig0107.png Figure 1-7. Git OS X installer.</p>

<p>
  The other major way is to install Git via MacPorts (<code>http://www.macports.org</code>). If you have MacPorts installed, install Git via
</p>

<pre>
$ sudo port install git +svn +doc +bash_completion +gitweb
</pre>

<p>
  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).
</p>

<p>
  Homebrew (<code>http://brew.sh/</code>) is another alternative to install Git. If you have Homebrew installed,
  install Git via
</p>

<pre>
$ brew install git
</pre>

<h3>Installing on Windows</h3>

<p>
  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 GitHub page, and run it:
</p>

<pre>
http://msysgit.github.io
</pre>

<p>
  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.
</p>

<p>
  Note on Windows usage: you should use Git with the provided msysGit shell
  (Unix style), it allows to use the complex lines of command given in this
  book. If you need, for some reason, to use the native Windows shell / command
  line console, you have to use double quotes instead of single quotes (for
  parameters with spaces in them) and you must quote the parameters ending with
  the circumflex accent (^) if they are last on the line, as it is a
  continuation symbol in Windows.
</p>

<h2>First-Time Git Setup</h2>

<p>
  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.
</p>

<p>
  Git comes with a tool called <code>git config</code> 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:
</p>

<ul>
  <li>
    <p>
      <code>/etc/gitconfig</code> file: Contains values for every user on the system and all their
      repositories. If you pass the option<code> --system</code> to <code>git config</code>, it reads and writes from this file specifically.
    </p>
  </li>

  <li>
    <p>
      <code>~/.gitconfig</code> file: Specific to your user. You can make Git read and write to this
      file specifically by passing the <code>--global</code> option.
    </p>
  </li>

  <li>
    <p>
      config file in the Git directory (that is, <code>.git/config</code>) of whatever repository you’re currently using: Specific to that single
      repository. Each level overrides values in the previous level, so values
      in <code>.git/config</code> trump those in <code>/etc/gitconfig</code>.
    </p>
  </li>
</ul>

<p>
  On Windows systems, Git looks for the <code>.gitconfig</code> file in the <code>$HOME</code> directory (<code>%USERPROFILE%</code> in Windows’ environment), which is <code>C:\Documents and Settings\$USER</code> or <code>C:\Users\$USER</code> for most people, depending on version (<code>$USER</code> is <code>%USERNAME%</code> in Windows’ environment). 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.
</p>

<h3>Your Identity</h3>

<p>
  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:
</p>

<pre>
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
</pre>

<p>
  Again, you need to do this only once if you pass the <code>--global</code> 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 <code>--global</code> option when you’re in that project.
</p>

<h3>Your Editor</h3>

<p>
  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:
</p>

<pre>
$ git config --global core.editor emacs
</pre>

<h3>Your Diff Tool</h3>

<p>
  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:
</p>

<pre>
$ git config --global merge.tool vimdiff
</pre>

<p>
  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.
</p>

<h3>Checking Your Settings</h3>

<p>
  If you want to check your settings, you can use the <code>git config --list</code> command to list all the settings Git can find at that point:
</p>

<pre>
$ git config --list
user.name=Scott Chacon
user.email=schacon@gmail.com
color.status=auto
color.branch=auto
color.interactive=auto
color.diff=auto
...
</pre>

<p>
  You may see keys more than once, because Git reads the same key from
  different files (<code>/etc/gitconfig</code> and <code>~/.gitconfig</code>, for example). In this case, Git uses the last value for each unique key it
  sees.
</p>

<p>
  You can also check what Git thinks a specific key’s value is by typing <code>git config {key}</code>:
</p>

<pre>
$ git config user.name
Scott Chacon
</pre>

<h2>Getting Help</h2>

<p>
  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:
</p>

<pre>
$ git help &lt;verb&gt;
$ git &lt;verb&gt; --help
$ man git-&lt;verb&gt;
</pre>

<p>
  For example, you can get the manpage help for the config command by running
</p>

<pre>
$ git help config
</pre>

<p>
  These commands are nice because you can access them anywhere, even offline.
  If the manpages and this book aren’t enough and you need in-person help, you
  can try the <code>#git</code> or <code>#github</code> channel on the Freenode IRC server (irc.freenode.net). These channels are
  regularly filled with hundreds of people who are all very knowledgeable about
  Git and are often willing to help.
</p>

<h2>Summary</h2>

<p>
  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.
</p>

<h1>Git Basics</h1>

<p>
  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.
</p>

<h2>Getting a Git Repository</h2>

<p>
  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.
</p>

<h3>Initializing a Repository in an Existing Directory</h3>

<p>
  If you’re starting to track an existing project in Git, you need to go to the
  project’s directory and type
</p>

<pre>
$ git init
</pre>

<p>
  This creates a new subdirectory named <code>.git</code> that contains all of your necessary repository files — a Git repository
  skeleton. At this point, nothing in your project is tracked yet. (See <i>Chapter 9</i> for more information about exactly what files are contained in the <code>.git</code> directory you just created.)
</p>

<p>
  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 <code>git add</code> commands that specify the files you want to track, followed by a commit:
</p>

<pre>
$ git add *.c
$ git add README
$ git commit -m 'initial project version'
</pre>

<p>
  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.
</p>

<h3>Cloning an Existing Repository</h3>

<p>
  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 <code>git clone</code>. If you’re familiar with other VCS systems such as Subversion, you’ll notice
  that the command is <code>clone</code> and not <code>checkout</code>. 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 <code>git clone</code>. 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 <i>Chapter 4</i> for more details).
</p>

<p>
  You clone a repository with <code>git clone [url]</code>. For example, if you want to clone the Ruby Git library called Grit, you can
  do so like this:
</p>

<pre>
$ git clone git://github.com/schacon/grit.git
</pre>

<p>
  That creates a directory named <code>grit</code>, initializes a <code>.git</code> 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 <code>grit</code> 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:
</p>

<pre>
$ git clone git://github.com/schacon/grit.git mygrit
</pre>

<p>
  That command does the same thing as the previous one, but the target
  directory is called <code>mygrit</code>.
</p>

<p>
  Git has a number of different transfer protocols you can use. The previous
  example uses the <code>git://</code> protocol, but you may also see <code>http(s)://</code> or <code>user@server:/path.git</code>, which uses the SSH transfer protocol. <i>Chapter 4</i> will introduce all of the available options the server can set up to access
  your Git repository and the pros and cons of each.
</p>

<h2>Recording Changes to the Repository</h2>

<p>
  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.
</p>

<p>
  Remember that each file in your working directory can be in one of two
  states: <i>tracked</i> or <i>untracked</i>. <i>Tracked</i> files are files that were in the last snapshot; they can be <i>unmodified</i>, <i>modified</i>, or <i>staged</i>. <i>Untracked</i> files are everything else — any files in your working directory that were
  not in your last snapshot and are not in your staging
  area.&emsp14;&emsp14;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.
</p>

<p>
  As you edit files, Git sees them as modified, because you’ve changed them
  since your last commit. You <i>stage</i> these modified files and then commit all your staged changes, and the cycle
  repeats. This lifecycle is illustrated in Figure 2-1.
</p>

<p>
  Insert 18333fig0201.png Figure 2-1. The lifecycle of the status of your
  files.
</p>

<h3>Checking the Status of Your Files</h3>

<p>
  The main tool you use to determine which files are in which state is the <code>git status</code> command. If you run this command directly after a clone, you should see
  something like this:
</p>

<pre>
$ git status
On branch master
nothing to commit, working directory clean
</pre>

<p>
  This means you have a clean working directory — in other words, no tracked
  files are modified. 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 <code>master</code>, which is the default; you won’t worry about it here. The next chapter will
  go over branches and references in detail.
</p>

<p>
  Let’s say you add a new file to your project, a simple <code>README</code> file. If the file didn’t exist before, and you run <code>git status</code>, you see your untracked file like so:
</p>

<pre>
$ vim README
$ git status
On branch master
Untracked files:
  (use "git add &lt;file&gt;..." to include in what will be committed)

        README

nothing added to commit but untracked files present (use "git add" to track)
</pre>

<p>
  You can see that your new <code>README</code> 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.
</p>

<h3>Tracking New Files</h3>

<p>
  In order to begin tracking a new file, you use the command <code>git add</code>. To begin tracking the <code>README</code> file, you can run this:
</p>

<pre>
$ git add README
</pre>

<p>
  If you run your status command again, you can see that your <code>README</code> file is now tracked and staged:
</p>

<pre>
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        new file:   README
</pre>

<p>
  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 <code>git add</code> is what will be in the historical snapshot. You may recall that when you ran
  <code>git init</code> earlier, you then ran <code>git add (files)</code> — that was to begin tracking files in your directory. The <code>git add</code> 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.
</p>

<h3>Staging Modified Files</h3>

<p>
  Let’s change a file that was already tracked. If you change a previously
  tracked file called <code>benchmarks.rb</code> and then run your <code>status</code> command again, you get something that looks like this:
</p>

<pre>
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        new file:   README

Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        modified:   benchmarks.rb
</pre>

<p>
  The <code>benchmarks.rb</code> file appears under a section named “Changes not staged for commit” — 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 <code>git add</code> 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 <code>git add</code> now to stage the <code>benchmarks.rb</code> file, and then run <code>git status</code> again:
</p>

<pre>
$ git add benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        new file:   README
        modified:   benchmarks.rb
</pre>

<p>
  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 <code>benchmarks.rb</code> before you commit it. You open it again and make that change, and you’re
  ready to commit. However, let’s run <code>git status</code> one more time:
</p>

<pre>
$ vim benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        new file:   README
        modified:   benchmarks.rb

Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        modified:   benchmarks.rb
</pre>

<p>
  What the heck? Now <code>benchmarks.rb</code> 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 <code>git add</code> command. If you commit now, the version of <code>benchmarks.rb</code> as it was when you last ran the <code>git add</code> 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 <code>git commit</code>. If you modify a file after you run <code>git add</code>, you have to run <code>git add</code> again to stage the latest version of the file:
</p>

<pre>
$ git add benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        new file:   README
        modified:   benchmarks.rb
</pre>

<h3>Ignoring Files</h3>

<p>
  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 <code>.gitignore</code>.&emsp14;&emsp14;Here is an example <code>.gitignore</code> file:
</p>

<pre>
$ cat .gitignore
*.[oa]
*~
</pre>

<p>
  The first line tells Git to ignore any files ending in <code>.o</code> or <code>.a</code> — <i>object</i> and <i>archive</i> files that may be the product of building your code. The second line tells
  Git to ignore all files that end with a tilde (<code>~</code>), which is used by many text editors such as Emacs to mark temporary files.
  You may also include a <code>log</code>, <code>tmp</code>, or <code>pid</code> directory; automatically generated documentation; and so on. Setting up a <code>.gitignore</code> 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.
</p>

<p>
  The rules for the patterns you can put in the <code>.gitignore</code> file are as follows:
</p>

<ul>
  <li>
    <p>Blank lines or lines starting with <code>#</code> are ignored.</p>
  </li>

  <li>
    <p>Standard glob patterns work.</p>
  </li>

  <li>
    <p>
      You can end patterns with a forward slash (<code>/</code>) to specify a directory.
    </p>
  </li>

  <li>
    <p>
      You can negate a pattern by starting it with an exclamation point (<code>!</code>).
    </p>
  </li>
</ul>

<p>
  Glob patterns are like simplified regular expressions that shells use. An
  asterisk (<code>*</code>) matches zero or more characters; <code>[abc]</code> matches any character inside the brackets (in this case <code>a</code>, <code>b</code>, or <code>c</code>); a question mark (<code>?</code>) matches a single character; and brackets enclosing characters separated by
  a hyphen(<code>[0-9]</code>) matches any character in the range (in this case 0 through 9) .
</p>

<p>Here is another example <code>.gitignore</code> file:</p>

<pre>
# a comment - this is ignored
# no .a files
*.a
# but do track lib.a, even though you're ignoring .a files above
!lib.a
# only ignore the root TODO file, not subdir/TODO
/TODO
# ignore all files in the build/ directory
build/
# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt
# ignore all .txt files in the doc/ directory
doc/**/*.txt
</pre>

<p>A <code>**/</code> pattern is available in Git since version 1.8.2.</p>

<h3>Viewing Your Staged and Unstaged Changes</h3>

<p>
  If the <code>git status</code> command is too vague for you — you want to know exactly what you changed,
  not just which files were changed — you can use the <code>git diff</code> command. We’ll cover <code>git diff</code> 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 <code>git status</code> answers those questions very generally, <code>git diff</code> shows you the exact lines added and removed — the patch, as it were.
</p>

<p>
  Let’s say you edit and stage the <code>README</code> file again and then edit the <code>benchmarks.rb</code> file without staging it. If you run your <code>status</code> command, you once again see something like this:
</p>

<pre>
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        new file:   README

Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        modified:   benchmarks.rb
</pre>

<p>
  To see what you’ve changed but not yet staged, type <code>git diff</code> with no other arguments:
</p>

<pre>
$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..da65585 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
           @commit.parents[0].parents[0].parents[0]
         end

+        run_code(x, 'commits 1') do
+          git.commits.size
+        end
+
         run_code(x, 'commits 2') do
           log = git.commits('master', 15)
           log.size
</pre>

<p>
  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.
</p>

<p>
  If you want to see what you’ve staged that will go into your next commit, you
  can use <code>git diff --cached</code>. (In Git versions 1.6.1 and later, you can also use <code>git diff --staged</code>, which may be easier to remember.) This command compares your staged changes
  to your last commit:
</p>

<pre>
$ git diff --cached
diff --git a/README b/README
new file mode 100644
index 0000000..03902a1
--- /dev/null
+++ b/README2
@@ -0,0 +1,5 @@
+grit
+ by Tom Preston-Werner, Chris Wanstrath
+ http://github.com/mojombo/grit
+
+Grit is a Ruby library for extracting information from a Git repository
</pre>

<p>
  It’s important to note that <code>git diff</code> 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, <code>git diff</code> will give you no output.
</p>

<p>
  For another example, if you stage the <code>benchmarks.rb</code> file and then edit it, you can use <code>git diff</code> to see the changes in the file that are staged and the changes that are
  unstaged:
</p>

<pre>
$ git add benchmarks.rb
$ echo '# test line' &gt;&gt; benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        modified:   benchmarks.rb

Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        modified:   benchmarks.rb
</pre>

<p>Now you can use <code>git diff</code> to see what is still unstaged</p>

<pre>
$ git diff
diff --git a/benchmarks.rb b/benchmarks.rb
index e445e28..86b2f7c 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -127,3 +127,4 @@ end
 main()

 ##pp Grit::GitRuby.cache_client.stats
+# test line
</pre>

<p>and <code>git diff --cached</code> to see what you’ve staged so far:</p>

<pre>
$ git diff --cached
diff --git a/benchmarks.rb b/benchmarks.rb
index 3cb747f..e445e28 100644
--- a/benchmarks.rb
+++ b/benchmarks.rb
@@ -36,6 +36,10 @@ def main
          @commit.parents[0].parents[0].parents[0]
        end

+        run_code(x, 'commits 1') do
+          git.commits.size
+        end
+
        run_code(x, 'commits 2') do
          log = git.commits('master', 15)
          log.size
</pre>

<h3>Committing Your Changes</h3>

<p>
  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 <code>git add</code> on since you edited them — won’t go into this commit. They will stay as
  modified files on your disk. In this case, the last time you ran <code>git status</code>, you saw that everything was staged, so you’re ready to commit your changes.
  The simplest way to commit is to type <code>git commit</code>:
</p>

<pre>
$ git commit
</pre>

<p>
  Doing so launches your editor of choice. (This is set by your shell’s <code>$EDITOR</code> environment variable — usually vim or emacs, although you can configure it
  with whatever you want using the <code>git config --global core.editor</code> command as you saw in <i>Chapter 1</i>).
</p>

<p>The editor displays the following text (this example is a Vim screen):</p>

<pre>
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#       new file:   README
#       modified:   benchmarks.rb
#
~
~
~
".git/COMMIT_EDITMSG" 10L, 283C
</pre>

<p>
  You can see that the default commit message contains the latest output of the
  <code>git status</code> 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 <code>-v</code> option to <code>git commit</code>. 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).
</p>

<p>
  Alternatively, you can type your commit message inline with the <code>commit</code> command by specifying it after a <code>-m</code> flag, like this:
</p>

<pre>
$ git commit -m "Story 182: Fix benchmarks for speed"
[master 463dc4f] Story 182: Fix benchmarks for speed
 2 files changed, 3 insertions(+)
 create mode 100644 README
</pre>

<p>
  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 (<code>master</code>), what SHA-1 checksum the commit has (<code>463dc4f</code>), how many files were changed, and statistics about lines added and removed
  in the commit.
</p>

<p>
  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.
</p>

<h3>Skipping the Staging Area</h3>

<p>
  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 <code>-a</code> option to the <code>git commit</code> command makes Git automatically stage every file that is already tracked
  before doing the commit, letting you skip the <code>git add</code> part:
</p>

<pre>
$ git status
On branch master
Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        modified:   benchmarks.rb

no changes added to commit (use "git add" and/or "git commit -a")
$ git commit -a -m 'added new benchmarks'
[master 83e38c7] added new benchmarks
 1 files changed, 5 insertions(+)
</pre>

<p>
  Notice how you don’t have to run <code>git add</code> on the <code>benchmarks.rb</code> file in this case before you commit.
</p>

<h3>Removing Files</h3>

<p>
  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 <code>git rm</code> 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.
</p>

<p>
  If you simply remove the file from your working directory, it shows up under
  the “Changes not staged for commit” (that is, <i>unstaged</i>) area of your <code>git status</code> output:
</p>

<pre>
$ rm grit.gemspec
$ git status
On branch master
Changes not staged for commit:
  (use "git add/rm &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        deleted:    grit.gemspec

no changes added to commit (use "git add" and/or "git commit -a")
</pre>

<p>Then, if you run <code>git rm</code>, it stages the file’s removal:</p>

<pre>
$ git rm grit.gemspec
rm 'grit.gemspec'
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        deleted:    grit.gemspec
</pre>

<p>
  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 <code>-f</code> 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.
</p>

<p>
  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 <code>.gitignore</code> file and accidentally staged it, like a large log file or a bunch of <code>.a</code> compiled files. To do this, use the <code>--cached</code> option:
</p>

<pre>
$ git rm --cached readme.txt
</pre>

<p>
  You can pass files, directories, and file-glob patterns to the <code>git rm</code> command. That means you can do things such as
</p>

<pre>
$ git rm log/\*.log
</pre>

<p>
  Note the backslash (<code>\</code>) in front of the <code>*</code>. This is necessary because Git does its own filename expansion in addition
  to your shell’s filename expansion. On Windows with the system console, the
  backslash must be omitted. This command removes all files that have the <code>.log</code> extension in the <code>log/</code> directory. Or, you can do something like this:
</p>

<pre>
$ git rm \*~
</pre>

<p>This command removes all files that end with <code>~</code>.</p>

<h3>Moving Files</h3>

<p>
  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.
</p>

<p>
  Thus it’s a bit confusing that Git has a <code>mv</code> command. If you want to rename a file in Git, you can run something like
</p>

<pre>
$ git mv file_from file_to
</pre>

<p>
  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:
</p>

<pre>
$ git mv README README.txt
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        renamed:    README -&gt; README.txt
</pre>

<p>However, this is equivalent to running something like this:</p>

<pre>
$ mv README README.txt
$ git rm README
$ git add README.txt
</pre>

<p>
  Git figures out that it’s a rename implicitly, so it doesn’t matter if you
  rename a file that way or with the <code>mv</code> command. The only real difference is that <code>mv</code> 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.
</p>

<h2>Viewing the Commit History</h2>

<p>
  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 <code>git log</code> command.
</p>

<p>
  These examples use a very simple project called <code>simplegit</code> that I often use for demonstrations. To get the project, run
</p>

<pre>
git clone git://github.com/schacon/simplegit-progit.git
</pre>

<p>
  When you run <code>git log</code> in this project, you should get output that looks something like this:
</p>

<pre>
$ git log
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test code

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sat Mar 15 10:31:28 2008 -0700

    first commit
</pre>

<p>
  By default, with no arguments, <code>git log</code> 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.
</p>

<p>
  A huge number and variety of options to the <code>git log</code> command are available to show you exactly what you’re looking for. Here,
  we’ll show you some of the most-used options.
</p>

<p>
  One of the more helpful options is <code>-p</code>, which shows the diff introduced in each commit. You can also use <code>-2</code>, which limits the output to only the last two entries:
</p>

<pre>
$ git log -p -2
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -5,5 +5,5 @@ require 'rake/gempackagetask'
 spec = Gem::Specification.new do |s|
     s.name      =   "simplegit"
-    s.version   =   "0.1.0"
+    s.version   =   "0.1.1"
     s.author    =   "Scott Chacon"
     s.email     =   "schacon@gee-mail.com

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test code

diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index a0a60ae..47c6340 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -18,8 +18,3 @@ class SimpleGit
     end

 end
-
-if $0 == __FILE__
-  git = SimpleGit.new
-  puts git.show
-end
\ No newline at end of file
</pre>

<p>
  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.
</p>

<p>
  Sometimes it's easier to review changes on the word level rather than on the
  line level. There is a <code>--word-diff</code> option available in Git, that you can append to the <code>git log -p</code> command to get word diff instead of normal line by line diff. Word diff
  format is quite useless when applied to source code, but it comes in handy
  when applied to large text files, like books or your dissertation. Here is an
  example:
</p>

<pre>
$ git log -U1 --word-diff
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

diff --git a/Rakefile b/Rakefile
index a874b73..8f94139 100644
--- a/Rakefile
+++ b/Rakefile
@@ -7,3 +7,3 @@ spec = Gem::Specification.new do |s|
    s.name      =   "simplegit"
    s.version   =   [-"0.1.0"-]{+"0.1.1"+}
    s.author    =   "Scott Chacon"
</pre>

<p>
  As you can see, there is no added and removed lines in this output as in a
  normal diff. Changes are shown inline instead. You can see the added word
  enclosed in <code>{+ +}</code> and removed one enclosed in <code>[- -]</code>. You may also want to reduce the usual three lines context in diff output to
  only one line, as the context is now words, not lines. You can do this with <code>-U1</code> as we did in the example above.
</p>

<p>
  You can also use a series of summarizing options with <code>git log</code>. For example, if you want to see some abbreviated stats for each commit, you
  can use the <code>--stat</code> option:
</p>

<pre>
$ git log --stat
commit ca82a6dff817ec66f44342007202690a93763949
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Mon Mar 17 21:52:11 2008 -0700

    changed the version number

 Rakefile |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

commit 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sat Mar 15 16:40:33 2008 -0700

    removed unnecessary test code

 lib/simplegit.rb |    5 -----
 1 file changed, 5 deletions(-)

commit a11bef06a3f659402fe7563abf99ad00de2209e6
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sat Mar 15 10:31:28 2008 -0700

    first commit

 README           |    6 ++++++
 Rakefile         |   23 +++++++++++++++++++++++
 lib/simplegit.rb |   25 +++++++++++++++++++++++++
 3 files changed, 54 insertions(+)
</pre>

<p>
  As you can see, the <code>--stat</code> 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. Another really useful
  option is <code>--pretty</code>. This option changes the log output to formats other than the default. A few
  prebuilt options are available for you to use. The <code>oneline</code> option prints each commit on a single line, which is useful if you’re
  looking at a lot of commits. In addition, the <code>short</code>, <code>full</code>, and <code>fuller</code> options show the output in roughly the same format but with less or more
  information, respectively:
</p>

<pre>
$ git log --pretty=oneline
ca82a6dff817ec66f44342007202690a93763949 changed the version number
085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 removed unnecessary test code
a11bef06a3f659402fe7563abf99ad00de2209e6 first commit
</pre>

<p>
  The most interesting option is <code>format</code>, 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:
</p>

<pre>
$ git log --pretty=format:"%h - %an, %ar : %s"
ca82a6d - Scott Chacon, 11 months ago : changed the version number
085bb3b - Scott Chacon, 11 months ago : removed unnecessary test code
a11bef0 - Scott Chacon, 11 months ago : first commit
</pre>

<p>Table 2-1 lists some of the more useful options that format takes.</p>

<pre>
Option	Description of Output
%H	Commit hash
%h	Abbreviated commit hash
%T	Tree hash
%t	Abbreviated tree hash
%P	Parent hashes
%p	Abbreviated parent hashes
%an	Author name
%ae	Author e-mail
%ad	Author date (format respects the --date= option)
%ar	Author date, relative
%cn	Committer name
%ce	Committer email
%cd	Committer date
%cr	Committer date, relative
%s	Subject
</pre>

<p>
  You may be wondering what the difference is between <i>author</i> and <i>committer</i>. The <i>author</i> is the person who originally wrote the patch, whereas the <i>committer</i> is the person who last applied the patch. 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 <i>Chapter 5</i>.
</p>

<p>
  The <code>oneline</code> and <code>format</code> options are particularly useful with another <code>log</code> option called <code>--graph</code>. This option adds a nice little ASCII graph showing your branch and merge
  history, which we can see in our copy of the Grit project repository:
</p>

<pre>
$ git log --pretty=format:"%h %s" --graph
* 2d3acf9 ignore errors from SIGCHLD on trap
*  5e3ee11 Merge branch 'master' of git://github.com/dustin/grit
|\
| * 420eac9 Added a method for getting the current branch.
* | 30e367c timeout code and tests
* | 5a09431 add timeout protection to grit
* | e1193f8 support for heads with slashes in them
|/
* d6016bc require time for xmlschema
*  11d191e Merge branch 'defunkt' into local
</pre>

<p>
  Those are only some simple output-formatting options to <code>git log</code> — 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 <code>log</code> command.
</p>

<pre>
Option	Description
-p	Show the patch introduced with each commit.
--word-diff	Show the patch in a word diff format.
--stat	Show statistics for files modified in each commit.
--shortstat	Display only the changed/insertions/deletions line from the --stat command.
--name-only	Show the list of files modified after the commit information.
--name-status	Show the list of files affected with added/modified/deleted information as well.
--abbrev-commit	Show only the first few characters of the SHA-1 checksum instead of all 40.
--relative-date	Display the date in a relative format (for example, “2 weeks ago”) instead of using the full date format.
--graph	Display an ASCII graph of the branch and merge history beside the log output.
--pretty	Show commits in an alternate format. Options include oneline, short, full, fuller, and format (where you specify your own format).
--oneline	A convenience option short for `--pretty=oneline --abbrev-commit`.
</pre>

<h3>Limiting Log Output</h3>

<p>
  In addition to output-formatting options, <code>git log</code> 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 <code>-2</code> option, which shows only the last two commits. In fact, you can do <code>-&lt;n></code>, where <code>n</code> is any integer to show the last <code>n</code> 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.
</p>

<p>
  However, the time-limiting options such as <code>--since</code> and <code>--until</code> are very useful. For example, this command gets the list of commits made in
  the last two weeks:
</p>

<pre>
$ git log --since=2.weeks
</pre>

<p>
  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”.
</p>

<p>
  You can also filter the list to commits that match some search criteria. The <code>--author</code> option allows you to filter on a specific author, and the <code>--grep</code> option lets you search for keywords in the commit messages. (Note that if
  you specify both author and grep options, the command will match commits with
  both.)
</p>

<p>
  If you want to specify multiple grep options, you have to add <code>--all-match</code> or the command will match commits with either.
</p>

<p>
  The last really useful option to pass to <code>git log</code> 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 (<code>--</code>) to separate the paths from the options.
</p>

<p>
  In Table 2-3 we’ll list these and a few other common options for your
  reference.
</p>

<pre>
Option	Description
-(n)	Show only the last n commits
--since, --after	Limit the commits to those whose CommitDate was made on-or-after the specified date/time.
--until, --before	Limit the commits to those whose CommitDate was made on-or-before the specified date/time.
--author	Only show commits in which the author entry matches the specified string.
--committer	Only show commits in which the committer entry matches the specified string.
</pre>

<h3>Limiting Log Output according to Date/Time</h3>

<p>
  To determine which commits in the Git source code repository
  (git://git.kernel.org/pub/scm/git/git.git) have CommitDate on 2014-04-29
  relative to your local timezone (as set on your computer), use
</p>

<pre>
$ git log --after="2014-04-29 00:00:00" --before="2014-04-29 23:59:59" \
  --pretty=fuller
</pre>

<p>
  As the output will be different according to the timezone where it will be
  run, it's recommended to always use an absolute time such as ISO 8601 format
  (which includes timezone information) as argument to <code>--after</code> and <code>--before</code>, so that everone running the command will get the same repeatable results.
</p>

<p>
  To obtain commits made at a specific instant in time (e.g. 29 April 2013 at
  17:07:22 CET), we can use
</p>

<pre>
$ git log  --after="2013-04-29T17:07:22+0200"      \
          --before="2013-04-29T17:07:22+0200" --pretty=fuller

commit de7c201a10857e5d424dbd8db880a6f24ba250f9
Author:     Ramkumar Ramachandra &lt;artagnon@gmail.com&gt;
AuthorDate: Mon Apr 29 18:19:37 2013 +0530
Commit:     Junio C Hamano &lt;gitster@pobox.com&gt;
CommitDate: Mon Apr 29 08:07:22 2013 -0700

    git-completion.bash: lexical sorting for diff.statGraphWidth
    
    df44483a (diff --stat: add config option to limit graph width,
    2012-03-01) added the option diff.startGraphWidth to the list of
    configuration variables in git-completion.bash, but failed to notice
    that the list is sorted alphabetically.  Move it to its rightful place
    in the list.
    
    Signed-off-by: Ramkumar Ramachandra &lt;artagnon@gmail.com&gt;
    Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;
</pre>

<p>
  The above times (<code>AuthorDate</code>, <code>CommitDate</code>) are displayed in default format (<code>--date=default</code>), which shows timezone information of respective author and commiter.
</p>

<p>
  Other useful formats include <code>--date=iso</code> (ISO 8601), <code>--date=rfc</code> (RFC 2822), <code>--date=raw</code> (seconds since the epoch (1970-01-01 UTC)) <code>--date=local</code> (times according to your local timezone) as well as <code>--date=relative</code> (e.g. "2 hours ago").
</p>

<p>
  When using <code>git log</code> without specifying time, the time defaults to the time at which the command
  is run on your computer (keeping the identical offset from UTC).
</p>

<p>
  For example, running a <code>git log</code> at 09:00 on your computer with your timezone currently 3 hours ahead of UTC,
  makes the following two commands equivalent:
</p>

<pre>
$ git log --after=2008-06-01 --before=2008-07-01
$ git log --after="2008-06-01T09:00:00+0300" \
    --before="2008-07-01T09:00:00+0300"
</pre>

<p>
  As a final example, if you want to see which commits modifying test files in
  the Git source code history were committed by Junio Hamano with CommitDate
  being in the month of October 2008 (relative to the timezone of New York) and
  were not merges, you can run something like this:
</p>

<pre>
    $ git log --pretty="%h - %s" --author=gitster \
       --after="2008-10-01T00:00:00-0400"         \
      --before="2008-10-31T23:59:59-0400" --no-merges -- t/
5610e3b - Fix testcase failure when extended attribute
acd3b9e - Enhance hold_lock_file_for_{update,append}()
f563754 - demonstrate breakage of detached checkout wi
d1a43f2 - reset --hard/read-tree --reset -u: remove un
51a94af - Fix "checkout --track -b newbranch" on detac
b0ad11e - pull: allow "git pull origin $something:$cur
</pre>

<p>
  Of the more than 36,000 commits in the Git source code history, this command
  shows the 6 that match those criteria.
</p>

<h3>Using a GUI to Visualize History</h3>

<p>
  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 <code>gitk</code> that is distributed with Git. Gitk is basically a visual <code>git log</code> tool, and it accepts nearly all the filtering options that <code>git log</code> does. If you type <code>gitk</code> on the command line in your project, you should see something like Figure
  2-2.
</p>

<p>Insert 18333fig0202.png Figure 2-2. The gitk history visualizer.</p>

<p>
  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.
</p>

<h2>Undoing Things</h2>

<p>
  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 revert some of these undos. This is one of the few areas in Git where
  you may lose some work if you do it wrong.
</p>

<h3>Changing Your Last Commit</h3>

<p>
  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 <code>--amend</code> option:
</p>

<pre>
$ git commit --amend
</pre>

<p>
  This command takes your staging area and uses it for the commit. If you’ve
  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.
</p>

<p>
  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.
</p>

<p>
  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:
</p>

<pre>
$ git commit -m 'initial commit'
$ git add forgotten_file
$ git commit --amend
</pre>

<p>
  After these three commands, you end up with a single commit — the second
  commit replaces the results of the first.
</p>

<h3>Unstaging a Staged File</h3>

<p>
  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 <code>git add *</code> and stage them both. How can you unstage one of the two? The <code>git status</code> command reminds you:
</p>

<pre>
$ git add .
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        modified:   README.txt
        modified:   benchmarks.rb
</pre>

<p>
  Right below the “Changes to be committed” text, it says "use <code>git reset HEAD &lt;file>...</code> to unstage". So, let’s use that advice to unstage the <code>benchmarks.rb</code> file:
</p>

<pre>
$ git reset HEAD benchmarks.rb
Unstaged changes after reset:
M       benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        modified:   README.txt

Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        modified:   benchmarks.rb
</pre>

<p>
  The command is a bit strange, but it works. The <code>benchmarks.rb</code> file is modified but once again unstaged.
</p>

<h3>Unmodifying a Modified File</h3>

<p>
  What if you realize that you don’t want to keep your changes to the <code>benchmarks.rb</code> 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, <code>git status</code> tells you how to do that, too. In the last example output, the unstaged area
  looks like this:
</p>

<pre>
Changes not staged for commit:
  (use "git add &lt;file&gt;..." to update what will be committed)
  (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)

        modified:   benchmarks.rb
</pre>

<p>
  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:
</p>

<pre>
$ git checkout -- benchmarks.rb
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        modified:   README.txt
</pre>

<p>
  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.
</p>

<p>
  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 <code>--amend</code> commit can be recovered (see <i>Chapter 9</i> for data recovery). However, anything you lose that was never committed is
  likely never to be seen again.
</p>

<h2>Working with Remotes</h2>

<p>
  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.
  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.
</p>

<h3>Showing Your Remotes</h3>

<p>
  To see which remote servers you have configured, you can run the <code>git remote</code> command. It lists the shortnames of each remote handle you’ve specified. If
  you’ve cloned your repository, you should at least see <i>origin</i> — that is the default name Git gives to the server you cloned from:
</p>

<pre>
$ git clone git://github.com/schacon/ticgit.git
Cloning into 'ticgit'...
remote: Reusing existing pack: 1857, done.
remote: Total 1857 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (1857/1857), 374.35 KiB | 193.00 KiB/s, done.
Resolving deltas: 100% (772/772), done.
Checking connectivity... done.
$ cd ticgit
$ git remote
origin
</pre>

<p>
  You can also specify <code>-v</code>, which shows you the URL that Git has stored for the shortname to be
  expanded to:
</p>

<pre>
$ git remote -v
origin  git://github.com/schacon/ticgit.git (fetch)
origin  git://github.com/schacon/ticgit.git (push)
</pre>

<p>
  If you have more than one remote, the command lists them all. For example, my
  Grit repository looks something like this.
</p>

<pre>
$ cd grit
$ git remote -v
bakkdoor  git://github.com/bakkdoor/grit.git
cho45     git://github.com/cho45/grit.git
defunkt   git://github.com/defunkt/grit.git
koke      git://github.com/koke/grit.git
origin    git@github.com:mojombo/grit.git
</pre>

<p>
  This means I 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 <i>Chapter 4</i>).
</p>

<h3>Adding Remote Repositories</h3>

<p>
  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 <code>git remote add [shortname] [url]</code>:
</p>

<pre>
$ git remote
origin
$ git remote add pb git://github.com/paulboone/ticgit.git
$ git remote -v
origin	git://github.com/schacon/ticgit.git
pb	git://github.com/paulboone/ticgit.git
</pre>

<p>
  Now you can use the string <code>pb</code> 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 <code>git fetch pb</code>:
</p>

<pre>
$ git fetch pb
remote: Counting objects: 58, done.
remote: Compressing objects: 100% (41/41), done.
remote: Total 44 (delta 24), reused 1 (delta 0)
Unpacking objects: 100% (44/44), done.
From git://github.com/paulboone/ticgit
 * [new branch]      master     -&gt; pb/master
 * [new branch]      ticgit     -&gt; pb/ticgit
</pre>

<p>
  Paul’s master branch is accessible locally as <code>pb/master</code> — 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.
</p>

<h3>Fetching and Pulling from Your Remotes</h3>

<p>As you just saw, to get data from your remote projects, you can run:</p>

<pre>
$ git fetch [remote-name]
</pre>

<p>
  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 <i>Chapter 3</i>.)
</p>

<p>
  If you clone a repository, the command automatically adds that remote
  repository under the name <i>origin</i>. So, <code>git fetch origin</code> 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 <code>fetch</code> 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.
</p>

<p>
  If you have a branch set up to track a remote branch (see the next section
  and <i>Chapter 3</i> for more information), you can use the <code>git pull</code> 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 <code>git clone</code> 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 <code>git pull</code> generally fetches data from the server you originally cloned from and
  automatically tries to merge it into the code you’re currently working on.
</p>

<h3>Pushing to Your Remotes</h3>

<p>
  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: <code>git push [remote-name] [branch-name]</code>. If you want to push your master branch to your <code>origin</code> 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:
</p>

<pre>
$ git push origin master
</pre>

<p>
  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 <i>Chapter 3</i> for more detailed information on how to push to remote servers.
</p>

<h3>Inspecting a Remote</h3>

<p>
  If you want to see more information about a particular remote, you can use
  the <code>git remote show [remote-name]</code> command. If you run this command with a particular shortname, such as <code>origin</code>, you get something like this:
</p>

<pre>
$ git remote show origin
* remote origin
  URL: git://github.com/schacon/ticgit.git
  Remote branch merged with 'git pull' while on branch master
    master
  Tracked remote branches
    master
    ticgit
</pre>

<p>
  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 <code>git pull</code>, 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.
</p>

<p>
  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 <code>git remote show</code>:
</p>

<pre>
$ git remote show origin
* remote origin
  URL: git@github.com:defunkt/github.git
  Remote branch merged with 'git pull' while on branch issues
    issues
  Remote branch merged with 'git pull' while on branch master
    master
  New remote branches (next fetch will store in remotes/origin)
    caching
  Stale tracking branches (use 'git remote prune')
    libwalker
    walker2
  Tracked remote branches
    acl
    apiv2
    dashboard2
    issues
    master
    postgres
  Local branch pushed with 'git push'
    master:master
</pre>

<p>
  This command shows which branch is automatically pushed when you run <code>git push</code> 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 <code>git pull</code>.
</p>

<h3>Removing and Renaming Remotes</h3>

<p>
  If you want to rename a reference, in newer versions of Git you can run <code>git remote rename</code> to change a remote’s shortname. For instance, if you want to rename <code>pb</code> to <code>paul</code>, you can do so with <code>git remote rename</code>:
</p>

<pre>
$ git remote rename pb paul
$ git remote
origin
paul
</pre>

<p>
  It’s worth mentioning that this changes your remote branch names, too. What
  used to be referenced at <code>pb/master</code> is now at <code>paul/master</code>.
</p>

<p>
  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 <code>git remote rm</code>:
</p>

<pre>
$ git remote rm paul
$ git remote
origin
</pre>

<h2>Tagging</h2>

<p>
  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 (<code>v1.0</code>, 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.
</p>

<h3>Listing Your Tags</h3>

<p>
  Listing the available tags in Git is straightforward. Just type <code>git tag</code>:
</p>

<pre>
$ git tag
v0.1
v1.3
</pre>

<p>
  This command lists the tags in alphabetical order; the order in which they
  appear has no real importance.
</p>

<p>
  You can also search for tags with a particular pattern. The Git source repo,
  for instance, contains more than 240 tags. If you’re only interested in
  looking at the 1.4.2 series, you can run this:
</p>

<pre>
$ git tag -l 'v1.4.2.*'
v1.4.2.1
v1.4.2.2
v1.4.2.3
v1.4.2.4
</pre>

<h3>Creating Tags</h3>

<p>
  Git uses two main types of tags: lightweight and annotated. A lightweight tag
  is very much like a branch that doesn’t change — it’s just a pointer to a
  specific commit. Annotated tags, however, are stored as full objects in the
  Git database. They’re checksummed; contain the tagger name, e-mail, and date;
  have a tagging message; and can be signed and verified with GNU Privacy Guard
  (GPG). It’s generally recommended that you create annotated tags so you can
  have all this information; but if you want a temporary tag or for some reason
  don’t want to keep the other information, lightweight tags are available too.
</p>

<h3>Annotated Tags</h3>

<p>
  Creating an annotated tag in Git is simple. The easiest way is to specify <code>-a</code> when you run the <code>tag</code> command:
</p>

<pre>
$ git tag -a v1.4 -m 'my version 1.4'
$ git tag
v0.1
v1.3
v1.4
</pre>

<p>
  The <code>-m</code> specifies a tagging message, which is stored with the tag. If you don’t
  specify a message for an annotated tag, Git launches your editor so you can
  type it in.
</p>

<p>
  You can see the tag data along with the commit that was tagged by using the <code>git show</code> command:
</p>

<pre>
$ git show v1.4
tag v1.4
Tagger: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Mon Feb 9 14:45:11 2009 -0800

my version 1.4

commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97...
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sun Feb 8 19:02:46 2009 -0800

    Merge branch 'experiment'
</pre>

<p>
  That shows the tagger information, the date the commit was tagged, and the
  annotation message before showing the commit information.
</p>

<h3>Signed Tags</h3>

<p>
  You can also sign your tags with GPG, assuming you have a private key. All
  you have to do is use <code>-s</code> instead of <code>-a</code>:
</p>

<pre>
$ git tag -s v1.5 -m 'my signed 1.5 tag'
You need a passphrase to unlock the secret key for
user: "Scott Chacon &lt;schacon@gee-mail.com&gt;"
1024-bit DSA key, ID F721C45A, created 2009-02-09
</pre>

<p>
  If you run <code>git show</code> on that tag, you can see your GPG signature attached to it:
</p>

<pre>
$ git show v1.5
tag v1.5
Tagger: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Mon Feb 9 15:22:20 2009 -0800

my signed 1.5 tag
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEABECAAYFAkmQurIACgkQON3DxfchxFr5cACeIMN+ZxLKggJQf0QYiQBwgySN
Ki0An2JeAVUCAiJ7Ox6ZEtK+NvZAj82/
=WryJ
-----END PGP SIGNATURE-----
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97...
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sun Feb 8 19:02:46 2009 -0800

    Merge branch 'experiment'
</pre>

<p>A bit later, you’ll learn how to verify signed tags.</p>

<h3>Lightweight Tags</h3>

<p>
  Another way to tag commits is with a lightweight tag. This is basically the
  commit checksum stored in a file — no other information is kept. To create a
  lightweight tag, don’t supply the <code>-a</code>, <code>-s</code>, or <code>-m</code> option:
</p>

<pre>
$ git tag v1.4-lw
$ git tag
v0.1
v1.3
v1.4
v1.4-lw
v1.5
</pre>

<p>
  This time, if you run <code>git show</code> on the tag, you don’t see the extra tag information. The command just shows
  the commit:
</p>

<pre>
$ git show v1.4-lw
commit 15027957951b64cf874c3557a0f3547bd83b3ff6
Merge: 4a447f7... a6b4c97...
Author: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Sun Feb 8 19:02:46 2009 -0800

    Merge branch 'experiment'
</pre>

<h3>Verifying Tags</h3>

<p>
  To verify a signed tag, you use <code>git tag -v [tag-name]</code>. This command uses GPG to verify the signature. You need the signer’s public
  key in your keyring for this to work properly:
</p>

<pre>
$ git tag -v v1.4.2.1
object 883653babd8ee7ea23e6a5c392bb739348b1eb61
type commit
tag v1.4.2.1
tagger Junio C Hamano &lt;junkio@cox.net&gt; 1158138501 -0700

GIT 1.4.2.1

Minor fixes since 1.4.2, including git-mv and git-http with alternates.
gpg: Signature made Wed Sep 13 02:08:25 2006 PDT using DSA key ID F3119B9A
gpg: Good signature from "Junio C Hamano &lt;junkio@cox.net&gt;"
gpg:                 aka "[jpeg image of size 1513]"
Primary key fingerprint: 3565 2A26 2040 E066 C9A7  4A7D C0C6 D9A4 F311 9B9A
</pre>

<p>
  If you don’t have the signer’s public key, you get something like this
  instead:
</p>

<pre>
gpg: Signature made Wed Sep 13 02:08:25 2006 PDT using DSA key ID F3119B9A
gpg: Can't check signature: public key not found
error: could not verify the tag 'v1.4.2.1'
</pre>

<h3>Tagging Later</h3>

<p>
  You can also tag commits after you’ve moved past them. Suppose your commit
  history looks like this:
</p>

<pre>
$ git log --pretty=oneline
15027957951b64cf874c3557a0f3547bd83b3ff6 Merge branch 'experiment'
a6b4c97498bd301d84096da251c98a07c7723e65 beginning write support
0d52aaab4479697da7686c15f77a3d64d9165190 one more thing
6d52a271eda8725415634dd79daabbc4d9b6008e Merge branch 'experiment'
0b7434d86859cc7b8c3d5e1dddfed66ff742fcbc added a commit function
4682c3261057305bdd616e23b64b0857d832627b added a todo file
166ae0c4d3f420721acbb115cc33848dfcc2121a started write support
9fceb02d0ae598e95dc970b74767f19372d61af8 updated rakefile
964f16d36dfccde844893cac5b347e7b3d44abbc commit the todo
8a5cbc430f1a9c3d00faaeffd07798508422908a updated readme
</pre>

<p>
  Now, suppose you forgot to tag the project at <code>v1.2</code>, which was at the "updated rakefile" commit. You can add it after the fact.
  To tag that commit, you specify the commit checksum (or part of it) at the
  end of the command:
</p>

<pre>
$ git tag -a v1.2 -m 'version 1.2' 9fceb02
</pre>

<p>You can see that you’ve tagged the commit:</p>

<pre>
$ git tag
v0.1
v1.2
v1.3
v1.4
v1.4-lw
v1.5

$ git show v1.2
tag v1.2
Tagger: Scott Chacon &lt;schacon@gee-mail.com&gt;
Date:   Mon Feb 9 15:32:16 2009 -0800

version 1.2
commit 9fceb02d0ae598e95dc970b74767f19372d61af8
Author: Magnus Chacon &lt;mchacon@gee-mail.com&gt;
Date:   Sun Apr 27 20:43:35 2008 -0700

    updated rakefile
...
</pre>

<h3>Sharing Tags</h3>

<p>
  By default, the <code>git push</code> command doesn’t transfer tags to remote servers. You will have to explicitly
  push tags to a shared server after you have created them.&emsp14;&emsp14;This
  process is just like sharing remote branches — you can run <code>git push origin [tagname]</code>.
</p>

<pre>
$ git push origin v1.5
Counting objects: 50, done.
Compressing objects: 100% (38/38), done.
Writing objects: 100% (44/44), 4.56 KiB, done.
Total 44 (delta 18), reused 8 (delta 1)
To git@github.com:schacon/simplegit.git
* [new tag]         v1.5 -&gt; v1.5
</pre>

<p>
  If you have a lot of tags that you want to push up at once, you can also use
  the <code>--tags</code> option to the <code>git push</code> command.&emsp14;&emsp14;This will transfer all of your tags to the remote
  server that are not already there.
</p>

<pre>
$ git push origin --tags
Counting objects: 50, done.
Compressing objects: 100% (38/38), done.
Writing objects: 100% (44/44), 4.56 KiB, done.
Total 44 (delta 18), reused 8 (delta 1)
To git@github.com:schacon/simplegit.git
 * [new tag]         v0.1 -&gt; v0.1
 * [new tag]         v1.2 -&gt; v1.2
 * [new tag]         v1.4 -&gt; v1.4
 * [new tag]         v1.4-lw -&gt; v1.4-lw
 * [new tag]         v1.5 -&gt; v1.5
</pre>

<p>
  Now, when someone else clones or pulls from your repository, they will get
  all your tags as well.
</p>

<h2>Tips and Tricks</h2>

<p>
  Before we finish this chapter on basic Git, a few little tips and tricks may
  make your Git experience a bit simpler, easier, or more familiar. Many people
  use Git without using any of these tips, and we won’t refer to them or assume
  you’ve used them later in the book; but you should probably know how to do
  them.
</p>

<h3>Auto-Completion</h3>

<p>
  If you use the Bash shell, Git comes with a nice auto-completion script you
  can enable. Download it directly from the Git source code at
  https://github.com/git/git/blob/master/contrib/completion/git-completion.bash
  . Copy this file to your home directory, and add this to your <code>.bashrc</code> file:
</p>

<pre>
source ~/git-completion.bash
</pre>

<p>
  If you want to set up Git to automatically have Bash shell completion for all
  users, copy this script to the <code>/opt/local/etc/bash_completion.d</code> directory on Mac systems or to the <code>/etc/bash_completion.d/</code> directory on Linux systems. This is a directory of scripts that Bash will
  automatically load to provide shell completions.
</p>

<p>
  If you’re using Windows with Git Bash, which is the default when installing
  Git on Windows with msysGit, auto-completion should be preconfigured.
</p>

<p>
  Press the Tab key when you’re writing a Git command, and it should return a
  set of suggestions for you to pick from:
</p>

<pre>
$ git co&lt;tab&gt;&lt;tab&gt;
commit config
</pre>

<p>
  In this case, typing <code>git co</code> and then pressing the Tab key twice suggests commit and config. Adding <code>m&lt;tab></code> completes <code>git commit</code> automatically.
</p>

<p>
  This also works with options, which is probably more useful. For instance, if
  you’re running a <code>git log</code> command and can’t remember one of the options, you can start typing it and
  press Tab to see what matches:
</p>

<pre>
$ git log --s&lt;tab&gt;&lt;tab&gt;
--shortstat               --sparse
--simplify-by-decoration  --src-prefix=
--simplify-merges         --stat
--since=                  --summary
</pre>

<p>
  That’s a pretty nice trick and may save you some time and documentation
  reading.
</p>

<h3>Git Aliases</h3>

<p>
  Git doesn’t infer your command if you type it in partially. If you don’t want
  to type the entire text of each of the Git commands, you can easily set up an
  alias for each command using <code>git config</code>. Here are a couple of examples you may want to set up:
</p>

<pre>
$ git config --global alias.co checkout
$ git config --global alias.br branch
$ git config --global alias.ci commit
$ git config --global alias.st status
</pre>

<p>
  This means that, for example, instead of typing <code>git commit</code>, you just need to type <code>git ci</code>. As you go on using Git, you’ll probably use other commands frequently as
  well; in this case, don’t hesitate to create new aliases.
</p>

<p>
  This technique can also be very useful in creating commands that you think
  should exist. For example, to correct the usability problem you encountered
  with unstaging a file, you can add your own unstage alias to Git:
</p>

<pre>
$ git config --global alias.unstage 'reset HEAD --'
</pre>

<p>This makes the following two commands equivalent:</p>

<pre>
$ git unstage fileA
$ git reset HEAD fileA
</pre>

<p>
  This seems a bit clearer. It’s also common to add a <code>last</code> command, like this:
</p>

<pre>
$ git config --global alias.last 'log -1 HEAD'
</pre>

<p>This way, you can see the last commit easily:</p>

<pre>
$ git last
commit 66938dae3329c7aebe598c2246a8e6af90d04646
Author: Josh Goebel &lt;dreamer3@example.com&gt;
Date:   Tue Aug 26 19:48:51 2008 +0800

    test for current head

    Signed-off-by: Scott Chacon &lt;schacon@example.com&gt;
</pre>

<p>
  As you can tell, Git simply replaces the new command with whatever you alias
  it to. However, maybe you want to run an external command, rather than a Git
  subcommand. In that case, you start the command with a <code>!</code> character. This is useful if you write your own tools that work with a Git
  repository. We can demonstrate by aliasing <code>git visual</code> to run <code>gitk</code>:
</p>

<pre>
$ git config --global alias.visual '!gitk'
</pre>

<h2>Summary</h2>

<p>
  At this point, you can do all the basic local Git operations — creating or
  cloning a repository, making changes, staging and committing those changes,
  and viewing the history of all the changes the repository has been through.
  Next, we’ll cover Git’s killer feature: its branching model.
</p>

<h1>Git Branching</h1>

<p>
  Nearly every VCS has some form of branching support. Branching means you
  diverge from the main line of development and continue to do work without
  messing with that main line. In many VCS tools, this is a somewhat expensive
  process, often requiring you to create a new copy of your source code
  directory, which can take a long time for large projects.
</p>

<p>
  Some people refer to the branching model in Git as its “killer
  feature”&emsp14;&emsp14;, and it certainly sets Git apart in the VCS
  community. Why is it so special? The way Git branches is incredibly
  lightweight, making branching operations nearly instantaneous and switching
  back and forth between branches generally just as fast. Unlike many other
  VCSs, Git encourages a workflow that branches and merges often, even multiple
  times in a day. Understanding and mastering this feature gives you a powerful
  and unique tool and can literally change the way that you develop.
</p>

<h2>What a Branch Is</h2>

<p>
  To really understand the way Git does branching, we need to take a step back
  and examine how Git stores its data. As you may remember from Chapter 1, Git
  doesn’t store data as a series of changesets or deltas, but instead as a
  series of snapshots.
</p>

<p>
  When you commit in Git, Git stores a commit object that contains a pointer to
  the snapshot of the content you staged, the author and message metadata, and
  zero or more pointers to the commit or commits that were the direct parents
  of this commit: zero parents for the first commit, one parent for a normal
  commit, and multiple parents for a commit that results from a merge of two or
  more branches.
</p>

<p>
  To visualize this, let’s assume that you have a directory containing three
  files, and you stage them all and commit. Staging the files checksums each
  one (the SHA-1 hash we mentioned in Chapter 1), stores that version of the
  file in the Git repository (Git refers to them as blobs), and adds that
  checksum to the staging area:
</p>

<pre>
$ git add README test.rb LICENSE
$ git commit -m 'initial commit of my project'
</pre>

<p>
  Running <code>git commit</code> checksums all project directories and stores them as <code>tree</code> objects in the Git repository. Git then creates a <code>commit</code> object that has the metadata and a pointer to the root project <code>tree</code> object so it can re-create that snapshot when needed.
</p>

<p>
  Your Git repository now contains five objects: one blob for the contents of
  each of your three files, one tree that lists the contents of the directory
  and specifies which file names are stored as which blobs, and one commit with
  the pointer to that root tree and all the commit metadata. Conceptually, the
  data in your Git repository looks something like Figure 3-1.
</p>

<p>Insert 18333fig0301.png Figure 3-1. Single commit repository data.</p>

<p>
  If you make some changes and commit again, the next commit stores a pointer
  to the commit that came immediately before it. After two more commits, your
  history might look something like Figure 3-2.
</p>

<p>
  Insert 18333fig0302.png Figure 3-2. Git object data for multiple commits.
</p>

<p>
  A branch in Git is simply a lightweight movable pointer to one of these
  commits. The default branch name in Git is master. As you initially make
  commits, you’re given a <code>master</code> branch that points to the last commit you made. Every time you commit, it
  moves forward automatically.
</p>

<p>
  Insert 18333fig0303.png Figure 3-3. Branch pointing into the commit data’s
  history.
</p>

<p>
  What happens if you create a new branch? Well, doing so creates a new pointer
  for you to move around. Let’s say you create a new branch called testing. You
  do this with the <code>git branch</code> command:
</p>

<pre>
$ git branch testing
</pre>

<p>
  This creates a new pointer at the same commit you’re currently on (see Figure
  3-4).
</p>

<p>
  Insert 18333fig0304.png Figure 3-4. Multiple branches pointing into the
  commit’s data history.
</p>

<p>
  How does Git know what branch you’re currently on? It keeps a special pointer
  called HEAD. Note that this is a lot different than the concept of HEAD in
  other VCSs you may be used to, such as Subversion or CVS. In Git, this is a
  pointer to the local branch you’re currently on. In this case, you’re still
  on master. The <code>git branch</code> command only created a new branch — it didn’t switch to that branch (see
  Figure 3-5).
</p>

<p>
  Insert 18333fig0305.png Figure 3-5. HEAD file pointing to the branch you’re
  on.
</p>

<p>
  To switch to an existing branch, you run the <code>git checkout</code> command. Let’s switch to the new testing branch:
</p>

<pre>
$ git checkout testing
</pre>

<p>This moves HEAD to point to the testing branch (see Figure 3-6).</p>

<p>
  Insert 18333fig0306.png Figure 3-6. HEAD points to another branch when you
  switch branches.
</p>

<p>What is the significance of that? Well, let’s do another commit:</p>

<pre>
$ vim test.rb
$ git commit -a -m 'made a change'
</pre>

<p>Figure 3-7 illustrates the result.</p>

<p>
  Insert 18333fig0307.png Figure 3-7. The branch that HEAD points to moves
  forward with each commit.
</p>

<p>
  This is interesting, because now your testing branch has moved forward, but
  your <code>master</code> branch still points to the commit you were on when you ran <code>git checkout</code> to switch branches. Let’s switch back to the <code>master</code> branch:
</p>

<pre>
$ git checkout master
</pre>

<p>Figure 3-8 shows the result.</p>

<p>
  Insert 18333fig0308.png Figure 3-8. HEAD moves to another branch on a
  checkout.
</p>

<p>
  That command did two things. It moved the HEAD pointer back to point to the <code>master</code> branch, and it reverted the files in your working directory back to the
  snapshot that <code>master</code> points to. This also means the changes you make from this point forward will
  diverge from an older version of the project. It essentially rewinds the work
  you’ve done in your testing branch temporarily so you can go in a different
  direction.
</p>

<p>Let’s make a few changes and commit again:</p>

<pre>
$ vim test.rb
$ git commit -a -m 'made other changes'
</pre>

<p>
  Now your project history has diverged (see Figure 3-9). You created and
  switched to a branch, did some work on it, and then switched back to your
  main branch and did other work. Both of those changes are isolated in
  separate branches: you can switch back and forth between the branches and
  merge them together when you’re ready. And you did all that with simple <code>branch</code> and <code>checkout</code> commands.
</p>

<p>Insert 18333fig0309.png Figure 3-9. The branch histories have diverged.</p>

<p>
  Because a branch in Git is in actuality a simple file that contains the 40
  character SHA-1 checksum of the commit it points to, branches are cheap to
  create and destroy. Creating a new branch is as quick and simple as writing
  41 bytes to a file (40 characters and a newline).
</p>

<p>
  This is in sharp contrast to the way most VCS tools branch, which involves
  copying all of the project’s files into a second directory. This can take
  several seconds or even minutes, depending on the size of the project,
  whereas in Git the process is always instantaneous. Also, because we’re
  recording the parents when we commit, finding a proper merge base for merging
  is automatically done for us and is generally very easy to do. These features
  help encourage developers to create and use branches often.
</p>

<p>Let’s see why you should do so.</p>

<h2>Basic Branching and Merging</h2>

<p>
  Let’s go through a simple example of branching and merging with a workflow
  that you might use in the real world. You’ll follow these steps:
</p>

<ol>
  <li>
    <p>Do work on a web site.</p>
  </li>

  <li>
    <p>Create a branch for a new story you’re working on.</p>
  </li>

  <li>
    <p>Do some work in that branch.</p>
  </li>
</ol>

<p>
  At this stage, you’ll receive a call that another issue is critical and you
  need a hotfix. You’ll do the following:
</p>

<ol>
  <li>
    <p>Switch back to your production branch.</p>
  </li>

  <li>
    <p>Create a branch to add the hotfix.</p>
  </li>

  <li>
    <p>After it’s tested, merge the hotfix branch, and push to production.</p>
  </li>

  <li>
    <p>Switch back to your original story and continue working.</p>
  </li>
</ol>

<h3>Basic Branching</h3>

<p>
  First, let’s say you’re working on your project and have a couple of commits
  already (see Figure 3-10).
</p>

<p>Insert 18333fig0310.png Figure 3-10. A short and simple commit history.</p>

<p>
  You’ve decided that you’re going to work on issue #53 in whatever
  issue-tracking system your company uses. To be clear, Git isn’t tied into any
  particular issue-tracking system; but because issue #53 is a focused topic
  that you want to work on, you’ll create a new branch in which to work. To
  create a branch and switch to it at the same time, you can run the <code>git checkout</code> command with the <code>-b</code> switch:
</p>

<pre>
$ git checkout -b iss53
Switched to a new branch 'iss53'
</pre>

<p>This is shorthand for:</p>

<pre>
$ git branch iss53
$ git checkout iss53
</pre>

<p>Figure 3-11 illustrates the result.</p>

<p>Insert 18333fig0311.png Figure 3-11. Creating a new branch pointer.</p>

<p>
  You work on your web site and do some commits. Doing so moves the <code>iss53</code> branch forward, because you have it checked out (that is, your HEAD is
  pointing to it; see Figure 3-12):
</p>

<pre>
$ vim index.html
$ git commit -a -m 'add a new footer [issue 53]'
</pre>

<p>
  Insert 18333fig0312.png Figure 3-12. The iss53 branch has moved forward with
  your work.
</p>

<p>
  Now you get the call that there is an issue with the web site, and you need
  to fix it immediately. With Git, you don’t have to deploy your fix along with
  the <code>iss53</code> changes you’ve made, and you don’t have to put a lot of effort into
  reverting those changes before you can work on applying your fix to what is
  in production. All you have to do is switch back to your master branch.
</p>

<p>
  However, before you do that, note that if your working directory or staging
  area has uncommitted changes that conflict with the branch you’re checking
  out, Git won’t let you switch branches. It’s best to have a clean working
  state when you switch branches. There are ways to get around this (namely,
  stashing and commit amending) that we’ll cover later. For now, you’ve
  committed all your changes, so you can switch back to your master branch:
</p>

<pre>
$ git checkout master
Switched to branch 'master'
</pre>

<p>
  At this point, your project working directory is exactly the way it was
  before you started working on issue #53, and you can concentrate on your
  hotfix. This is an important point to remember: Git resets your working
  directory to look like the snapshot of the commit that the branch you check
  out points to. It adds, removes, and modifies files automatically to make
  sure your working copy is what the branch looked like on your last commit to
  it.
</p>

<p>
  Next, you have a hotfix to make. Let’s create a hotfix branch on which to
  work until it’s completed (see Figure 3-13):
</p>

<pre>
$ git checkout -b hotfix
Switched to a new branch 'hotfix'
$ vim index.html
$ git commit -a -m 'fix the broken email address'
[hotfix 3a0874c] fix the broken email address
 1 files changed, 1 deletion(-)
</pre>

<p>
  Insert 18333fig0313.png Figure 3-13. hotfix branch based back at your master
  branch point.
</p>

<p>
  You can run your tests, make sure the hotfix is what you want, and merge it
  back into your master branch to deploy to production. You do this with the <code>git merge</code> command:
</p>

<pre>
$ git checkout master
$ git merge hotfix
Updating f42c576..3a0874c
Fast-forward
 README | 1 -
 1 file changed, 1 deletion(-)
</pre>

<p>
  You’ll notice the phrase "Fast-forward" in that merge. Because the commit
  pointed to by the branch you merged in was directly upstream of the commit
  you’re on, Git moves the pointer forward. To phrase that another way, when
  you try to merge one commit with a commit that can be reached by following
  the first commit’s history, Git simplifies things by moving the pointer
  forward because there is no divergent work to merge together — this is called
  a "fast forward".
</p>

<p>
  Your change is now in the snapshot of the commit pointed to by the <code>master</code> branch, and you can deploy your change (see Figure 3-14).
</p>

<p>
  Insert 18333fig0314.png Figure 3-14. Your master branch points to the same
  place as your hotfix branch after the merge.
</p>

<p>
  After your super-important fix is deployed, you’re ready to switch back to
  the work you were doing before you were interrupted. However, first you’ll
  delete the <code>hotfix</code> branch, because you no longer need it — the <code>master</code> branch points at the same place. You can delete it with the <code>-d</code> option to <code>git branch</code>:
</p>

<pre>
$ git branch -d hotfix
Deleted branch hotfix (was 3a0874c).
</pre>

<p>
  Now you can switch back to your work-in-progress branch on issue #53 and
  continue working on it (see Figure 3-15):
</p>

<pre>
$ git checkout iss53
Switched to branch 'iss53'
$ vim index.html
$ git commit -a -m 'finish the new footer [issue 53]'
[iss53 ad82d7a] finish the new footer [issue 53]
 1 file changed, 1 insertion(+)
</pre>

<p>
  Insert 18333fig0315.png Figure 3-15. Your iss53 branch can move forward
  independently.
</p>

<p>
  It’s worth noting here that the work you did in your <code>hotfix</code> branch is not contained in the files in your <code>iss53</code> branch. If you need to pull it in, you can merge your <code>master</code> branch into your <code>iss53</code> branch by running <code>git merge master</code>, or you can wait to integrate those changes until you decide to pull the <code>iss53</code> branch back into <code>master</code> later.
</p>

<h3>Basic Merging</h3>

<p>
  Suppose you’ve decided that your issue #53 work is complete and ready to be
  merged into your <code>master</code> branch. In order to do that, you’ll merge in your <code>iss53</code> branch, much like you merged in your <code>hotfix</code> branch earlier. All you have to do is check out the branch you wish to merge
  into and then run the <code>git merge</code> command:
</p>

<pre>
$ git checkout master
$ git merge iss53
Auto-merging README
Merge made by the 'recursive' strategy.
 README | 1 +
 1 file changed, 1 insertion(+)
</pre>

<p>
  This looks a bit different than the <code>hotfix</code> merge you did earlier. In this case, your development history has diverged
  from some older point. Because the commit on the branch you’re on isn’t a
  direct ancestor of the branch you’re merging in, Git has to do some work. In
  this case, Git does a simple three-way merge, using the two snapshots pointed
  to by the branch tips and the common ancestor of the two. Figure 3-16
  highlights the three snapshots that Git uses to do its merge in this case.
</p>

<p>
  Insert 18333fig0316.png Figure 3-16. Git automatically identifies the best
  common-ancestor merge base for branch merging.
</p>

<p>
  Instead of just moving the branch pointer forward, Git creates a new snapshot
  that results from this three-way merge and automatically creates a new commit
  that points to it (see Figure 3-17). This is referred to as a merge commit
  and is special in that it has more than one parent.
</p>

<p>
  It’s worth pointing out that Git determines the best common ancestor to use
  for its merge base; this is different than CVS or Subversion (before version
  1.5), where the developer doing the merge has to figure out the best merge
  base for themselves. This makes merging a heck of a lot easier in Git than in
  these other systems.
</p>

<p>
  Insert 18333fig0317.png Figure 3-17. Git automatically creates a new commit
  object that contains the merged work.
</p>

<p>
  Now that your work is merged in, you have no further need for the <code>iss53</code> branch. You can delete it and then manually close the ticket in your
  ticket-tracking system:
</p>

<pre>
$ git branch -d iss53
</pre>

<h3>Basic Merge Conflicts</h3>

<p>
  Occasionally, this process doesn’t go smoothly. If you changed the same part
  of the same file differently in the two branches you’re merging together, Git
  won’t be able to merge them cleanly. If your fix for issue #53 modified the
  same part of a file as the <code>hotfix</code>, you’ll get a merge conflict that looks something like this:
</p>

<pre>
$ git merge iss53
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.
</pre>

<p>
  Git hasn’t automatically created a new merge commit. It has paused the
  process while you resolve the conflict. If you want to see which files are
  unmerged at any point after a merge conflict, you can run <code>git status</code>:
</p>

<pre>
$ git status
On branch master
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  (use "git add &lt;file&gt;..." to mark resolution)

        both modified:      index.html

no changes added to commit (use "git add" and/or "git commit -a")
</pre>

<p>
  Anything that has merge conflicts and hasn’t been resolved is listed as
  unmerged. Git adds standard conflict-resolution markers to the files that
  have conflicts, so you can open them manually and resolve those conflicts.
  Your file contains a section that looks something like this:
</p>

<pre>
&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD
&lt;div id="footer"&gt;contact : email.support@github.com&lt;/div&gt;
=======
&lt;div id="footer"&gt;
  please contact us at support@github.com
&lt;/div&gt;
&gt;&gt;&gt;&gt;&gt;&gt;&gt; iss53
</pre>

<p>
  This means the version in HEAD (your master branch, because that was what you
  had checked out when you ran your merge command) is the top part of that
  block (everything above the <code>=======</code>), while the version in your <code>iss53</code> branch looks like everything in the bottom part. In order to resolve the
  conflict, you have to either choose one side or the other or merge the
  contents yourself. For instance, you might resolve this conflict by replacing
  the entire block with this:
</p>

<pre>
&lt;div id="footer"&gt;
please contact us at email.support@github.com
&lt;/div&gt;
</pre>

<p>
  This resolution has a little of each section, and I’ve fully removed the <code>&lt;&lt;&lt;&lt;&lt;&lt;&lt;</code>, <code>=======</code>, and <code>>>>>>>></code> lines. After you’ve resolved each of these sections in each conflicted file,
  run <code>git add</code> on each file to mark it as resolved. Staging the file marks it as resolved
  in Git. If you want to use a graphical tool to resolve these issues, you can
  run <code>git mergetool</code>, which fires up an appropriate visual merge tool and walks you through the
  conflicts:
</p>

<pre>
$ git mergetool

This message is displayed because 'merge.tool' is not configured.
See 'git mergetool --tool-help' or 'git help config' for more details.
'git mergetool' will now attempt to use one of the following tools:
opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse diffmerge ecmerge p4merge araxis bc3 codecompare vimdiff emerge
Merging:
index.html

Normal merge conflict for 'index.html':
  {local}: modified file
  {remote}: modified file
Hit return to start merge resolution tool (opendiff):
</pre>

<p>
  If you want to use a merge tool other than the default (Git chose <code>opendiff</code> for me in this case because I ran the command on a Mac), you can see all the
  supported tools listed at the top after “... one of the following tools:”.
  Type the name of the tool you’d rather use. In Chapter 7, we’ll discuss how
  you can change this default value for your environment.
</p>

<p>
  After you exit the merge tool, Git asks you if the merge was successful. If
  you tell the script that it was, it stages the file to mark it as resolved
  for you.
</p>

<p>
  You can run <code>git status</code> again to verify that all conflicts have been resolved:
</p>

<pre>
$ git status
On branch master
Changes to be committed:
  (use "git reset HEAD &lt;file&gt;..." to unstage)

        modified:   index.html
</pre>

<p>
  If you’re happy with that, and you verify that everything that had conflicts
  has been staged, you can type <code>git commit</code> to finalize the merge commit. The commit message by default looks something
  like this:
</p>

<pre>
Merge branch 'iss53'

Conflicts:
  index.html
#
# It looks like you may be committing a merge.
# If this is not correct, please remove the file
#       .git/MERGE_HEAD
# and try again.
#
</pre>

<p>
  You can modify that message with details about how you resolved the merge if
  you think it would be helpful to others looking at this merge in the future —
  why you did what you did, if it’s not obvious.
</p>

<h2>Branch Management</h2>

<p>
  Now that you’ve created, merged, and deleted some branches, let’s look at
  some branch-management tools that will come in handy when you begin using
  branches all the time.
</p>

<p>
  The <code>git branch</code> command does more than just create and delete branches. If you run it with
  no arguments, you get a simple listing of your current branches:
</p>

<pre>
$ git branch
  iss53
* master
  testing
</pre>

<p>
  Notice the <code>*</code> character that prefixes the <code>master</code> branch: it indicates the branch that you currently have checked out. This
  means that if you commit at this point, the <code>master</code> branch will be moved forward with your new work. To see the last commit on
  each branch, you can run <code>git branch -v</code>:
</p>

<pre>
$ git branch -v
  iss53   93b412c fix javascript issue
* master  7a98805 Merge branch 'iss53'
  testing 782fd34 add scott to the author list in the readmes
</pre>

<p>
  Another useful option to figure out what state your branches are in is to
  filter this list to branches that you have or have not yet merged into the
  branch you’re currently on. There are useful <code>--merged</code> and <code>--no-merged</code> options available in Git for this purpose. To see which branches are already
  merged into the branch you’re on, you can run <code>git branch --merged</code>:
</p>

<pre>
$ git branch --merged
  iss53
* master
</pre>

<p>
  Because you already merged in <code>iss53</code> earlier, you see it in your list. Branches on this list without the <code>*</code> in front of them are generally fine to delete with <code>git branch -d</code>; you’ve already incorporated their work into another branch, so you’re not
  going to lose anything.
</p>

<p>
  To see all the branches that contain work you haven’t yet merged in, you can
  run <code>git branch --no-merged</code>:
</p>

<pre>
$ git branch --no-merged
  testing
</pre>

<p>
  This shows your other branch. Because it contains work that isn’t merged in
  yet, trying to delete it with <code>git branch -d</code> will fail:
</p>

<pre>
$ git branch -d testing
error: The branch 'testing' is not fully merged.
If you are sure you want to delete it, run 'git branch -D testing'.
</pre>

<p>
  If you really do want to delete the branch and lose that work, you can force
  it with <code>-D</code>, as the helpful message points out.
</p>

<h2>Branching Workflows</h2>

<p>
  Now that you have the basics of branching and merging down, what can or
  should you do with them? In this section, we’ll cover some common workflows
  that this lightweight branching makes possible, so you can decide if you
  would like to incorporate it into your own development cycle.
</p>

<h3>Long-Running Branches</h3>

<p>
  Because Git uses a simple three-way merge, merging from one branch into
  another multiple times over a long period is generally easy to do. This means
  you can have several branches that are always open and that you use for
  different stages of your development cycle; you can merge regularly from some
  of them into others.
</p>

<p>
  Many Git developers have a workflow that embraces this approach, such as
  having only code that is entirely stable in their <code>master</code> branch — possibly only code that has been or will be released. They have
  another parallel branch named develop or next that they work from or use to
  test stability — it isn’t necessarily always stable, but whenever it gets to
  a stable state, it can be merged into <code>master</code>. It’s used to pull in topic branches (short-lived branches, like your
  earlier <code>iss53</code> branch) when they’re ready, to make sure they pass all the tests and don’t
  introduce bugs.
</p>

<p>
  In reality, we’re talking about pointers moving up the line of commits you’re
  making. The stable branches are farther down the line in your commit history,
  and the bleeding-edge branches are farther up the history (see Figure 3-18).
</p>

<p>
  Insert 18333fig0318.png Figure 3-18. More stable branches are generally
  farther down the commit history.
</p>

<p>
  It’s generally easier to think about them as work silos, where sets of
  commits graduate to a more stable silo when they’re fully tested (see Figure
  3-19).
</p>

<p>
  Insert 18333fig0319.png Figure 3-19. It may be helpful to think of your
  branches as silos.
</p>

<p>
  You can keep doing this for several levels of stability. Some larger projects
  also have a <code>proposed</code> or <code>pu</code> (proposed updates) branch that has integrated branches that may not be ready
  to go into the <code>next</code> or <code>master</code> branch. The idea is that your branches are at various levels of stability;
  when they reach a more stable level, they’re merged into the branch above
  them. Again, having multiple long-running branches isn’t necessary, but it’s
  often helpful, especially when you’re dealing with very large or complex
  projects.
</p>

<h3>Topic Branches</h3>

<p>
  Topic branches, however, are useful in projects of any size. A topic branch
  is a short-lived branch that you create and use for a single particular
  feature or related work. This is something you’ve likely never done with a
  VCS before because it’s generally too expensive to create and merge branches.
  But in Git it’s common to create, work on, merge, and delete branches several
  times a day.
</p>

<p>
  You saw this in the last section with the <code>iss53</code> and <code>hotfix</code> branches you created. You did a few commits on them and deleted them
  directly after merging them into your main branch. This technique allows you
  to context-switch quickly and completely — because your work is separated
  into silos where all the changes in that branch have to do with that topic,
  it’s easier to see what has happened during code review and such. You can
  keep the changes there for minutes, days, or months, and merge them in when
  they’re ready, regardless of the order in which they were created or worked
  on.
</p>

<p>
  Consider an example of doing some work (on <code>master</code>), branching off for an issue (<code>iss91</code>), working on it for a bit, branching off the second branch to try another
  way of handling the same thing (<code>iss91v2</code>), going back to your master branch and working there for a while, and then
  branching off there to do some work that you’re not sure is a good idea (<code>dumbidea</code> branch). Your commit history will look something like Figure 3-20.
</p>

<p>
  Insert 18333fig0320.png Figure 3-20. Your commit history with multiple topic
  branches.
</p>

<p>
  Now, let’s say you decide you like the second solution to your issue best (<code>iss91v2</code>); and you showed the <code>dumbidea</code> branch to your coworkers, and it turns out to be genius. You can throw away
  the original <code>iss91</code> branch (losing commits C5 and C6) and merge in the other two. Your history
  then looks like Figure 3-21.
</p>

<p>
  Insert 18333fig0321.png Figure 3-21. Your history after merging in dumbidea
  and iss91v2.
</p>

<p>
  It’s important to remember when you’re doing all this that these branches are
  completely local. When you’re branching and merging, everything is being done
  only in your Git repository — no server communication is happening.
</p>

<h2>Remote Branches</h2>

<p>
  Remote branches are references to the state of branches on your remote
  repositories. They’re local branches that you can’t move; they’re moved
  automatically whenever you do any network communication. Remote branches act
  as bookmarks to remind you where the branches on your remote repositories
  were the last time you connected to them.
</p>

<p>
  They take the form <code>(remote)/(branch)</code>. For instance, if you wanted to see what the <code>master</code> branch on your <code>origin</code> remote looked like as of the last time you communicated with it, you would
  check the <code>origin/master</code> branch. If you were working on an issue with a partner and they pushed up an
  <code>iss53</code> branch, you might have your own local <code>iss53</code> branch; but the branch on the server would point to the commit at <code>origin/iss53</code>.
</p>

<p>
  This may be a bit confusing, so let’s look at an example. Let’s say you have
  a Git server on your network at <code>git.ourcompany.com</code>. If you clone from this, Git automatically names it <code>origin</code> for you, pulls down all its data, creates a pointer to where its <code>master</code> branch is, and names it <code>origin/master</code> locally; and you can’t move it. Git also gives you your own <code>master</code> branch starting at the same place as origin’s <code>master</code> branch, so you have something to work from (see Figure 3-22).
</p>

<p>
  Insert 18333fig0322.png Figure 3-22. A Git clone gives you your own master
  branch and origin/master pointing to origin’s master branch.
</p>

<p>
  If you do some work on your local master branch, and, in the meantime,
  someone else pushes to <code>git.ourcompany.com</code> and updates its master branch, then your histories move forward differently.
  Also, as long as you stay out of contact with your origin server, your <code>origin/master</code> pointer doesn’t move (see Figure 3-23).
</p>

<p>
  Insert 18333fig0323.png Figure 3-23. Working locally and having someone push
  to your remote server makes each history move forward differently.
</p>

<p>
  To synchronize your work, you run a <code>git fetch origin</code> command. This command looks up which server origin is (in this case, it’s <code>git.ourcompany.com</code>), fetches any data from it that you don’t yet have, and updates your local
  database, moving your <code>origin/master</code> pointer to its new, more up-to-date position (see Figure 3-24).
</p>

<p>
  Insert 18333fig0324.png Figure 3-24. The <code>git fetch</code> command updates your remote references.
</p>

<p>
  To demonstrate having multiple remote servers and what remote branches for
  those remote projects look like, let’s assume you have another internal Git
  server that is used only for development by one of your sprint teams. This
  server is at <code>git.team1.ourcompany.com</code>. You can add it as a new remote reference to the project you’re currently
  working on by running the <code>git remote add</code> command as we covered in Chapter 2. Name this remote <code>teamone</code>, which will be your shortname for that whole URL (see Figure 3-25).
</p>

<p>Insert 18333fig0325.png Figure 3-25. Adding another server as a remote.</p>

<p>
  Now, you can run <code>git fetch teamone</code> to fetch everything the remote <code>teamone</code> server has that you don’t have yet. Because that server has a subset of the
  data your <code>origin</code> server has right now, Git fetches no data but sets a remote branch called <code>teamone/master</code> to point to the commit that <code>teamone</code> has as its <code>master</code> branch (see Figure 3-26).
</p>

<p>
  Insert 18333fig0326.png Figure 3-26. You get a reference to teamone’s master
  branch position locally.
</p>

<h3>Pushing</h3>

<p>
  When you want to share a branch with the world, you need to push it up to a
  remote that you have write access to. Your local branches aren’t
  automatically synchronized to the remotes you write to — you have to
  explicitly push the branches you want to share. That way, you can use private
  branches for work you don’t want to share, and push up only the topic
  branches you want to collaborate on.
</p>

<p>
  If you have a branch named <code>serverfix</code> that you want to work on with others, you can push it up the same way you
  pushed your first branch. Run <code>git push (remote) (branch)</code>:
</p>

<pre>
$ git push origin serverfix
Counting objects: 20, done.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (15/15), 1.74 KiB, done.
Total 15 (delta 5), reused 0 (delta 0)
To git@github.com:schacon/simplegit.git
 * [new branch]      serverfix -&gt; serverfix
</pre>

<p>
  This is a bit of a shortcut. Git automatically expands the <code>serverfix</code> branchname out to <code>refs/heads/serverfix:refs/heads/serverfix</code>, which means, “Take my serverfix local branch and push it to update the
  remote’s serverfix branch.” We’ll go over the <code>refs/heads/</code> part in detail in Chapter 9, but you can generally leave it off. You can
  also do <code>git push origin serverfix:serverfix</code>, which does the same thing — it says, “Take my serverfix and make it the
  remote’s serverfix.” You can use this format to push a local branch into a
  remote branch that is named differently. If you didn’t want it to be called <code>serverfix</code> on the remote, you could instead run <code>git push origin serverfix:awesomebranch</code> to push your local <code>serverfix</code> branch to the <code>awesomebranch</code> branch on the remote project.
</p>

<p>
  The next time one of your collaborators fetches from the server, they will
  get a reference to where the server’s version of <code>serverfix</code> is under the remote branch <code>origin/serverfix</code>:
</p>

<pre>
$ git fetch origin
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (14/14), done.
remote: Total 15 (delta 5), reused 0 (delta 0)
Unpacking objects: 100% (15/15), done.
From git@github.com:schacon/simplegit
 * [new branch]      serverfix    -&gt; origin/serverfix
</pre>

<p>
  It’s important to note that when you do a fetch that brings down new remote
  branches, you don’t automatically have local, editable copies of them. In
  other words, in this case, you don’t have a new <code>serverfix</code> branch — you only have an <code>origin/serverfix</code> pointer that you can’t modify.
</p>

<p>
  To merge this work into your current working branch, you can run <code>git merge origin/serverfix</code>. If you want your own <code>serverfix</code> branch that you can work on, you can base it off your remote branch:
</p>

<pre>
$ git checkout -b serverfix origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'
</pre>

<p>
  This gives you a local branch that you can work on that starts where <code>origin/serverfix</code> is.
</p>

<h3>Tracking Branches</h3>

<p>
  Checking out a local branch from a remote branch automatically creates what
  is called a <i>tracking branch</i>. Tracking branches are local branches that have a direct relationship to a
  remote branch. If you’re on a tracking branch and type <code>git push</code>, Git automatically knows which server and branch to push to. Also, running <code>git pull</code> while on one of these branches fetches all the remote references and then
  automatically merges in the corresponding remote branch.
</p>

<p>
  When you clone a repository, it generally automatically creates a <code>master</code> branch that tracks <code>origin/master</code>. That’s why <code>git push</code> and <code>git pull</code> work out of the box with no other arguments. However, you can set up other
  tracking branches if you wish — ones that don’t track branches on <code>origin</code> and don’t track the <code>master</code> branch. The simple case is the example you just saw, running <code>git checkout -b [branch] [remotename]/[branch]</code>. If you have Git version 1.6.2 or later, you can also use the <code>--track</code> shorthand:
</p>

<pre>
$ git checkout --track origin/serverfix
Branch serverfix set up to track remote branch serverfix from origin.
Switched to a new branch 'serverfix'
</pre>

<p>
  To set up a local branch with a different name than the remote branch, you
  can easily use the first version with a different local branch name:
</p>

<pre>
$ git checkout -b sf origin/serverfix
Branch sf set up to track remote branch serverfix from origin.
Switched to a new branch 'sf'
</pre>

<p>
  Now, your local branch <code>sf</code> will automatically push to and pull from <code>origin/serverfix</code>.
</p>

<h3>Deleting Remote Branches</h3>

<p>
  Suppose you’re done with a remote branch — say, you and your collaborators
  are finished with a feature and have merged it into your remote’s <code>master</code> branch (or whatever branch your stable codeline is in). You can delete a
  remote branch using the rather obtuse syntax <code>git push [remotename] :[branch]</code>. If you want to delete your <code>serverfix</code> branch from the server, you run the following:
</p>

<pre>
$ git push origin :serverfix
To git@github.com:schacon/simplegit.git
 - [deleted]         serverfix
</pre>

<p>
  Boom. No more branch on your server. You may want to dog-ear this page,
  because you’ll need that command, and you’ll likely forget the syntax. A way
  to remember this command is by recalling the <code>git push [remotename] [localbranch]:[remotebranch]</code> syntax that we went over a bit earlier. If you leave off the <code>[localbranch]</code> portion, then you’re basically saying, “Take nothing on my side and make it
  be <code>[remotebranch]</code>.”
</p>

<h2>Rebasing</h2>

<p>
  In Git, there are two main ways to integrate changes from one branch into
  another: the <code>merge</code> and the <code>rebase</code>. In this section you’ll learn what rebasing is, how to do it, why it’s a
  pretty amazing tool, and in what cases you won’t want to use it.
</p>

<h3>The Basic Rebase</h3>

<p>
  If you go back to an earlier example from the Merge section (see Figure
  3-27), you can see that you diverged your work and made commits on two
  different branches.
</p>

<p>
  Insert 18333fig0327.png Figure 3-27. Your initial diverged commit history.
</p>

<p>
  The easiest way to integrate the branches, as we’ve already covered, is the <code>merge</code> command. It performs a three-way merge between the two latest branch
  snapshots (C3 and C4) and the most recent common ancestor of the two (C2),
  creating a new snapshot (and commit), as shown in Figure 3-28.
</p>

<p>
  Insert 18333fig0328.png Figure 3-28. Merging a branch to integrate the
  diverged work history.
</p>

<p>
  However, there is another way: you can take the patch of the change that was
  introduced in C3 and reapply it on top of C4. In Git, this is called <i>rebasing</i>. With the <code>rebase</code> command, you can take all the changes that were committed on one branch and
  replay them on another one.
</p>

<p>In this example, you’d run the following:</p>

<pre>
$ git checkout experiment
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: added staged command
</pre>

<p>
  It works by going to the common ancestor of the two branches (the one you’re
  on and the one you’re rebasing onto), getting the diff introduced by each
  commit of the branch you’re on, saving those diffs to temporary files,
  resetting the current branch to the same commit as the branch you are
  rebasing onto, and finally applying each change in turn. Figure 3-29
  illustrates this process.
</p>

<p>
  Insert 18333fig0329.png Figure 3-29. Rebasing the change introduced in C3
  onto C4.
</p>

<p>
  At this point, you can go back to the master branch and do a fast-forward
  merge (see Figure 3-30).
</p>

<p>Insert 18333fig0330.png Figure 3-30. Fast-forwarding the master branch.</p>

<p>
  Now, the snapshot pointed to by C3' is exactly the same as the one that was
  pointed to by C5 in the merge example. There is no difference in the end
  product of the integration, but rebasing makes for a cleaner history. If you
  examine the log of a rebased branch, it looks like a linear history: it
  appears that all the work happened in series, even when it originally
  happened in parallel.
</p>

<p>
  Often, you’ll do this to make sure your commits apply cleanly on a remote
  branch — perhaps in a project to which you’re trying to contribute but that
  you don’t maintain. In this case, you’d do your work in a branch and then
  rebase your work onto <code>origin/master</code> when you were ready to submit your patches to the main project. That way,
  the maintainer doesn’t have to do any integration work — just a fast-forward
  or a clean apply.
</p>

<p>
  Note that the snapshot pointed to by the final commit you end up with,
  whether it’s the last of the rebased commits for a rebase or the final merge
  commit after a merge, is the same snapshot — it’s only the history that is
  different. Rebasing replays changes from one line of work onto another in the
  order they were introduced, whereas merging takes the endpoints and merges
  them together.
</p>

<h3>More Interesting Rebases</h3>

<p>
  You can also have your rebase replay on something other than the rebase
  branch. Take a history like Figure 3-31, for example. You branched a topic
  branch (<code>server</code>) to add some server-side functionality to your project, and made a commit.
  Then, you branched off that to make the client-side changes (<code>client</code>) and committed a few times. Finally, you went back to your server branch and
  did a few more commits.
</p>

<p>
  Insert 18333fig0331.png Figure 3-31. A history with a topic branch off
  another topic branch.
</p>

<p>
  Suppose you decide that you want to merge your client-side changes into your
  mainline for a release, but you want to hold off on the server-side changes
  until it’s tested further. You can take the changes on client that aren’t on
  server (C8 and C9) and replay them on your master branch by using the <code>--onto</code> option of <code>git rebase</code>:
</p>

<pre>
$ git rebase --onto master server client
</pre>

<p>
  This basically says, “Check out the client branch, figure out the patches
  from the common ancestor of the <code>client</code> and <code>server</code> branches, and then replay them onto <code>master</code>.” It’s a bit complex; but the result, shown in Figure 3-32, is pretty cool.
</p>

<p>
  Insert 18333fig0332.png Figure 3-32. Rebasing a topic branch off another
  topic branch.
</p>

<p>Now you can fast-forward your master branch (see Figure 3-33):</p>

<pre>
$ git checkout master
$ git merge client
</pre>

<p>
  Insert 18333fig0333.png Figure 3-33. Fast-forwarding your master branch to
  include the client branch changes.
</p>

<p>
  Let’s say you decide to pull in your server branch as well. You can rebase
  the server branch onto the master branch without having to check it out first
  by running <code>git rebase [basebranch] [topicbranch]</code> — which checks out the topic branch (in this case, <code>server</code>) for you and replays it onto the base branch (<code>master</code>):
</p>

<pre>
$ git rebase master server
</pre>

<p>
  This replays your <code>server</code> work on top of your <code>master</code> work, as shown in Figure 3-34.
</p>

<p>
  Insert 18333fig0334.png Figure 3-34. Rebasing your server branch on top of
  your master branch.
</p>

<p>Then, you can fast-forward the base branch (<code>master</code>):</p>

<pre>
$ git checkout master
$ git merge server
</pre>

<p>
  You can remove the <code>client</code> and <code>server</code> branches because all the work is integrated and you don’t need them anymore,
  leaving your history for this entire process looking like Figure 3-35:
</p>

<pre>
$ git branch -d client
$ git branch -d server
</pre>

<p>Insert 18333fig0335.png Figure 3-35. Final commit history.</p>

<h3>The Perils of Rebasing</h3>

<p>
  Ahh, but the bliss of rebasing isn’t without its drawbacks, which can be
  summed up in a single line:
</p>

<p>
  <b>Do not rebase commits that you have pushed to a public repository.</b>
</p>

<p>
  If you follow that guideline, you’ll be fine. If you don’t, people will hate
  you, and you’ll be scorned by friends and family.
</p>

<p>
  When you rebase stuff, you’re abandoning existing commits and creating new
  ones that are similar but different. If you push commits somewhere and others
  pull them down and base work on them, and then you rewrite those commits with
  <code>git rebase</code> and push them up again, your collaborators will have to re-merge their work
  and things will get messy when you try to pull their work back into yours.
</p>

<p>
  Let’s look at an example of how rebasing work that you’ve made public can
  cause problems. Suppose you clone from a central server and then do some work
  off that. Your commit history looks like Figure 3-36.
</p>

<p>
  Insert 18333fig0336.png Figure 3-36. Clone a repository, and base some work
  on it.
</p>

<p>
  Now, someone else does more work that includes a merge, and pushes that work
  to the central server. You fetch them and merge the new remote branch into
  your work, making your history look something like Figure 3-37.
</p>

<p>
  Insert 18333fig0337.png Figure 3-37. Fetch more commits, and merge them into
  your work.
</p>

<p>
  Next, the person who pushed the merged work decides to go back and rebase
  their work instead; they do a <code>git push --force</code> to overwrite the history on the server. You then fetch from that server,
  bringing down the new commits.
</p>

<p>
  Insert 18333fig0338.png Figure 3-38. Someone pushes rebased commits,
  abandoning commits you’ve based your work on.
</p>

<p>
  At this point, you have to merge this work in again, even though you’ve
  already done so. Rebasing changes the SHA-1 hashes of these commits so to Git
  they look like new commits, when in fact you already have the C4 work in your
  history (see Figure 3-39).
</p>

<p>
  Insert 18333fig0339.png Figure 3-39. You merge in the same work again into a
  new merge commit.
</p>

<p>
  You have to merge that work in at some point so you can keep up with the
  other developer in the future. After you do that, your commit history will
  contain both the C4 and C4' commits, which have different SHA-1 hashes but
  introduce the same work and have the same commit message. If you run a <code>git log</code> when your history looks like this, you’ll see two commits that have the same
  author date and message, which will be confusing. Furthermore, if you push
  this history back up to the server, you’ll reintroduce all those rebased
  commits to the central server, which can further confuse people.
</p>

<p>
  If you treat rebasing as a way to clean up and work with commits before you
  push them, and if you only rebase commits that have never been available
  publicly, then you’ll be fine. If you rebase commits that have already been
  pushed publicly, and people may have based work on those commits, then you
  may be in for some frustrating trouble.
</p>

<h2>Summary</h2>

<p>
  We’ve covered basic branching and merging in Git. You should feel comfortable
  creating and switching to new branches, switching between branches and
  merging local branches together.&emsp14;&emsp14;You should also be able to
  share your branches by pushing them to a shared server, working with others
  on shared branches and rebasing your branches before they are shared.
</p>

<h1>Git on the Server</h1>

<p>
  At this point, you should be able to do most of the day-to-day tasks for
  which you’ll be using Git. However, in order to do any collaboration in Git,
  you’ll need to have a remote Git repository. Although you can technically
  push changes to and pull changes from individuals’ repositories, doing so is
  discouraged because you can fairly easily confuse what they’re working on if
  you’re not careful. Furthermore, you want your collaborators to be able to
  access the repository even if your computer is offline — having a more
  reliable common repository is often useful. Therefore, the preferred method
  for collaborating with someone is to set up an intermediate repository that
  you both have access to, and push to and pull from that. We’ll refer to this
  repository as a "Git server"; but you’ll notice that it generally takes a
  tiny amount of resources to host a Git repository, so you’ll rarely need to
  use an entire server for it.
</p>

<p>
  Running a Git server is simple. First, you choose which protocols you want
  your server to communicate with. The first section of this chapter will cover
  the available protocols and the pros and cons of each. The next sections will
  explain some typical setups using those protocols and how to get your server
  running with them. Last, we’ll go over a few hosted options, if you don’t
  mind hosting your code on someone else’s server and don’t want to go through
  the hassle of setting up and maintaining your own server.
</p>

<p>
  If you have no interest in running your own server, you can skip to the last
  section of the chapter to see some options for setting up a hosted account
  and then move on to the next chapter, where we discuss the various ins and
  outs of working in a distributed source control environment.
</p>

<p>
  A remote repository is generally a <i>bare repository</i> — a Git repository that has no working directory. Because the repository is
  only used as a collaboration point, there is no reason to have a snapshot
  checked out on disk; it’s just the Git data. In the simplest terms, a bare
  repository is the contents of your project’s <code>.git</code> directory and nothing else.
</p>

<h2>The Protocols</h2>

<p>
  Git can use four major network protocols to transfer data: Local, Secure
  Shell (SSH), Git, and HTTP. Here we’ll discuss what they are and in what
  basic circumstances you would want (or not want) to use them.
</p>

<p>
  It’s important to note that with the exception of the HTTP protocols, all of
  these require Git to be installed and working on the server.
</p>

<h3>Local Protocol</h3>

<p>
  The most basic is the <i>Local protocol</i>, in which the remote repository is in another directory on disk. This is
  often used if everyone on your team has access to a shared filesystem such as
  an NFS mount, or in the less likely case that everyone logs in to the same
  computer. The latter wouldn’t be ideal, because all your code repository
  instances would reside on the same computer, making a catastrophic loss much
  more likely.
</p>

<p>
  If you have a shared mounted filesystem, then you can clone, push to, and
  pull from a local file-based repository. To clone a repository like this or
  to add one as a remote to an existing project, use the path to the repository
  as the URL. For example, to clone a local repository, you can run something
  like this:
</p>

<pre>
$ git clone /opt/git/project.git
</pre>

<p>Or you can do this:</p>

<pre>
$ git clone file:///opt/git/project.git
</pre>

<p>
  Git operates slightly differently if you explicitly specify <code>file://</code> at the beginning of the URL. If you just specify the path, and the source
  and the destination are on the same filesystem, Git tries to hardlink the
  objects it needs. If they are not on the same filesystem, it will copy the
  objects it needs using the system's standard copying functionality. If you
  specify <code>file://</code>, Git fires up the processes that it normally uses to transfer data over a
  network which is generally a lot less efficient method of transferring the
  data. The main reason to specify the <code>file://</code> prefix is if you want a clean copy of the repository with extraneous
  references or objects left out — generally after an import from another
  version-control system or something similar (see Chapter 9 for maintenance
  tasks). We’ll use the normal path here because doing so is almost always
  faster.
</p>

<p>
  To add a local repository to an existing Git project, you can run something
  like this:
</p>

<pre>
$ git remote add local_proj /opt/git/project.git
</pre>

<p>
  Then, you can push to and pull from that remote as though you were doing so
  over a network.
</p>

<p>The Pros</p>

<p>
  The pros of file-based repositories are that they’re simple and they use
  existing file permissions and network access. If you already have a shared
  filesystem to which your whole team has access, setting up a repository is
  very easy. You stick the bare repository copy somewhere everyone has shared
  access to and set the read/write permissions as you would for any other
  shared directory. We’ll discuss how to export a bare repository copy for this
  purpose in the next section, “Getting Git on a Server.”
</p>

<p>
  This is also a nice option for quickly grabbing work from someone else’s
  working repository. If you and a co-worker are working on the same project
  and they want you to check something out, running a command like <code>git pull /home/john/project</code> is often easier than them pushing to a remote server and you pulling down.
</p>

<p>The Cons</p>

<p>
  The cons of this method are that shared access is generally more difficult to
  set up and reach from multiple locations than basic network access. If you
  want to push from your laptop when you’re at home, you have to mount the
  remote disk, which can be difficult and slow compared to network-based
  access.
</p>

<p>
  It’s also important to mention that this isn’t necessarily the fastest option
  if you’re using a shared mount of some kind. A local repository is fast only
  if you have fast access to the data. A repository on NFS is often slower than
  the repository over SSH on the same server, allowing Git to run off local
  disks on each system.
</p>

<h3>The SSH Protocol</h3>

<p>
  Probably the most common transport protocol for Git is SSH. This is because
  SSH access to servers is already set up in most places — and if it isn’t,
  it’s easy to do. SSH is also the only network-based protocol that you can
  easily read from and write to. The other two network protocols (HTTP and Git)
  are generally read-only, so even if you have them available for the unwashed
  masses, you still need SSH for your own write commands. SSH is also an
  authenticated network protocol; and because it’s ubiquitous, it’s generally
  easy to set up and use.
</p>

<p>
  To clone a Git repository over SSH, you can specify ssh:// URL like this:
</p>

<pre>
$ git clone ssh://user@server/project.git
</pre>

<p>Or you can use the shorter scp-like syntax for SSH protocol:</p>

<pre>
$ git clone user@server:project.git
</pre>

<p>
  You can also not specify a user, and Git assumes the user you’re currently
  logged in as.
</p>

<p>The Pros</p>

<p>
  The pros of using SSH are many. First, you basically have to use it if you
  want authenticated write access to your repository over a network. Second,
  SSH is relatively easy to set up — SSH daemons are commonplace, many network
  admins have experience with them, and many OS distributions are set up with
  them or have tools to manage them. Next, access over SSH is secure — all data
  transfer is encrypted and authenticated. Last, like the Git and Local
  protocols, SSH is efficient, making the data as compact as possible before
  transferring it.
</p>

<p>The Cons</p>

<p>
  The negative aspect of SSH is that you can’t serve anonymous access of your
  repository over it. People must have access to your machine over SSH to
  access it, even in a read-only capacity, which doesn’t make SSH access
  conducive to open source projects. If you’re using it only within your
  corporate network, SSH may be the only protocol you need to deal with. If you
  want to allow anonymous read-only access to your projects, you’ll have to set
  up SSH for you to push over but something else for others to pull over.
</p>

<h3>The Git Protocol</h3>

<p>
  Next is the Git protocol. This is a special daemon that comes packaged with
  Git; it listens on a dedicated port (9418) that provides a service similar to
  the SSH protocol, but with absolutely no authentication. In order for a
  repository to be served over the Git protocol, you must create the <code>git-daemon-export-ok</code> file — the daemon won’t serve a repository without that file in it — but
  other than that there is no security. Either the Git repository is available
  for everyone to clone or it isn’t. This means that there is generally no
  pushing over this protocol. You can enable push access; but given the lack of
  authentication, if you turn on push access, anyone on the internet who finds
  your project’s URL could push to your project. Suffice it to say that this is
  rare.
</p>

<p>The Pros</p>

<p>
  The Git protocol is the fastest transfer protocol available. If you’re
  serving a lot of traffic for a public project or serving a very large project
  that doesn’t require user authentication for read access, it’s likely that
  you’ll want to set up a Git daemon to serve your project. It uses the same
  data-transfer mechanism as the SSH protocol but without the encryption and
  authentication overhead.
</p>

<p>The Cons</p>

<p>
  The downside of the Git protocol is the lack of authentication. It’s
  generally undesirable for the Git protocol to be the only access to your
  project. Generally, you’ll pair it with SSH access for the few developers who
  have push (write) access and have everyone else use <code>git://</code> for read-only access. It’s also probably the most difficult protocol to set
  up. It must run its own daemon, which is custom — we’ll look at setting one
  up in the “Gitosis” section of this chapter — it requires <code>xinetd</code> configuration or the like, which isn’t always a walk in the park. It also
  requires firewall access to port 9418, which isn’t a standard port that
  corporate firewalls always allow. Behind big corporate firewalls, this
  obscure port is commonly blocked.
</p>

<h3>The HTTP/S Protocol</h3>

<p>
  Last we have the HTTP protocol. The beauty of the HTTP or HTTPS protocol is
  the simplicity of setting it up. Basically, all you have to do is put the
  bare Git repository under your HTTP document root and set up a specific <code>post-update</code> hook, and you’re done (See Chapter 7 for details on Git hooks). At that
  point, anyone who can access the web server under which you put the
  repository can also clone your repository. To allow read access to your
  repository over HTTP, do something like this:
</p>

<pre>
$ cd /var/www/htdocs/
$ git clone --bare /path/to/git_project gitproject.git
$ cd gitproject.git
$ mv hooks/post-update.sample hooks/post-update
$ chmod a+x hooks/post-update
</pre>

<p>
  That’s all. The <code>post-update</code> hook that comes with Git by default runs the appropriate command (<code>git update-server-info</code>) to make HTTP fetching and cloning work properly. This command is run when
  you push to this repository over SSH; then, other people can clone via
  something like
</p>

<pre>
$ git clone http://example.com/gitproject.git
</pre>

<p>
  In this particular case, we’re using the <code>/var/www/htdocs</code> path that is common for Apache setups, but you can use any static web server
  — just put the bare repository in its path. The Git data is served as basic
  static files (see Chapter 9 for details about exactly how it’s served).
</p>

<p>
  It’s possible to make Git push over HTTP as well, although that technique
  isn’t as widely used and requires you to set up complex WebDAV requirements.
  Because it’s rarely used, we won’t cover it in this book. If you’re
  interested in using the HTTP-push protocols, you can read about preparing a
  repository for this purpose at <code>http://www.kernel.org/pub/software/scm/git/docs/howto/setup-git-server-over-http.txt</code>. One nice thing about making Git push over HTTP is that you can use any
  WebDAV server, without specific Git features; so, you can use this
  functionality if your web-hosting provider supports WebDAV for writing
  updates to your web site.
</p>

<p>The Pros</p>

<p>
  The upside of using the HTTP protocol is that it’s easy to set up. Running
  the handful of required commands gives you a simple way to give the world
  read access to your Git repository. It takes only a few minutes to do. The
  HTTP protocol also isn’t very resource intensive on your server. Because it
  generally uses a static HTTP server to serve all the data, a normal Apache
  server can serve thousands of files per second on average — it’s difficult to
  overload even a small server.
</p>

<p>
  You can also serve your repositories read-only over HTTPS, which means you
  can encrypt the content transfer; or you can go so far as to make the clients
  use specific signed SSL certificates. Generally, if you’re going to these
  lengths, it’s easier to use SSH public keys; but it may be a better solution
  in your specific case to use signed SSL certificates or other HTTP-based
  authentication methods for read-only access over HTTPS.
</p>

<p>
  Another nice thing is that HTTP is such a commonly used protocol that
  corporate firewalls are often set up to allow traffic through this port.
</p>

<p>The Cons</p>

<p>
  The downside of serving your repository over HTTP is that it’s relatively
  inefficient for the client. It generally takes a lot longer to clone or fetch
  from the repository, and you often have a lot more network overhead and
  transfer volume over HTTP than with any of the other network protocols.
  Because it’s not as intelligent about transferring only the data you need —
  there is no dynamic work on the part of the server in these transactions —
  the HTTP protocol is often referred to as a <i>dumb</i> protocol. For more information about the differences in efficiency between
  the HTTP protocol and the other protocols, see Chapter 9.
</p>

<h2>Getting Git on a Server</h2>

<p>
  In order to initially set up any Git server, you have to export an existing
  repository into a new bare repository — a repository that doesn’t contain a
  working directory. This is generally straightforward to do. In order to clone
  your repository to create a new bare repository, you run the clone command
  with the <code>--bare</code> option. By convention, bare repository directories end in <code>.git</code>, like so:
</p>

<pre>
$ git clone --bare my_project my_project.git
Cloning into bare repository 'my_project.git'...
done.
</pre>

<p>
  You should now have a copy of the Git directory data in your <code>my_project.git</code> directory.
</p>

<p>This is roughly equivalent to something like</p>

<pre>
$ cp -Rf my_project/.git my_project.git
</pre>

<p>
  There are a couple of minor differences in the configuration file; but for
  your purpose, this is close to the same thing. It takes the Git repository by
  itself, without a working directory, and creates a directory specifically for
  it alone.
</p>

<h3>Putting the Bare Repository on a Server</h3>

<p>
  Now that you have a bare copy of your repository, all you need to do is put
  it on a server and set up your protocols. Let’s say you’ve set up a server
  called <code>git.example.com</code> that you have SSH access to, and you want to store all your Git repositories
  under the <code>/opt/git</code> directory. You can set up your new repository by copying your bare
  repository over:
</p>

<pre>
$ scp -r my_project.git user@git.example.com:/opt/git
</pre>

<p>
  At this point, other users who have SSH access to the same server which has
  read-access to the <code>/opt/git</code> directory can clone your repository by running
</p>

<pre>
$ git clone user@git.example.com:/opt/git/my_project.git
</pre>

<p>
  If a user SSHs into a server and has write access to the <code>/opt/git/my_project.git</code> directory, they will also automatically have push access.&emsp14;&emsp14;Git
  will automatically add group write permissions to a repository properly if
  you run the <code>git init</code> command with the <code>--shared</code> option.
</p>

<pre>
$ ssh user@git.example.com
$ cd /opt/git/my_project.git
$ git init --bare --shared
</pre>

<p>
  You see how easy it is to take a Git repository, create a bare version, and
  place it on a server to which you and your collaborators have SSH access. Now
  you’re ready to collaborate on the same project.
</p>

<p>
  It’s important to note that this is literally all you need to do to run a
  useful Git server to which several people have access — just add SSH-able
  accounts on a server, and stick a bare repository somewhere that all those
  users have read and write access to. You’re ready to go — nothing else
  needed.
</p>

<p>
  In the next few sections, you’ll see how to expand to more sophisticated
  setups. This discussion will include not having to create user accounts for
  each user, adding public read access to repositories, setting up web UIs,
  using the Gitosis tool, and more. However, keep in mind that to collaborate
  with a couple of people on a private project, all you <i>need</i> is an SSH server and a bare repository.
</p>

<h3>Small Setups</h3>

<p>
  If you’re a small outfit or are just trying out Git in your organization and
  have only a few developers, things can be simple for you. One of the most
  complicated aspects of setting up a Git server is user management. If you
  want some repositories to be read-only to certain users and read/write to
  others, access and permissions can be a bit difficult to arrange.
</p>

<p>SSH Access</p>

<p>
  If you already have a server to which all your developers have SSH access,
  it’s generally easiest to set up your first repository there, because you
  have to do almost no work (as we covered in the last section). If you want
  more complex access control type permissions on your repositories, you can
  handle them with the normal filesystem permissions of the operating system
  your server runs.
</p>

<p>
  If you want to place your repositories on a server that doesn’t have accounts
  for everyone on your team whom you want to have write access, then you must
  set up SSH access for them. We assume that if you have a server with which to
  do this, you already have an SSH server installed, and that’s how you’re
  accessing the server.
</p>

<p>
  There are a few ways you can give access to everyone on your team. The first
  is to set up accounts for everybody, which is straightforward but can be
  cumbersome. You may not want to run <code>adduser</code> and set temporary passwords for every user.
</p>

<p>
  A second method is to create a single 'git' user on the machine, ask every
  user who is to have write access to send you an SSH public key, and add that
  key to the <code>~/.ssh/authorized_keys</code> file of your new 'git' user. At that point, everyone will be able to access
  that machine via the 'git' user. This doesn’t affect the commit data in any
  way — the SSH user you connect as doesn’t affect the commits you’ve recorded.
</p>

<p>
  Another way to do it is to have your SSH server authenticate from an LDAP
  server or some other centralized authentication source that you may already
  have set up. As long as each user can get shell access on the machine, any
  SSH authentication mechanism you can think of should work.
</p>

<h2>Generating Your SSH Public Key</h2>

<p>
  That being said, many Git servers authenticate using SSH public keys. In
  order to provide a public key, each user in your system must generate one if
  they don’t already have one. This process is similar across all operating
  systems. First, you should check to make sure you don’t already have a key.
  By default, a user’s SSH keys are stored in that user’s <code>~/.ssh</code> directory. You can easily check to see if you have a key already by going to
  that directory and listing the contents:
</p>

<pre>
$ cd ~/.ssh
$ ls
authorized_keys2  id_dsa       known_hosts
config            id_dsa.pub
</pre>

<p>
  You’re looking for a pair of files named something and something.pub, where
  the something is usually <code>id_dsa</code> or <code>id_rsa</code>. The <code>.pub</code> file is your public key, and the other file is your private key. If you
  don’t have these files (or you don’t even have a <code>.ssh</code> directory), you can create them by running a program called <code>ssh-keygen</code>, which is provided with the SSH package on Linux/Mac systems and comes with
  the MSysGit package on Windows:
</p>

<pre>
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/schacon/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/schacon/.ssh/id_rsa.
Your public key has been saved in /Users/schacon/.ssh/id_rsa.pub.
The key fingerprint is:
43:c5:5b:5f:b1:f1:50:43:ad:20:a6:92:6a:1f:9a:3a schacon@agadorlaptop.local
</pre>

<p>
  First it confirms where you want to save the key (<code>.ssh/id_rsa</code>), and then it asks twice for a passphrase, which you can leave empty if you
  don’t want to type a password when you use the key.
</p>

<p>
  Now, each user that does this has to send their public key to you or whoever
  is administrating the Git server (assuming you’re using an SSH server setup
  that requires public keys). All they have to do is copy the contents of the <code>.pub</code> file and e-mail it. The public keys look something like this:
</p>

<pre>
$ cat ~/.ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAklOUpkDHrfHY17SbrmTIpNLTGK9Tjom/BWDSU
GPl+nafzlHDTYW7hdI4yZ5ew18JH4JW9jbhUFrviQzM7xlELEVf4h9lFX5QVkbPppSwg0cda3
Pbv7kOdJ/MTyBlWXFCR+HAo3FXRitBqxiX1nKhXpHAZsMciLq8V6RjsNAQwdsdMFvSlVK/7XA
t3FaoJoAsncM1Q9x5+3V0Ww68/eIFmb1zuUFljQJKprrX88XypNDvjYNby6vw/Pb0rwert/En
mZ+AW4OZPnTPI89ZPmVMLuayrD2cE86Z/il8b+gw3r3+1nKatmIkjn2so1d01QraTlMqVSsbx
NrRFi9wrf+M7Q== schacon@agadorlaptop.local
</pre>

<p>
  For a more in-depth tutorial on creating an SSH key on multiple operating
  systems, see the GitHub guide on SSH keys at <code>http://github.com/guides/providing-your-ssh-key</code>.
</p>

<h2>Setting Up the Server</h2>

<p>
  Let’s walk through setting up SSH access on the server side. In this example,
  you’ll use the <code>authorized_keys</code> method for authenticating your users. We also assume you’re running a
  standard Linux distribution like Ubuntu. First, you create a 'git' user and a
  <code>.ssh</code> directory for that user.
</p>

<pre>
$ sudo adduser git
$ su git
$ cd
$ mkdir .ssh
</pre>

<p>
  Next, you need to add some developer SSH public keys to the <code>authorized_keys</code> file for that user. Let’s assume you’ve received a few keys by e-mail and
  saved them to temporary files. Again, the public keys look something like
  this:
</p>

<pre>
$ cat /tmp/id_rsa.john.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCB007n/ww+ouN4gSLKssMxXnBOvf9LGt4L
ojG6rs6hPB09j9R/T17/x4lhJA0F3FR1rP6kYBRsWj2aThGw6HXLm9/5zytK6Ztg3RPKK+4k
Yjh6541NYsnEAZuXz0jTTyAUfrtU3Z5E003C4oxOj6H0rfIF1kKI9MAQLMdpGW1GYEIgS9Ez
Sdfd8AcCIicTDWbqLAcU4UpkaX8KyGlLwsNuuGztobF8m72ALC/nLF6JLtPofwFBlgc+myiv
O7TCUSBdLQlgMVOFq1I2uPWQOkOWQAHukEOmfjy2jctxSDBQ220ymjaNsHT4kgtZg2AYYgPq
dAv8JggJICUvax2T9va5 gsg-keypair
</pre>

<p>You just append them to your <code>authorized_keys</code> file:</p>

<pre>
$ cat /tmp/id_rsa.john.pub &gt;&gt; ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.josie.pub &gt;&gt; ~/.ssh/authorized_keys
$ cat /tmp/id_rsa.jessica.pub &gt;&gt; ~/.ssh/authorized_keys
</pre>

<p>
  Key-based SSH authentication usually enforces security by requiring
  restricted rights on the involved files. To prevent SSH from refusing to
  work, type this:
</p>

<pre>
$ chmod -R go= ~/.ssh
</pre>

<p>
  Now, you can set up an empty repository for your users by running <code>git init</code> with the <code>--bare</code> option, which initializes the repository without a working directory:
</p>

<pre>
$ cd /opt/git
$ mkdir project.git
$ cd project.git
$ git --bare init
</pre>

<p>
  Then, John, Josie, or Jessica can push the first version of their project
  into that repository by adding it as a remote and pushing up a branch. Note
  that someone must shell onto the machine and create a bare repository every
  time you want to add a project. Let’s use <code>gitserver</code> as the hostname of the server on which you’ve set up your 'git' user and
  repository. If you’re running it internally, and you set up DNS for <code>gitserver</code> to point to that server, then you can use the commands pretty much as is:
</p>

<pre>
# on Johns computer
$ cd myproject
$ git init
$ git add .
$ git commit -m 'initial commit'
$ git remote add origin git@gitserver:/opt/git/project.git
$ git push origin master
</pre>

<p>
  At this point, the others can clone it down and push changes back up just as
  easily:
</p>

<pre>
$ git clone git@gitserver:/opt/git/project.git
$ cd project
$ vim README
$ git commit -am 'fix for the README file'
$ git push origin master
</pre>

<p>
  With this method, you can quickly get a read/write Git server up and running
  for a handful of developers.
</p>

<p>
  As an extra precaution, you can easily restrict the 'git' user to only doing
  Git activities with a limited shell tool called <code>git-shell</code> that comes with Git. If you set this as your 'git' user’s login shell, then
  the 'git' user can’t have normal shell access to your server. To use this,
  specify <code>git-shell</code> instead of bash or csh for your user’s login shell. To do so, you’ll likely
  have to edit your <code>/etc/passwd</code> file:
</p>

<pre>
$ sudo vim /etc/passwd
</pre>

<p>At the bottom, you should find a line that looks something like this:</p>

<pre>
git:x:1000:1000::/home/git:/bin/sh
</pre>

<p>
  Change <code>/bin/sh</code> to <code>/usr/bin/git-shell</code> (or run <code>which git-shell</code> to see where it’s installed). The line should look something like this:
</p>

<pre>
git:x:1000:1000::/home/git:/usr/bin/git-shell
</pre>

<p>
  Now, the 'git' user can only use the SSH connection to push and pull Git
  repositories and can’t shell onto the machine. If you try, you’ll see a login
  rejection like this:
</p>

<pre>
$ ssh git@gitserver
fatal: What do you think I am? A shell?
Connection to gitserver closed.
</pre>

<h2>Public Access</h2>

<p>
  What if you want anonymous read access to your project? Perhaps instead of
  hosting an internal private project, you want to host an open source project.
  Or maybe you have a bunch of automated build servers or continuous
  integration servers that change a lot, and you don’t want to have to generate
  SSH keys all the time — you just want to add simple anonymous read access.
</p>

<p>
  Probably the simplest way for smaller setups is to run a static web server
  with its document root where your Git repositories are, and then enable that <code>post-update</code> hook we mentioned in the first section of this chapter. Let’s work from the
  previous example. Say you have your repositories in the <code>/opt/git</code> directory, and an Apache server is running on your machine. Again, you can
  use any web server for this; but as an example, we’ll demonstrate some basic
  Apache configurations that should give you an idea of what you might need.
</p>

<p>First you need to enable the hook:</p>

<pre>
$ cd project.git
$ mv hooks/post-update.sample hooks/post-update
$ chmod a+x hooks/post-update
</pre>

<p>
  What does this <code>post-update</code> hook do? It looks basically like this:
</p>

<pre>
$ cat .git/hooks/post-update
#!/bin/sh
#
# An example hook script to prepare a packed repository for use over
# dumb transports.
#
# To enable this hook, rename this file to "post-update".
#

exec git-update-server-info
</pre>

<p>
  This means that when you push to the server via SSH, Git will run this
  command to update the files needed for HTTP fetching.
</p>

<p>
  Next, you need to add a VirtualHost entry to your Apache configuration with
  the document root as the root directory of your Git projects. Here, we’re
  assuming that you have wildcard DNS set up to send <code>*.gitserver</code> to whatever box you’re using to run all this:
</p>

<pre>
&lt;VirtualHost *:80&gt;
    ServerName git.gitserver
    DocumentRoot /opt/git
    &lt;Directory /opt/git/&gt;
        Order allow, deny
        allow from all
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;
</pre>

<p>
  You’ll also need to set the Unix user group of the <code>/opt/git</code> directories to <code>www-data</code> so your web server can read-access the repositories, because the Apache
  instance running the CGI script will (by default) be running as that user:
</p>

<pre>
$ chgrp -R www-data /opt/git
</pre>

<p>
  When you restart Apache, you should be able to clone your repositories under
  that directory by specifying the URL for your project:
</p>

<pre>
$ git clone http://git.gitserver/project.git
</pre>

<p>
  This way, you can set up HTTP-based read access to any of your projects for a
  fair number of users in a few minutes. Another simple option for public
  unauthenticated access is to start a Git daemon, although that requires you
  to daemonize the process - we’ll cover this option in the next section, if
  you prefer that route.
</p>

<h2>GitWeb</h2>

<p>
  Now that you have basic read/write and read-only access to your project, you
  may want to set up a simple web-based visualizer. Git comes with a CGI script
  called GitWeb that is commonly used for this. You can see GitWeb in use at
  sites like <code>http://git.kernel.org</code> (see Figure 4-1).
</p>

<p>Insert 18333fig0401.png Figure 4-1. The GitWeb web-based user interface.</p>

<p>
  If you want to check out what GitWeb would look like for your project, Git
  comes with a command to fire up a temporary instance if you have a
  lightweight server on your system like <code>lighttpd</code> or <code>webrick</code>. On Linux machines, <code>lighttpd</code> is often installed, so you may be able to get it to run by typing <code>git instaweb</code> in your project directory. If you’re running a Mac, Leopard comes
  preinstalled with Ruby, so <code>webrick</code> may be your best bet. To start <code>instaweb</code> with a non-lighttpd handler, you can run it with the <code>--httpd</code> option.
</p>

<pre>
$ git instaweb --httpd=webrick
[2009-02-21 10:02:21] INFO  WEBrick 1.3.1
[2009-02-21 10:02:21] INFO  ruby 1.8.6 (2008-03-03) [universal-darwin9.0]
</pre>

<p>
  That starts up an HTTPD server on port 1234 and then automatically starts a
  web browser that opens on that page. It’s pretty easy on your part. When
  you’re done and want to shut down the server, you can run the same command
  with the <code>--stop</code> option:
</p>

<pre>
$ git instaweb --httpd=webrick --stop
</pre>

<p>
  If you want to run the web interface on a server all the time for your team
  or for an open source project you’re hosting, you’ll need to set up the CGI
  script to be served by your normal web server. Some Linux distributions have
  a <code>gitweb</code> package that you may be able to install via <code>apt</code> or <code>yum</code>, so you may want to try that first. We’ll walk though installing GitWeb
  manually very quickly. First, you need to get the Git source code, which
  GitWeb comes with, and generate the custom CGI script:
</p>

<pre>
$ git clone git://git.kernel.org/pub/scm/git/git.git
$ cd git/
$ make GITWEB_PROJECTROOT="/opt/git" \
        prefix=/usr gitweb
$ sudo cp -Rf gitweb /var/www/
</pre>

<p>
  Notice that you have to tell the command where to find your Git repositories
  with the <code>GITWEB_PROJECTROOT</code> variable. Now, you need to make Apache use CGI for that script, for which
  you can add a VirtualHost:
</p>

<pre>
&lt;VirtualHost *:80&gt;
    ServerName gitserver
    DocumentRoot /var/www/gitweb
    &lt;Directory /var/www/gitweb&gt;
        Options ExecCGI +FollowSymLinks +SymLinksIfOwnerMatch
        AllowOverride All
        order allow,deny
        Allow from all
        AddHandler cgi-script cgi
        DirectoryIndex gitweb.cgi
    &lt;/Directory&gt;
&lt;/VirtualHost&gt;
</pre>

<p>
  Again, GitWeb can be served with any CGI capable web server; if you prefer to
  use something else, it shouldn’t be difficult to set up. At this point, you
  should be able to visit <code>http://gitserver/</code> to view your repositories online, and you can use <code>http://git.gitserver</code> to clone and fetch your repositories over HTTP.
</p>

<h2>Gitosis</h2>

<p>
  Keeping all users’ public keys in the <code>authorized_keys</code> file for access works well only for a while. When you have hundreds of
  users, it’s much more of a pain to manage that process. You have to shell
  onto the server each time, and there is no access control — everyone in the
  file has read and write access to every project.
</p>

<p>
  At this point, you may want to turn to a widely used software project called
  Gitosis. Gitosis is basically a set of scripts that help you manage the <code>authorized_keys</code> file as well as implement some simple access controls. The really
  interesting part is that the UI for this tool for adding people and
  determining access isn’t a web interface but a special Git repository. You
  set up the information in that project; and when you push it, Gitosis
  reconfigures the server based on that, which is cool.
</p>

<p>
  Installing Gitosis isn’t the simplest task ever, but it’s not too difficult.
  It’s easiest to use a Linux server for it — these examples use a stock Ubuntu
  8.10 server.
</p>

<p>
  Gitosis requires some Python tools, so first you have to install the Python
  setuptools package, which Ubuntu provides as python-setuptools:
</p>

<pre>
$ apt-get install python-setuptools
</pre>

<p>Next, you clone and install Gitosis from the project’s main site:</p>

<pre>
$ git clone https://github.com/tv42/gitosis.git
$ cd gitosis
$ sudo python setup.py install
</pre>

<p>
  That installs a couple of executables that Gitosis will use. Next, Gitosis
  wants to put its repositories under <code>/home/git</code>, which is fine. But you have already set up your repositories in <code>/opt/git</code>, so instead of reconfiguring everything, you create a symlink:
</p>

<pre>
$ ln -s /opt/git /home/git/repositories
</pre>

<p>
  Gitosis is going to manage your keys for you, so you need to remove the
  current file, re-add the keys later, and let Gitosis control the <code>authorized_keys</code> file automatically. For now, move the <code>authorized_keys</code> file out of the way:
</p>

<pre>
$ mv /home/git/.ssh/authorized_keys /home/git/.ssh/ak.bak
</pre>

<p>
  Next you need to turn your shell back on for the 'git' user, if you changed
  it to the <code>git-shell</code> command. People still won’t be able to log in, but Gitosis will control that
  for you. So, let’s change this line in your <code>/etc/passwd</code> file
</p>

<pre>
git:x:1000:1000::/home/git:/usr/bin/git-shell
</pre>

<p>back to this:</p>

<pre>
git:x:1000:1000::/home/git:/bin/sh
</pre>

<p>
  Now it’s time to initialize Gitosis. You do this by running the <code>gitosis-init</code> command with your personal public key. If your public key isn’t on the
  server, you’ll have to copy it there:
</p>

<pre>
$ sudo -H -u git gitosis-init &lt; /tmp/id_dsa.pub
Initialized empty Git repository in /opt/git/gitosis-admin.git/
Reinitialized existing Git repository in /opt/git/gitosis-admin.git/
</pre>

<p>
  This lets the user with that key modify the main Git repository that controls
  the Gitosis setup. Next, you have to manually set the execute bit on the <code>post-update</code> script for your new control repository.
</p>

<pre>
$ sudo chmod 755 /opt/git/gitosis-admin.git/hooks/post-update
</pre>

<p>
  You’re ready to roll. If you’re set up correctly, you can try to SSH into
  your server as the user for which you added the public key to initialize
  Gitosis. You should see something like this:
</p>

<pre>
$ ssh git@gitserver
PTY allocation request failed on channel 0
ERROR:gitosis.serve.main:Need SSH_ORIGINAL_COMMAND in environment.
  Connection to gitserver closed.
</pre>

<p>
  That means Gitosis recognized you but shut you out because you’re not trying
  to do any Git commands. So, let’s do an actual Git command — you’ll clone the
  Gitosis control repository:
</p>

<pre>
# on your local computer
$ git clone git@gitserver:gitosis-admin.git
</pre>

<p>
  Now you have a directory named <code>gitosis-admin</code>, which has two major parts:
</p>

<pre>
$ cd gitosis-admin
$ find .
./gitosis.conf
./keydir
./keydir/scott.pub
</pre>

<p>
  The <code>gitosis.conf</code> file is the control file you use to specify users, repositories, and
  permissions. The <code>keydir</code> directory is where you store the public keys of all the users who have any
  sort of access to your repositories — one file per user. The name of the file
  in <code>keydir</code> (in the previous example, <code>scott.pub</code>) will be different for you — Gitosis takes that name from the description at
  the end of the public key that was imported with the <code>gitosis-init</code> script.
</p>

<p>
  If you look at the <code>gitosis.conf</code> file, it should only specify information about the <code>gitosis-admin</code> project that you just cloned:
</p>

<pre>
$ cat gitosis.conf
[gitosis]

[group gitosis-admin]
members = scott
writable = gitosis-admin
</pre>

<p>
  It shows you that the 'scott' user — the user with whose public key you
  initialized Gitosis — is the only one who has access to the <code>gitosis-admin</code> project.
</p>

<p>
  Now, let’s add a new project for you. You’ll add a new section called <code>mobile</code> where you’ll list the developers on your mobile team and projects that those
  developers need access to. Because 'scott' is the only user in the system
  right now, you’ll add him as the only member, and you’ll create a new project
  called <code>iphone_project</code> to start on:
</p>

<pre>
[group mobile]
members = scott
writable = iphone_project
</pre>

<p>
  Whenever you make changes to the <code>gitosis-admin</code> project, you have to commit the changes and push them back up to the server
  in order for them to take effect:
</p>

<pre>
$ git commit -am 'add iphone_project and mobile group'
[master 8962da8] add iphone_project and mobile group
 1 file changed, 4 insertions(+)
$ git push origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 272 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@gitserver:gitosis-admin.git
   fb27aec..8962da8  master -&gt; master
</pre>

<p>
  You can make your first push to the new <code>iphone_project</code> project by adding your server as a remote to your local version of the
  project and pushing. You no longer have to manually create a bare repository
  for new projects on the server — Gitosis creates them automatically when it
  sees the first push:
</p>

<pre>
$ git remote add origin git@gitserver:iphone_project.git
$ git push origin master
Initialized empty Git repository in /opt/git/iphone_project.git/
Counting objects: 3, done.
Writing objects: 100% (3/3), 230 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@gitserver:iphone_project.git
 * [new branch]      master -&gt; master
</pre>

<p>
  Notice that you don’t need to specify the path (in fact, doing so won’t
  work), just a colon and then the name of the project — Gitosis finds it for
  you.
</p>

<p>
  You want to work on this project with your friends, so you’ll have to re-add
  their public keys. But instead of appending them manually to the <code>~/.ssh/authorized_keys</code> file on your server, you’ll add them, one key per file, into the <code>keydir</code> directory. How you name the keys determines how you refer to the users in
  the <code>gitosis.conf</code> file. Let’s re-add the public keys for John, Josie, and Jessica:
</p>

<pre>
$ cp /tmp/id_rsa.john.pub keydir/john.pub
$ cp /tmp/id_rsa.josie.pub keydir/josie.pub
$ cp /tmp/id_rsa.jessica.pub keydir/jessica.pub
</pre>

<p>
  Now you can add them all to your 'mobile' team so they have read and write
  access to <code>iphone_project</code>:
</p>

<pre>
[group mobile]
members = scott john josie jessica
writable = iphone_project
</pre>

<p>
  After you commit and push that change, all four users will be able to read
  from and write to that project.
</p>

<p>
  Gitosis has simple access controls as well. If you want John to have only
  read access to this project, you can do this instead:
</p>

<pre>
[group mobile]
members = scott josie jessica
writable = iphone_project

[group mobile_ro]
members = john
readonly = iphone_project
</pre>

<p>
  Now John can clone the project and get updates, but Gitosis won’t allow him
  to push back up to the project. You can create as many of these groups as you
  want, each containing different users and projects. You can also specify
  another group as one of the members (using <code>@</code> as prefix), to inherit all of its members automatically:
</p>

<pre>
[group mobile_committers]
members = scott josie jessica

[group mobile]
members   = @mobile_committers
writable  = iphone_project

[group mobile_2]
members   = @mobile_committers john
writable  = another_iphone_project
</pre>

<p>
  If you have any issues, it may be useful to add <code>loglevel=DEBUG</code> under the <code>[gitosis]</code> section. If you’ve lost push access by pushing a messed-up configuration,
  you can manually fix the file on the server under <code>/home/git/.gitosis.conf</code> — the file from which Gitosis reads its info. A push to the project takes
  the <code>gitosis.conf</code> file you just pushed up and sticks it there. If you edit that file manually,
  it remains like that until the next successful push to the <code>gitosis-admin</code> project.
</p>

<h2>Gitolite</h2>

<p>
  This section serves as a quick introduction to Gitolite, and provides basic
  installation and setup instructions.&emsp14;&emsp14;It cannot, however,
  replace the enormous amount of <a href="http://sitaramc.github.com/gitolite/master-toc.html">documentation</a> that Gitolite comes with.&emsp14;&emsp14;There may also be occasional
  changes to this section itself, so you may also want to look at the latest
  version <a href="http://sitaramc.github.com/gitolite/progit.html">here</a>.
</p>

<p>
  Gitolite is an authorization layer on top of Git, relying on <code>sshd</code> or <code>httpd</code> for authentication.&emsp14;&emsp14;(Recap: authentication is identifying who
  the user is, authorization is deciding if he is allowed to do what he is
  attempting to).
</p>

<p>
  Gitolite allows you to specify permissions not just by repository, but also
  by branch or tag names within each repository.&emsp14;&emsp14;That is, you
  can specify that certain people (or groups of people) can only push certain
  "refs" (branches or tags) but not others.
</p>

<h3>Installing</h3>

<p>
  Installing Gitolite is very easy, even if you don’t read the extensive
  documentation that comes with it.&emsp14;&emsp14;You need an account on a
  Unix server of some kind.&emsp14;&emsp14;You do not need root access,
  assuming Git, Perl, and an OpenSSH compatible SSH server are already
  installed.&emsp14;&emsp14;In the examples below, we will use the <code>git</code> account on a host called <code>gitserver</code>.
</p>

<p>
  Gitolite is somewhat unusual as far as "server" software goes — access is via
  SSH, and so every userid on the server is a potential "gitolite
  host".&emsp14;&emsp14;We will describe the simplest install method in this
  article; for the other methods please see the documentation.
</p>

<p>
  To begin, create a user called <code>git</code> on your server and login to this user.&emsp14;&emsp14;Copy your SSH public
  key (a file called <code>~/.ssh/id_rsa.pub</code> if you did a plain <code>ssh-keygen</code> with all the defaults) from your workstation, renaming it to <code>&lt;yourname>.pub</code> (we'll use <code>scott.pub</code> in our examples).&emsp14;&emsp14;Then run these commands:
</p>

<pre>
$ git clone git://github.com/sitaramc/gitolite
$ gitolite/install -ln
    # assumes $HOME/bin exists and is in your $PATH
$ gitolite setup -pk $HOME/scott.pub
</pre>

<p>
  That last command creates new Git repository called <code>gitolite-admin</code> on the server.
</p>

<p>
  Finally, back on your workstation, run <code>git clone git@gitserver:gitolite-admin</code>. And you’re done!&emsp14;&emsp14;Gitolite has now been installed on the
  server, and you now have a brand new repository called <code>gitolite-admin</code> in your workstation.&emsp14;&emsp14;You administer your Gitolite setup by
  making changes to this repository and pushing.
</p>

<h3>Customising the Install</h3>

<p>
  While the default, quick, install works for most people, there are some ways
  to customise the install if you need to.&emsp14;&emsp14;Some changes can be
  made simply by editing the rc file, but if that is not sufficient, there’s
  documentation on customising Gitolite.
</p>

<h3>Config File and Access Control Rules</h3>

<p>
  Once the install is done, you switch to the <code>gitolite-admin</code> clone you just made on your workstation, and poke around to see what you
  got:
</p>

<pre>
$ cd ~/gitolite-admin/
$ ls
conf/  keydir/
$ find conf keydir -type f
conf/gitolite.conf
keydir/scott.pub
$ cat conf/gitolite.conf

repo gitolite-admin
    RW+                 = scott

repo testing
    RW+                 = @all
</pre>

<p>
  Notice that "scott" (the name of the pubkey in the <code>gitolite setup</code> command you used earlier) has read-write permissions on the <code>gitolite-admin</code> repository as well as a public key file of the same name.
</p>

<p>
  Adding users is easy.&emsp14;&emsp14;To add a user called "alice", obtain her
  public key, name it <code>alice.pub</code>, and put it in the <code>keydir</code> directory of the clone of the <code>gitolite-admin</code> repo you just made on your workstation.&emsp14;&emsp14;Add, commit, and push
  the change, and the user has been added.
</p>

<p>
  The config file syntax for Gitolite is well documented, so we’ll only mention
  some highlights here.
</p>

<p>
  You can group users or repos for convenience.&emsp14;&emsp14;The group names
  are just like macros; when defining them, it doesn’t even matter whether they
  are projects or users; that distinction is only made when you <i>use</i> the "macro".
</p>

<pre>
@oss_repos      = linux perl rakudo git gitolite
@secret_repos   = fenestra pear

@admins         = scott
@interns        = ashok
@engineers      = sitaram dilbert wally alice
@staff          = @admins @engineers @interns
</pre>

<p>
  You can control permissions at the "ref" level.&emsp14;&emsp14;In the
  following example, interns can only push the "int"
  branch.&emsp14;&emsp14;Engineers can push any branch whose name starts with
  "eng-", and tags that start with "rc" followed by a digit.&emsp14;&emsp14;And
  the admins can do anything (including rewind) to any ref.
</p>

<pre>
repo @oss_repos
    RW  int$                = @interns
    RW  eng-                = @engineers
    RW  refs/tags/rc[0-9]   = @engineers
    RW+                     = @admins
</pre>

<p>
  The expression after the <code>RW</code> or <code>RW+</code> is a regular expression (regex) that the refname (ref) being pushed is
  matched against.&emsp14;&emsp14;So we call it a "refex"!&emsp14;&emsp14;Of
  course, a refex can be far more powerful than shown here, so don’t overdo it
  if you’re not comfortable with Perl regexes.
</p>

<p>
  Also, as you probably guessed, Gitolite prefixes <code>refs/heads/</code> as a syntactic convenience if the refex does not begin with <code>refs/</code>.
</p>

<p>
  An important feature of the config file’s syntax is that all the rules for a
  repository need not be in one place.&emsp14;&emsp14;You can keep all the
  common stuff together, like the rules for all <code>oss_repos</code> shown above, then add specific rules for specific cases later on, like so:
</p>

<pre>
repo gitolite
    RW+                     = sitaram
</pre>

<p>
  That rule will just get added to the ruleset for the <code>gitolite</code> repository.
</p>

<p>
  At this point you might be wondering how the access control rules are
  actually applied, so let’s go over that briefly.
</p>

<p>
  There are two levels of access control in Gitolite.&emsp14;&emsp14;The first
  is at the repository level; if you have read (or write) access to <i>any</i> ref in the repository, then you have read (or write) access to the
  repository.
</p>

<p>
  The second level, applicable only to "write" access, is by branch or tag
  within a repository.&emsp14;&emsp14;The username, the access being attempted
  (<code>W</code> or <code>+</code>), and the refname being updated are known.&emsp14;&emsp14;The access rules
  are checked in order of appearance in the config file, looking for a match
  for this combination (but remember that the refname is regex-matched, not
  merely string-matched).&emsp14;&emsp14;If a match is found, the push
  succeeds.&emsp14;&emsp14;A fallthrough results in access being denied.
</p>

<h3>Advanced Access Control with "deny" rules</h3>

<p>
  So far, we’ve only seen permissions to be one of <code>R</code>, <code>RW</code>, or <code>RW+</code>.&emsp14;&emsp14;However, Gitolite allows another permission: <code>-</code>, standing for "deny".&emsp14;&emsp14;This gives you a lot more power, at the
  expense of some complexity, because now fallthrough is not the <i>only</i> way for access to be denied, so the <i>order of the rules now matters</i>!
</p>

<p>
  Let us say, in the situation above, we want engineers to be able to rewind
  any branch <i>except</i> master and integ.&emsp14;&emsp14;Here’s how to do that:
</p>

<pre>
    RW  master integ    = @engineers
    -   master integ    = @engineers
    RW+                 = @engineers
</pre>

<p>
  Again, you simply follow the rules top down until you hit a match for your
  access mode, or a deny.&emsp14;&emsp14;Non-rewind push to master or integ is
  allowed by the first rule.&emsp14;&emsp14;A rewind push to those refs does
  not match the first rule, drops down to the second, and is therefore
  denied.&emsp14;&emsp14;Any push (rewind or non-rewind) to refs other than
  master or integ won’t match the first two rules anyway, and the third rule
  allows it.
</p>

<h3>Restricting pushes by files changed</h3>

<p>
  In addition to restricting what branches a user can push changes to, you can
  also restrict what files they are allowed to touch.&emsp14;&emsp14;For
  example, perhaps the Makefile (or some other program) is really not supposed
  to be changed by just anyone, because a lot of things depend on it or would
  break if the changes are not done <i>just right</i>.&emsp14;&emsp14;You can tell Gitolite:
</p>

<pre>
repo foo
    RW                      =   @junior_devs @senior_devs

    -   VREF/NAME/Makefile  =   @junior_devs
</pre>

<p>
  Users who are migrating from the older Gitolite should note that there is a
  significant change in behaviour with regard to this feature; please see the
  migration guide for details.
</p>

<h3>Personal Branches</h3>

<p>
  Gitolite also has a feature called "personal branches" (or rather, "personal
  branch namespace") that can be very useful in a corporate environment.
</p>

<p>
  A lot of code exchange in the Git world happens by "please pull"
  requests.&emsp14;&emsp14;In a corporate environment, however, unauthenticated
  access is a no-no, and a developer workstation cannot do authentication, so
  you have to push to the central server and ask someone to pull from there.
</p>

<p>
  This would normally cause the same branch name clutter as in a centralised
  VCS, plus setting up permissions for this becomes a chore for the admin.
</p>

<p>
  Gitolite lets you define a "personal" or "scratch" namespace prefix for each
  developer (for example, <code>refs/personal/&lt;devname>/*</code>); please see the documentation for details.
</p>

<h3>"Wildcard" repositories</h3>

<p>
  Gitolite allows you to specify repositories with wildcards (actually Perl
  regexes), like, for example <code>assignments/s[0-9][0-9]/a[0-9][0-9]</code>, to pick a random example.&emsp14;&emsp14;It also allows you to assign a new
  permission mode (<code>C</code>) which enables users to create repositories based on such wild cards,
  automatically assigns ownership to the specific user who created it, allows
  him/her to hand out <code>R</code> and <code>RW</code> permissions to other users to collaborate, etc.&emsp14;&emsp14;Again, please
  see the documentation for details.
</p>

<h3>Other Features</h3>

<p>
  We’ll round off this discussion with a sampling of other features, all of
  which, and many more, are described in great detail in the documentation.
</p>

<p>
  <b>Logging</b>: Gitolite logs all successful accesses.&emsp14;&emsp14;If you were somewhat
  relaxed about giving people rewind permissions (<code>RW+</code>) and some kid blew away <code>master</code>, the log file is a life saver, in terms of easily and quickly finding the
  SHA that got hosed.
</p>

<p>
  <b>Access rights reporting</b>: Another convenient feature is what happens when you try and just ssh to the
  server.&emsp14;&emsp14;Gitolite shows you what repos you have access to, and
  what that access may be.&emsp14;&emsp14;Here’s an example:
</p>

<pre>
    hello scott, this is git@git running gitolite3 v3.01-18-g9609868 on git 1.7.4.4

         R     anu-wsd
         R     entrans
         R  W  git-notes
         R  W  gitolite
         R  W  gitolite-admin
         R     indic_web_input
         R     shreelipi_converter
</pre>

<p>
  <b>Delegation</b>: For really large installations, you can delegate responsibility for groups
  of repositories to various people and have them manage those pieces
  independently.&emsp14;&emsp14;This reduces the load on the main admin, and
  makes him less of a bottleneck.
</p>

<p>
  <b>Mirroring</b>: Gitolite can help you maintain multiple mirrors, and switch between them
  easily if the primary server goes down.
</p>

<h2>Git Daemon</h2>

<p>
  For public, unauthenticated read access to your projects, you’ll want to move
  past the HTTP protocol and start using the Git protocol. The main reason is
  speed. The Git protocol is far more efficient and thus faster than the HTTP
  protocol, so using it will save your users time.
</p>

<p>
  Again, this is for unauthenticated read-only access. If you’re running this
  on a server outside your firewall, it should only be used for projects that
  are publicly visible to the world. If the server you’re running it on is
  inside your firewall, you might use it for projects that a large number of
  people or computers (continuous integration or build servers) have read-only
  access to, when you don’t want to have to add an SSH key for each.
</p>

<p>
  In any case, the Git protocol is relatively easy to set up. Basically, you
  need to run this command in a daemonized manner:
</p>

<pre>
git daemon --reuseaddr --base-path=/opt/git/ /opt/git/
</pre>

<p>
  <code>--reuseaddr</code> allows the server to restart without waiting for old connections to time
  out, the <code>--base-path</code> option allows people to clone projects without specifying the entire path,
  and the path at the end tells the Git daemon where to look for repositories
  to export. If you’re running a firewall, you’ll also need to punch a hole in
  it at port 9418 on the box you’re setting this up on.
</p>

<p>
  You can daemonize this process a number of ways, depending on the operating
  system you’re running. On an Ubuntu machine, you use an Upstart script. So,
  in the following file
</p>

<pre>
/etc/event.d/local-git-daemon
</pre>

<p>you put this script:</p>

<pre>
start on startup
stop on shutdown
exec /usr/bin/git daemon \
    --user=git --group=git \
    --reuseaddr \
    --base-path=/opt/git/ \
    /opt/git/
respawn
</pre>

<p>
  For security reasons, it is strongly encouraged to have this daemon run as a
  user with read-only permissions to the repositories — you can easily do this
  by creating a new user 'git-ro' and running the daemon as
  them.&emsp14;&emsp14;For the sake of simplicity we’ll simply run it as the
  same 'git' user that Gitosis is running as.
</p>

<p>
  When you restart your machine, your Git daemon will start automatically and
  respawn if it goes down. To get it running without having to reboot, you can
  run this:
</p>

<pre>
initctl start local-git-daemon
</pre>

<p>
  On other systems, you may want to use <code>xinetd</code>, a script in your <code>sysvinit</code> system, or something else — as long as you get that command daemonized and
  watched somehow.
</p>

<p>
  Next, you have to tell your Gitosis server which repositories to allow
  unauthenticated Git server-based access to. If you add a section for each
  repository, you can specify the ones from which you want your Git daemon to
  allow reading. If you want to allow Git protocol access for the <code>iphone_project</code>, you add this to the end of the <code>gitosis.conf</code> file:
</p>

<pre>
[repo iphone_project]
daemon = yes
</pre>

<p>
  When that is committed and pushed up, your running daemon should start
  serving requests for the project to anyone who has access to port 9418 on
  your server.
</p>

<p>
  If you decide not to use Gitosis, but you want to set up a Git daemon, you’ll
  have to run this on each project you want the Git daemon to serve:
</p>

<pre>
$ cd /path/to/project.git
$ touch git-daemon-export-ok
</pre>

<p>
  The presence of that file tells Git that it’s OK to serve this project
  without authentication.
</p>

<p>
  Gitosis can also control which projects GitWeb shows. First, you need to add
  something like the following to the <code>/etc/gitweb.conf</code> file:
</p>

<pre>
$projects_list = "/home/git/gitosis/projects.list";
$projectroot = "/home/git/repositories";
$export_ok = "git-daemon-export-ok";
@git_base_url_list = ('git://gitserver');
</pre>

<p>
  You can control which projects GitWeb lets users browse by adding or removing
  a <code>gitweb</code> setting in the Gitosis configuration file. For instance, if you want the <code>iphone_project</code> to show up on GitWeb, you make the <code>repo</code> setting look like this:
</p>

<pre>
[repo iphone_project]
daemon = yes
gitweb = yes
</pre>

<p>
  Now, if you commit and push the project, GitWeb will automatically start
  showing the <code>iphone_project</code>.
</p>

<h2>Hosted Git</h2>

<p>
  If you don’t want to go through all of the work involved in setting up your
  own Git server, you have several options for hosting your Git projects on an
  external dedicated hosting site. Doing so offers a number of advantages: a
  hosting site is generally quick to set up and easy to start projects on, and
  no server maintenance or monitoring is involved. Even if you set up and run
  your own server internally, you may still want to use a public hosting site
  for your open source code — it’s generally easier for the open source
  community to find and help you with.
</p>

<p>
  These days, you have a huge number of hosting options to choose from, each
  with different advantages and disadvantages. To see an up-to-date list, check
  out the following page:
</p>

<pre>
https://git.wiki.kernel.org/index.php/GitHosting
</pre>

<p>
  Because we can’t cover all of them, and because I happen to work at one of
  them, we’ll use this section to walk through setting up an account and
  creating a new project at GitHub. This will give you an idea of what is
  involved.
</p>

<p>
  GitHub is by far the largest open source Git hosting site and it’s also one
  of the very few that offers both public and private hosting options so you
  can keep your open source and private commercial code in the same place. In
  fact, we used GitHub to privately collaborate on this book.
</p>

<h3>GitHub</h3>

<p>
  GitHub is slightly different than most code-hosting sites in the way that it
  namespaces projects. Instead of being primarily based on the project, GitHub
  is user-centric. That means when I host my <code>grit</code> project on GitHub, you won’t find it at <code>github.com/grit</code> but instead at <code>github.com/schacon/grit</code>. There is no canonical version of any project, which allows a project to
  move from one user to another seamlessly if the first author abandons the
  project.
</p>

<p>
  GitHub is also a commercial company that charges for accounts that maintain
  private repositories, but anyone can quickly get a free account to host as
  many open source projects as they want. We’ll quickly go over how that is
  done.
</p>

<h3>Setting Up a User Account</h3>

<p>
  The first thing you need to do is set up a free user account. If you visit
  the "Plans and pricing" page at <code>https://github.com/pricing</code> and click the "Sign Up" button on the Free account (see Figure 4-2), you’re
  taken to the signup page.
</p>

<p>Insert 18333fig0402.png Figure 4-2. The GitHub plan page.</p>

<p>
  Here you must choose a username that isn’t yet taken in the system and enter
  an e-mail address that will be associated with the account and a password
  (see Figure 4-3).
</p>

<p>Insert 18333fig0403.png Figure 4-3. The GitHub user signup form.</p>

<p>
  If you have it available, this is a good time to add your public SSH key as
  well. We covered how to generate a new key earlier, in the "Simple Setups"
  section. Take the contents of the public key of that pair, and paste it into
  the SSH Public Key text box. Clicking the "explain ssh keys" link takes you
  to detailed instructions on how to do so on all major operating systems.
  Clicking the "I agree, sign me up" button takes you to your new user
  dashboard (see Figure 4-4).
</p>

<p>Insert 18333fig0404.png Figure 4-4. The GitHub user dashboard.</p>

<p>Next you can create a new repository.</p>

<h3>Creating a New Repository</h3>

<p>
  Start by clicking the "create a new one" link next to Your Repositories on
  the user dashboard. You’re taken to the Create a New Repository form (see
  Figure 4-5).
</p>

<p>Insert 18333fig0405.png Figure 4-5. Creating a new repository on GitHub.</p>

<p>
  All you really have to do is provide a project name, but you can also add a
  description. When that is done, click the "Create Repository" button. Now you
  have a new repository on GitHub (see Figure 4-6).
</p>

<p>Insert 18333fig0406.png Figure 4-6. GitHub project header information.</p>

<p>
  Since you have no code there yet, GitHub will show you instructions for how
  create a brand-new project, push an existing Git project up, or import a
  project from a public Subversion repository (see Figure 4-7).
</p>

<p>Insert 18333fig0407.png Figure 4-7. Instructions for a new repository.</p>

<p>
  These instructions are similar to what we’ve already gone over. To initialize
  a project if it isn’t already a Git project, you use
</p>

<pre>
$ git init
$ git add .
$ git commit -m 'initial commit'
</pre>

<p>
  When you have a Git repository locally, add GitHub as a remote and push up
  your master branch:
</p>

<pre>
$ git remote add origin git@github.com:testinguser/iphone_project.git
$ git push origin master
</pre>

<p>
  Now your project is hosted on GitHub, and you can give the URL to anyone you
  want to share your project with. In this case, it’s <code>http://github.com/testinguser/iphone_project</code>. You can also see from the header on each of your project’s pages that you
  have two Git URLs (see Figure 4-8).
</p>

<p>
  Insert 18333fig0408.png Figure 4-8. Project header with a public URL and a
  private URL.
</p>

<p>
  The Public Clone URL is a public, read-only Git URL over which anyone can
  clone the project. Feel free to give out that URL and post it on your web
  site or what have you.
</p>

<p>
  The Your Clone URL is a read/write SSH-based URL that you can read or write
  over only if you connect with the SSH private key associated with the public
  key you uploaded for your user. When other users visit this project page,
  they won’t see that URL—only the public one.
</p>

<h3>Importing from Subversion</h3>

<p>
  If you have an existing public Subversion project that you want to import
  into Git, GitHub can often do that for you. At the bottom of the instructions
  page is a link to a Subversion import. If you click it, you see a form with
  information about the import process and a text box where you can paste in
  the URL of your public Subversion project (see Figure 4-9).
</p>

<p>Insert 18333fig0409.png Figure 4-9. Subversion importing interface.</p>

<p>
  If your project is very large, nonstandard, or private, this process probably
  won’t work for you. In Chapter 7, you’ll learn how to do more complicated
  manual project imports.
</p>

<h3>Adding Collaborators</h3>

<p>
  Let’s add the rest of the team. If John, Josie, and Jessica all sign up for
  accounts on GitHub, and you want to give them push access to your repository,
  you can add them to your project as collaborators. Doing so will allow pushes
  from their public keys to work.
</p>

<p>
  Click the "edit" button in the project header or the Admin tab at the top of
  the project to reach the Admin page of your GitHub project (see Figure 4-10).
</p>

<p>Insert 18333fig0410.png Figure 4-10. GitHub administration page.</p>

<p>
  To give another user write access to your project, click the “Add another
  collaborator” link. A new text box appears, into which you can type a
  username. As you type, a helper pops up, showing you possible username
  matches. When you find the correct user, click the Add button to add that
  user as a collaborator on your project (see Figure 4-11).
</p>

<p>
  Insert 18333fig0411.png Figure 4-11. Adding a collaborator to your project.
</p>

<p>
  When you’re finished adding collaborators, you should see a list of them in
  the Repository Collaborators box (see Figure 4-12).
</p>

<p>
  Insert 18333fig0412.png Figure 4-12. A list of collaborators on your project.
</p>

<p>
  If you need to revoke access to individuals, you can click the "revoke" link,
  and their push access will be removed. For future projects, you can also copy
  collaborator groups by copying the permissions of an existing project.
</p>

<h3>Your Project</h3>

<p>
  After you push your project up or have it imported from Subversion, you have
  a main project page that looks something like Figure 4-13.
</p>

<p>Insert 18333fig0413.png Figure 4-13. A GitHub main project page.</p>

<p>
  When people visit your project, they see this page. It contains tabs to
  different aspects of your projects. The Commits tab shows a list of commits
  in reverse chronological order, similar to the output of the <code>git log</code> command. The Network tab shows all the people who have forked your project
  and contributed back. The Downloads tab allows you to upload project binaries
  and link to tarballs and zipped versions of any tagged points in your
  project. The Wiki tab provides a wiki where you can write documentation or
  other information about your project. The Graphs tab has some contribution
  visualizations and statistics about your project. The main Source tab that
  you land on shows your project’s main directory listing and automatically
  renders the README file below it if you have one. This tab also shows a box
  with the latest commit information.
</p>

<h3>Forking Projects</h3>

<p>
  If you want to contribute to an existing project to which you don’t have push
  access, GitHub encourages forking the project. When you land on a project
  page that looks interesting and you want to hack on it a bit, you can click
  the "fork" button in the project header to have GitHub copy that project to
  your user so you can push to it.
</p>

<p>
  This way, projects don’t have to worry about adding users as collaborators to
  give them push access. People can fork a project and push to it, and the main
  project maintainer can pull in those changes by adding them as remotes and
  merging in their work.
</p>

<p>
  To fork a project, visit the project page (in this case, mojombo/chronic) and
  click the "fork" button in the header (see Figure 4-14).
</p>

<p>
  Insert 18333fig0414.png Figure 4-14. Get a writable copy of any repository by
  clicking the "fork" button.
</p>

<p>
  After a few seconds, you’re taken to your new project page, which indicates
  that this project is a fork of another one (see Figure 4-15).
</p>

<p>Insert 18333fig0415.png Figure 4-15. Your fork of a project.</p>

<h3>GitHub Summary</h3>

<p>
  That’s all we’ll cover about GitHub, but it’s important to note how quickly
  you can do all this. You can create an account, add a new project, and push
  to it in a matter of minutes. If your project is open source, you also get a
  huge community of developers who now have visibility into your project and
  may well fork it and help contribute to it. At the very least, this may be a
  way to get up and running with Git and try it out quickly.
</p>

<h2>Summary</h2>

<p>
  You have several options to get a remote Git repository up and running so
  that you can collaborate with others or share your work.
</p>

<p>
  Running your own server gives you a lot of control and allows you to run the
  server within your own firewall, but such a server generally requires a fair
  amount of your time to set up and maintain. If you place your data on a
  hosted server, it’s easy to set up and maintain; however, you have to be able
  to keep your code on someone else’s servers, and some organizations don’t
  allow that.
</p>

<p>
  It should be fairly straightforward to determine which solution or
  combination of solutions is appropriate for you and your organization.
</p>

<h1>Distributed Git</h1>

<p>
  Now that you have a remote Git repository set up as a point for all the
  developers to share their code, and you’re familiar with basic Git commands
  in a local workflow, you’ll look at how to utilize some of the distributed
  workflows that Git affords you.
</p>

<p>
  In this chapter, you’ll see how to work with Git in a distributed environment
  as a contributor and an integrator. That is, you’ll learn how to contribute
  code successfully to a project and make it as easy on you and the project
  maintainer as possible, and also how to maintain a project successfully with
  a number of developers contributing.
</p>

<h2>Distributed Workflows</h2>

<p>
  Unlike Centralized Version Control Systems (CVCSs), the distributed nature of
  Git allows you to be far more flexible in how developers collaborate on
  projects. In centralized systems, every developer is a node working more or
  less equally on a central hub. In Git, however, every developer is
  potentially both a node and a hub — that is, every developer can both
  contribute code to other repositories and maintain a public repository on
  which others can base their work and which they can contribute to. This opens
  a vast range of workflow possibilities for your project and/or your team, so
  I’ll cover a few common paradigms that take advantage of this flexibility.
  I’ll go over the strengths and possible weaknesses of each design; you can
  choose a single one to use, or you can mix and match features from each.
</p>

<h3>Centralized Workflow</h3>

<p>
  In centralized systems, there is generally a single collaboration model—the
  centralized workflow. One central hub, or repository, can accept code, and
  everyone synchronizes their work to it. A number of developers are nodes —
  consumers of that hub — and synchronize to that one place (see Figure 5-1).
</p>

<p>Insert 18333fig0501.png Figure 5-1. Centralized workflow.</p>

<p>
  This means that if two developers clone from the hub and both make changes,
  the first developer to push their changes back up can do so with no problems.
  The second developer must merge in the first one’s work before pushing
  changes up, so as not to overwrite the first developer’s changes. This
  concept is true in Git as it is in Subversion (or any CVCS), and this model
  works perfectly in Git.
</p>

<p>
  If you have a small team or are already comfortable with a centralized
  workflow in your company or team, you can easily continue using that workflow
  with Git. Simply set up a single repository, and give everyone on your team
  push access; Git won’t let users overwrite each other. If one developer
  clones, makes changes, and then tries to push their changes while another
  developer has pushed in the meantime, the server will reject that developer’s
  changes. They will be told that they’re trying to push non-fast-forward
  changes and that they won’t be able to do so until they fetch and merge. This
  workflow is attractive to a lot of people because it’s a paradigm that many
  are familiar and comfortable with.
</p>

<h3>Integration-Manager Workflow</h3>

<p>
  Because Git allows you to have multiple remote repositories, it’s possible to
  have a workflow where each developer has write access to their own public
  repository and read access to everyone else’s. This scenario often includes a
  canonical repository that represents the "official" project. To contribute to
  that project, you create your own public clone of the project and push your
  changes to it. Then, you can send a request to the maintainer of the main
  project to pull in your changes. They can add your repository as a remote,
  test your changes locally, merge them into their branch, and push back to
  their repository. The process works as follows (see Figure 5-2):
</p>

<ol>
  <li>
    <p>The project maintainer pushes to their public repository.</p>
  </li>

  <li>
    <p>A contributor clones that repository and makes changes.</p>
  </li>

  <li>
    <p>The contributor pushes to their own public copy.</p>
  </li>

  <li>
    <p>
      The contributor sends the maintainer an e-mail asking them to pull
      changes.
    </p>
  </li>

  <li>
    <p>
      The maintainer adds the contributor’s repo as a remote and merges
      locally.
    </p>
  </li>

  <li>
    <p>The maintainer pushes merged changes to the main repository.</p>
  </li>
</ol>

<p>Insert 18333fig0502.png Figure 5-2. Integration-manager workflow.</p>

<p>
  This is a very common workflow with sites like GitHub, where it’s easy to
  fork a project and push your changes into your fork for everyone to see. One
  of the main advantages of this approach is that you can continue to work, and
  the maintainer of the main repository can pull in your changes at any time.
  Contributors don’t have to wait for the project to incorporate their changes
  — each party can work at their own pace.
</p>

<h3>Dictator and Lieutenants Workflow</h3>

<p>
  This is a variant of a multiple-repository workflow. It’s generally used by
  huge projects with hundreds of collaborators; one famous example is the Linux
  kernel. Various integration managers are in charge of certain parts of the
  repository; they’re called lieutenants. All the lieutenants have one
  integration manager known as the benevolent dictator. The benevolent
  dictator’s repository serves as the reference repository from which all the
  collaborators need to pull. The process works like this (see Figure 5-3):
</p>

<ol>
  <li>
    <p>
      Regular developers work on their topic branch and rebase their work on
      top of master. The master branch is that of the dictator.
    </p>
  </li>

  <li>
    <p>
      Lieutenants merge the developers’ topic branches into their master
      branch.
    </p>
  </li>

  <li>
    <p>
      The dictator merges the lieutenants’ master branches into the dictator’s
      master branch.
    </p>
  </li>

  <li>
    <p>
      The dictator pushes their master to the reference repository so the other
      developers can rebase on it.
    </p>
  </li>
</ol>

<p>Insert 18333fig0503.png Figure 5-3. Benevolent dictator workflow.</p>

<p>
  This kind of workflow isn’t common but can be useful in very big projects or
  in highly hierarchical environments, as it allows the project leader (the
  dictator) to delegate much of the work and collect large subsets of code at
  multiple points before integrating them.
</p>

<p>
  These are some commonly used workflows that are possible with a distributed
  system like Git, but you can see that many variations are possible to suit
  your particular real-world workflow. Now that you can (I hope) determine
  which workflow combination may work for you, I’ll cover some more specific
  examples of how to accomplish the main roles that make up the different
  flows.
</p>

<h2>Contributing to a Project</h2>

<p>
  You know what the different workflows are, and you should have a pretty good
  grasp of fundamental Git usage. In this section, you’ll learn about a few
  common patterns for contributing to a project.
</p>

<p>
  The main difficulty with describing this process is that there are a huge
  number of variations on how it’s done. Because Git is very flexible, people
  can and do work together many ways, and it’s problematic to describe how you
  should contribute to a project — every project is a bit different. Some of
  the variables involved are active contributor size, chosen workflow, your
  commit access, and possibly the external contribution method.
</p>

<p>
  The first variable is active contributor size. How many users are actively
  contributing code to this project, and how often? In many instances, you’ll
  have two or three developers with a few commits a day, or possibly less for
  somewhat dormant projects. For really large companies or projects, the number
  of developers could be in the thousands, with dozens or even hundreds of
  patches coming in each day. This is important because with more and more
  developers, you run into more issues with making sure your code applies
  cleanly or can be easily merged. Changes you submit may be rendered obsolete
  or severely broken by work that is merged in while you were working or while
  your changes were waiting to be approved or applied. How can you keep your
  code consistently up to date and your patches valid?
</p>

<p>
  The next variable is the workflow in use for the project. Is it centralized,
  with each developer having equal write access to the main codeline? Does the
  project have a maintainer or integration manager who checks all the patches?
  Are all the patches peer-reviewed and approved? Are you involved in that
  process? Is a lieutenant system in place, and do you have to submit your work
  to them first?
</p>

<p>
  The next issue is your commit access. The workflow required in order to
  contribute to a project is much different if you have write access to the
  project than if you don’t. If you don’t have write access, how does the
  project prefer to accept contributed work? Does it even have a policy? How
  much work are you contributing at a time? How often do you contribute?
</p>

<p>
  All these questions can affect how you contribute effectively to a project
  and what workflows are preferred or available to you. I’ll cover aspects of
  each of these in a series of use cases, moving from simple to more complex;
  you should be able to construct the specific workflows you need in practice
  from these examples.
</p>

<h3>Commit Guidelines</h3>

<p>
  Before you start looking at the specific use cases, here’s a quick note about
  commit messages. Having a good guideline for creating commits and sticking to
  it makes working with Git and collaborating with others a lot easier. The Git
  project provides a document that lays out a number of good tips for creating
  commits from which to submit patches — you can read it in the Git source code
  in the <code>Documentation/SubmittingPatches</code> file.
</p>

<p>
  First, you don’t want to submit any whitespace errors. Git provides an easy
  way to check for this — before you commit, run <code>git diff --check</code>, which identifies possible whitespace errors and lists them for you. Here is
  an example, where I’ve replaced a red terminal color with <code>X</code>s:
</p>

<pre>
$ git diff --check
lib/simplegit.rb:5: trailing whitespace.
+    @git_dir = File.expand_path(git_dir)XX
lib/simplegit.rb:7: trailing whitespace.
+ XXXXXXXXXXX
lib/simplegit.rb:26: trailing whitespace.
+    def command(git_cmd)XXXX
</pre>

<p>
  If you run that command before committing, you can tell if you’re about to
  commit whitespace issues that may annoy other developers.
</p>

<p>
  Next, try to make each commit a logically separate changeset. If you can, try
  to make your changes digestible — don’t code for a whole weekend on five
  different issues and then submit them all as one massive commit on Monday.
  Even if you don’t commit during the weekend, use the staging area on Monday
  to split your work into at least one commit per issue, with a useful message
  per commit. If some of the changes modify the same file, try to use <code>git add --patch</code> to partially stage files (covered in detail in Chapter 6). The project
  snapshot at the tip of the branch is identical whether you do one commit or
  five, as long as all the changes are added at some point, so try to make
  things easier on your fellow developers when they have to review your
  changes. This approach also makes it easier to pull out or revert one of the
  changesets if you need to later. Chapter 6 describes a number of useful Git
  tricks for rewriting history and interactively staging files — use these
  tools to help craft a clean and understandable history.
</p>

<p>
  The last thing to keep in mind is the commit message. Getting in the habit of
  creating quality commit messages makes using and collaborating with Git a lot
  easier. As a general rule, your messages should start with a single line
  that’s no more than about 50 characters and that describes the changeset
  concisely, followed by a blank line, followed by a more detailed explanation.
  The Git project requires that the more detailed explanation include your
  motivation for the change and contrast its implementation with previous
  behavior — this is a good guideline to follow. It’s also a good idea to use
  the imperative present tense in these messages. In other words, use commands.
  Instead of "I added tests for" or "Adding tests for," use "Add tests for."
  Here is a template originally written by Tim Pope at tpope.net:
</p>

<pre>
Short (50 chars or less) summary of changes

More detailed explanatory text, if necessary.  Wrap it to about 72
characters or so.  In some contexts, the first line is treated as the
subject of an email and the rest of the text as the body.  The blank
line separating the summary from the body is critical (unless you omit
the body entirely); tools like rebase can get confused if you run the
two together.

Further paragraphs come after blank lines.

 - Bullet points are okay, too

 - Typically a hyphen or asterisk is used for the bullet, preceded by a
   single space, with blank lines in between, but conventions vary here
</pre>

<p>
  If all your commit messages look like this, things will be a lot easier for
  you and the developers you work with. The Git project has well-formatted
  commit messages — I encourage you to run <code>git log --no-merges</code> there to see what a nicely formatted project-commit history looks like.
</p>

<p>
  In the following examples, and throughout most of this book, for the sake of
  brevity I don’t format messages nicely like this; instead, I use the <code>-m</code> option to <code>git commit</code>. Do as I say, not as I do.
</p>

<h3>Private Small Team</h3>

<p>
  The simplest setup you’re likely to encounter is a private project with one
  or two other developers. By private, I mean closed source — not
  read-accessible to the outside world. You and the other developers all have
  push access to the repository.
</p>

<p>
  In this environment, you can follow a workflow similar to what you might do
  when using Subversion or another centralized system. You still get the
  advantages of things like offline committing and vastly simpler branching and
  merging, but the workflow can be very similar; the main difference is that
  merges happen client-side rather than on the server at commit time. Let’s see
  what it might look like when two developers start to work together with a
  shared repository. The first developer, John, clones the repository, makes a
  change, and commits locally. (I’m replacing the protocol messages with <code>...</code> in these examples to shorten them somewhat.)
</p>

<pre>
# John's Machine
$ git clone john@githost:simplegit.git
Initialized empty Git repository in /home/john/simplegit/.git/
...
$ cd simplegit/
$ vim lib/simplegit.rb
$ git commit -am 'removed invalid default value'
[master 738ee87] removed invalid default value
 1 files changed, 1 insertions(+), 1 deletions(-)
</pre>

<p>
  The second developer, Jessica, does the same thing — clones the repository
  and commits a change:
</p>

<pre>
# Jessica's Machine
$ git clone jessica@githost:simplegit.git
Initialized empty Git repository in /home/jessica/simplegit/.git/
...
$ cd simplegit/
$ vim TODO
$ git commit -am 'add reset task'
[master fbff5bc] add reset task
 1 files changed, 1 insertions(+), 0 deletions(-)
</pre>

<p>Now, Jessica pushes her work up to the server:</p>

<pre>
# Jessica's Machine
$ git push origin master
...
To jessica@githost:simplegit.git
   1edee6b..fbff5bc  master -&gt; master
</pre>

<p>John tries to push his change up, too:</p>

<pre>
# John's Machine
$ git push origin master
To john@githost:simplegit.git
 ! [rejected]        master -&gt; master (non-fast forward)
error: failed to push some refs to 'john@githost:simplegit.git'
</pre>

<p>
  John isn’t allowed to push because Jessica has pushed in the meantime. This
  is especially important to understand if you’re used to Subversion, because
  you’ll notice that the two developers didn’t edit the same file. Although
  Subversion automatically does such a merge on the server if different files
  are edited, in Git you must merge the commits locally. John has to fetch
  Jessica’s changes and merge them in before he will be allowed to push:
</p>

<pre>
$ git fetch origin
...
From john@githost:simplegit
 + 049d078...fbff5bc master     -&gt; origin/master
</pre>

<p>At this point, John’s local repository looks something like Figure 5-4.</p>

<p>Insert 18333fig0504.png Figure 5-4. John’s initial repository.</p>

<p>
  John has a reference to the changes Jessica pushed up, but he has to merge
  them into his own work before he is allowed to push:
</p>

<pre>
$ git merge origin/master
Merge made by recursive.
 TODO |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
</pre>

<p>
  The merge goes smoothly — John’s commit history now looks like Figure 5-5.
</p>

<p>
  Insert 18333fig0505.png Figure 5-5. John’s repository after merging <code>origin/master</code>.
</p>

<p>
  Now, John can test his code to make sure it still works properly, and then he
  can push his new merged work up to the server:
</p>

<pre>
$ git push origin master
...
To john@githost:simplegit.git
   fbff5bc..72bbc59  master -&gt; master
</pre>

<p>Finally, John’s commit history looks like Figure 5-6.</p>

<p>
  Insert 18333fig0506.png Figure 5-6. John’s history after pushing to the
  origin server.
</p>

<p>
  In the meantime, Jessica has been working on a topic branch. She’s created a
  topic branch called <code>issue54</code> and done three commits on that branch. She hasn’t fetched John’s changes
  yet, so her commit history looks like Figure 5-7.
</p>

<p>Insert 18333fig0507.png Figure 5-7. Jessica’s initial commit history.</p>

<p>Jessica wants to sync up with John, so she fetches:</p>

<pre>
# Jessica's Machine
$ git fetch origin
...
From jessica@githost:simplegit
   fbff5bc..72bbc59  master     -&gt; origin/master
</pre>

<p>
  That pulls down the work John has pushed up in the meantime. Jessica’s
  history now looks like Figure 5-8.
</p>

<p>
  Insert 18333fig0508.png Figure 5-8. Jessica’s history after fetching John’s
  changes.
</p>

<p>
  Jessica thinks her topic branch is ready, but she wants to know what she has
  to merge her work into so that she can push. She runs <code>git log</code> to find out:
</p>

<pre>
$ git log --no-merges origin/master ^issue54
commit 738ee872852dfaa9d6634e0dea7a324040193016
Author: John Smith &lt;jsmith@example.com&gt;
Date:   Fri May 29 16:01:27 2009 -0700

    removed invalid default value
</pre>

<p>
  Now, Jessica can merge her topic work into her <code>master</code> branch, merge John’s work (<code>origin/master</code>) into her <code>master</code> branch, and then push back to the server again. First, she switches back to
  her <code>master</code> branch to integrate all this work:
</p>

<pre>
$ git checkout master
Switched to branch "master"
Your branch is behind 'origin/master' by 2 commits, and can be fast-forwarded.
</pre>

<p>
  She can merge either <code>origin/master</code> or <code>issue54</code> first — they’re both upstream, so the order doesn’t matter. The end snapshot
  should be identical no matter which order she chooses; only the history will
  be slightly different. She chooses to merge in <code>issue54</code> first:
</p>

<pre>
$ git merge issue54
Updating fbff5bc..4af4298
Fast forward
 README           |    1 +
 lib/simplegit.rb |    6 +++++-
 2 files changed, 6 insertions(+), 1 deletions(-)
</pre>

<p>
  No problems occur; as you can see, it was a simple fast-forward. Now Jessica
  merges in John’s work (<code>origin/master</code>):
</p>

<pre>
$ git merge origin/master
Auto-merging lib/simplegit.rb
Merge made by recursive.
 lib/simplegit.rb |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
</pre>

<p>Everything merges cleanly, and Jessica’s history looks like Figure 5-9.</p>

<p>
  Insert 18333fig0509.png Figure 5-9. Jessica’s history after merging John’s
  changes.
</p>

<p>
  Now <code>origin/master</code> is reachable from Jessica’s <code>master</code> branch, so she should be able to successfully push (assuming John hasn’t
  pushed again in the meantime):
</p>

<pre>
$ git push origin master
...
To jessica@githost:simplegit.git
   72bbc59..8059c15  master -&gt; master
</pre>

<p>
  Each developer has committed a few times and merged each other’s work
  successfully; see Figure 5-10.
</p>

<p>
  Insert 18333fig0510.png Figure 5-10. Jessica’s history after pushing all
  changes back to the server.
</p>

<p>
  That is one of the simplest workflows. You work for a while, generally in a
  topic branch, and merge into your <code>master</code> branch when it’s ready to be integrated. When you want to share that work,
  you merge it into your own <code>master</code> branch, then fetch and merge <code>origin/master</code> if it has changed, and finally push to the <code>master</code> branch on the server. The general sequence is something like that shown in
  Figure 5-11.
</p>

<p>
  Insert 18333fig0511.png Figure 5-11. General sequence of events for a simple
  multiple-developer Git workflow.
</p>

<h3>Private Managed Team</h3>

<p>
  In this next scenario, you’ll look at contributor roles in a larger private
  group. You’ll learn how to work in an environment where small groups
  collaborate on features and then those team-based contributions are
  integrated by another party.
</p>

<p>
  Let’s say that John and Jessica are working together on one feature, while
  Jessica and Josie are working on a second. In this case, the company is using
  a type of integration-manager workflow where the work of the individual
  groups is integrated only by certain engineers, and the <code>master</code> branch of the main repo can be updated only by those engineers. In this
  scenario, all work is done in team-based branches and pulled together by the
  integrators later.
</p>

<p>
  Let’s follow Jessica’s workflow as she works on her two features,
  collaborating in parallel with two different developers in this environment.
  Assuming she already has her repository cloned, she decides to work on <code>featureA</code> first. She creates a new branch for the feature and does some work on it
  there:
</p>

<pre>
# Jessica's Machine
$ git checkout -b featureA
Switched to a new branch "featureA"
$ vim lib/simplegit.rb
$ git commit -am 'add limit to log function'
[featureA 3300904] add limit to log function
 1 files changed, 1 insertions(+), 1 deletions(-)
</pre>

<p>
  At this point, she needs to share her work with John, so she pushes her <code>featureA</code> branch commits up to the server. Jessica doesn’t have push access to the <code>master</code> branch — only the integrators do — so she has to push to another branch in
  order to collaborate with John:
</p>

<pre>
$ git push origin featureA
...
To jessica@githost:simplegit.git
 * [new branch]      featureA -&gt; featureA
</pre>

<p>
  Jessica e-mails John to tell him that she’s pushed some work into a branch
  named <code>featureA</code> and he can look at it now. While she waits for feedback from John, Jessica
  decides to start working on <code>featureB</code> with Josie. To begin, she starts a new feature branch, basing it off the
  server’s <code>master</code> branch:
</p>

<pre>
# Jessica's Machine
$ git fetch origin
$ git checkout -b featureB origin/master
Switched to a new branch "featureB"
</pre>

<p>
  Now, Jessica makes a couple of commits on the <code>featureB</code> branch:
</p>

<pre>
$ vim lib/simplegit.rb
$ git commit -am 'made the ls-tree function recursive'
[featureB e5b0fdc] made the ls-tree function recursive
 1 files changed, 1 insertions(+), 1 deletions(-)
$ vim lib/simplegit.rb
$ git commit -am 'add ls-files'
[featureB 8512791] add ls-files
 1 files changed, 5 insertions(+), 0 deletions(-)
</pre>

<p>Jessica’s repository looks like Figure 5-12.</p>

<p>Insert 18333fig0512.png Figure 5-12. Jessica’s initial commit history.</p>

<p>
  She’s ready to push up her work, but gets an e-mail from Josie that a branch
  with some initial work on it was already pushed to the server as <code>featureBee</code>. Jessica first needs to merge those changes in with her own before she can
  push to the server. She can then fetch Josie’s changes down with <code>git fetch</code>:
</p>

<pre>
$ git fetch origin
...
From jessica@githost:simplegit
 * [new branch]      featureBee -&gt; origin/featureBee
</pre>

<p>
  Jessica can now merge this into the work she did with <code>git merge</code>:
</p>

<pre>
$ git merge origin/featureBee
Auto-merging lib/simplegit.rb
Merge made by recursive.
 lib/simplegit.rb |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
</pre>

<p>
  There is a bit of a problem — she needs to push the merged work in her <code>featureB</code> branch to the <code>featureBee</code> branch on the server. She can do so by specifying the local branch followed
  by a colon (:) followed by the remote branch to the <code>git push</code> command:
</p>

<pre>
$ git push origin featureB:featureBee
...
To jessica@githost:simplegit.git
   fba9af8..cd685d1  featureB -&gt; featureBee
</pre>

<p>
  This is called a <i>refspec</i>. See Chapter 9 for a more detailed discussion of Git refspecs and different
  things you can do with them.
</p>

<p>
  Next, John e-mails Jessica to say he’s pushed some changes to the <code>featureA</code> branch and ask her to verify them. She runs a <code>git fetch</code> to pull down those changes:
</p>

<pre>
$ git fetch origin
...
From jessica@githost:simplegit
   3300904..aad881d  featureA   -&gt; origin/featureA
</pre>

<p>Then, she can see what has been changed with <code>git log</code>:</p>

<pre>
$ git log origin/featureA ^featureA
commit aad881d154acdaeb2b6b18ea0e827ed8a6d671e6
Author: John Smith &lt;jsmith@example.com&gt;
Date:   Fri May 29 19:57:33 2009 -0700

    changed log output to 30 from 25
</pre>

<p>
  Finally, she merges John’s work into her own <code>featureA</code> branch:
</p>

<pre>
$ git checkout featureA
Switched to branch "featureA"
$ git merge origin/featureA
Updating 3300904..aad881d
Fast forward
 lib/simplegit.rb |   10 +++++++++-
1 files changed, 9 insertions(+), 1 deletions(-)
</pre>

<p>
  Jessica wants to tweak something, so she commits again and then pushes this
  back up to the server:
</p>

<pre>
$ git commit -am 'small tweak'
[featureA 774b3ed] small tweak
 1 files changed, 1 insertions(+), 1 deletions(-)
$ git push origin featureA
...
To jessica@githost:simplegit.git
   3300904..774b3ed  featureA -&gt; featureA
</pre>

<p>Jessica’s commit history now looks something like Figure 5-13.</p>

<p>
  Insert 18333fig0513.png Figure 5-13. Jessica’s history after committing on a
  feature branch.
</p>

<p>
  Jessica, Josie, and John inform the integrators that the <code>featureA</code> and <code>featureBee</code> branches on the server are ready for integration into the mainline. After
  they integrate these branches into the mainline, a fetch will bring down the
  new merge commits, making the commit history look like Figure 5-14.
</p>

<p>
  Insert 18333fig0514.png Figure 5-14. Jessica’s history after merging both her
  topic branches.
</p>

<p>
  Many groups switch to Git because of this ability to have multiple teams
  working in parallel, merging the different lines of work late in the process.
  The ability of smaller subgroups of a team to collaborate via remote branches
  without necessarily having to involve or impede the entire team is a huge
  benefit of Git. The sequence for the workflow you saw here is something like
  Figure 5-15.
</p>

<p>
  Insert 18333fig0515.png Figure 5-15. Basic sequence of this managed-team
  workflow.
</p>

<h3>Public Small Project</h3>

<p>
  Contributing to public projects is a bit different. Because you don’t have
  the permissions to directly update branches on the project, you have to get
  the work to the maintainers some other way. This first example describes
  contributing via forking on Git hosts that support easy forking. The
  repo.or.cz and GitHub hosting sites both support this, and many project
  maintainers expect this style of contribution. The next section deals with
  projects that prefer to accept contributed patches via e-mail.
</p>

<p>
  First, you’ll probably want to clone the main repository, create a topic
  branch for the patch or patch series you’re planning to contribute, and do
  your work there. The sequence looks basically like this:
</p>

<pre>
$ git clone (url)
$ cd project
$ git checkout -b featureA
$ (work)
$ git commit
$ (work)
$ git commit
</pre>

<p>
  You may want to use <code>rebase -i</code> to squash your work down to a single commit, or rearrange the work in the
  commits to make the patch easier for the maintainer to review — see Chapter 6
  for more information about interactive rebasing.
</p>

<p>
  When your branch work is finished and you’re ready to contribute it back to
  the maintainers, go to the original project page and click the "Fork" button,
  creating your own writable fork of the project. You then need to add in this
  new repository URL as a second remote, in this case named <code>myfork</code>:
</p>

<pre>
$ git remote add myfork (url)
</pre>

<p>
  You need to push your work up to it. It’s easiest to push the remote branch
  you’re working on up to your repository, rather than merging into your master
  branch and pushing that up. The reason is that if the work isn’t accepted or
  is cherry picked, you don’t have to rewind your master branch. If the
  maintainers merge, rebase, or cherry-pick your work, you’ll eventually get it
  back via pulling from their repository anyhow:
</p>

<pre>
$ git push myfork featureA
</pre>

<p>
  When your work has been pushed up to your fork, you need to notify the
  maintainer. This is often called a pull request, and you can either generate
  it via the website — GitHub has a "pull request" button that automatically
  messages the maintainer — or run the <code>git request-pull</code> command and e-mail the output to the project maintainer manually.
</p>

<p>
  The <code>request-pull</code> command takes the base branch into which you want your topic branch pulled
  and the Git repository URL you want them to pull from, and outputs a summary
  of all the changes you’re asking to be pulled in. For instance, if Jessica
  wants to send John a pull request, and she’s done two commits on the topic
  branch she just pushed up, she can run this:
</p>

<pre>
$ git request-pull origin/master myfork
The following changes since commit 1edee6b1d61823a2de3b09c160d7080b8d1b3a40:
  John Smith (1):
        added a new function

are available in the git repository at:

  git://githost/simplegit.git featureA

Jessica Smith (2):
      add limit to log function
      change log output to 30 from 25

 lib/simplegit.rb |   10 +++++++++-
 1 files changed, 9 insertions(+), 1 deletions(-)
</pre>

<p>
  The output can be sent to the maintainer—it tells them where the work was
  branched from, summarizes the commits, and tells where to pull this work
  from.
</p>

<p>
  On a project for which you’re not the maintainer, it’s generally easier to
  have a branch like <code>master</code> always track <code>origin/master</code> and to do your work in topic branches that you can easily discard if they’re
  rejected.&emsp14;&emsp14;Having work themes isolated into topic branches also
  makes it easier for you to rebase your work if the tip of the main repository
  has moved in the meantime and your commits no longer apply cleanly. For
  example, if you want to submit a second topic of work to the project, don’t
  continue working on the topic branch you just pushed up — start over from the
  main repository’s <code>master</code> branch:
</p>

<pre>
$ git checkout -b featureB origin/master
$ (work)
$ git commit
$ git push myfork featureB
$ (email maintainer)
$ git fetch origin
</pre>

<p>
  Now, each of your topics is contained within a silo — similar to a patch
  queue — that you can rewrite, rebase, and modify without the topics
  interfering or interdepending on each other as in Figure 5-16.
</p>

<p>
  Insert 18333fig0516.png Figure 5-16. Initial commit history with featureB
  work.
</p>

<p>
  Let’s say the project maintainer has pulled in a bunch of other patches and
  tried your first branch, but it no longer cleanly merges. In this case, you
  can try to rebase that branch on top of <code>origin/master</code>, resolve the conflicts for the maintainer, and then resubmit your changes:
</p>

<pre>
$ git checkout featureA
$ git rebase origin/master
$ git push -f myfork featureA
</pre>

<p>This rewrites your history to now look like Figure 5-17.</p>

<p>Insert 18333fig0517.png Figure 5-17. Commit history after featureA work.</p>

<p>
  Because you rebased the branch, you have to specify the <code>-f</code> to your push command in order to be able to replace the <code>featureA</code> branch on the server with a commit that isn’t a descendant of it. An
  alternative would be to push this new work to a different branch on the
  server (perhaps called <code>featureAv2</code>).
</p>

<p>
  Let’s look at one more possible scenario: the maintainer has looked at work
  in your second branch and likes the concept but would like you to change an
  implementation detail. You’ll also take this opportunity to move the work to
  be based off the project’s current <code>master</code> branch. You start a new branch based off the current <code>origin/master</code> branch, squash the <code>featureB</code> changes there, resolve any conflicts, make the implementation change, and
  then push that up as a new branch:
</p>

<pre>
$ git checkout -b featureBv2 origin/master
$ git merge --no-commit --squash featureB
$ (change implementation)
$ git commit
$ git push myfork featureBv2
</pre>

<p>
  The <code>--squash</code> option takes all the work on the merged branch and squashes it into one
  non-merge commit on top of the branch you’re on. The <code>--no-commit</code> option tells Git not to automatically record a commit. This allows you to
  introduce all the changes from another branch and then make more changes
  before recording the new commit.
</p>

<p>
  Now you can send the maintainer a message that you’ve made the requested
  changes and they can find those changes in your <code>featureBv2</code> branch (see Figure 5-18).
</p>

<p>
  Insert 18333fig0518.png Figure 5-18. Commit history after featureBv2 work.
</p>

<h3>Public Large Project</h3>

<p>
  Many larger projects have established procedures for accepting patches —
  you’ll need to check the specific rules for each project, because they will
  differ. However, many larger public projects accept patches via a developer
  mailing list, so I’ll go over an example of that now.
</p>

<p>
  The workflow is similar to the previous use case — you create topic branches
  for each patch series you work on. The difference is how you submit them to
  the project. Instead of forking the project and pushing to your own writable
  version, you generate e-mail versions of each commit series and e-mail them
  to the developer mailing list:
</p>

<pre>
$ git checkout -b topicA
$ (work)
$ git commit
$ (work)
$ git commit
</pre>

<p>
  Now you have two commits that you want to send to the mailing list. You use <code>git format-patch</code> to generate the mbox-formatted files that you can e-mail to the list — it
  turns each commit into an e-mail message with the first line of the commit
  message as the subject and the rest of the message plus the patch that the
  commit introduces as the body. The nice thing about this is that applying a
  patch from an e-mail generated with <code>format-patch</code> preserves all the commit information properly, as you’ll see more of in the
  next section when you apply these patches:
</p>

<pre>
$ git format-patch -M origin/master
0001-add-limit-to-log-function.patch
0002-changed-log-output-to-30-from-25.patch
</pre>

<p>
  The <code>format-patch</code> command prints out the names of the patch files it creates. The <code>-M</code> switch tells Git to look for renames. The files end up looking like this:
</p>

<pre>
$ cat 0001-add-limit-to-log-function.patch
From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001
From: Jessica Smith &lt;jessica@example.com&gt;
Date: Sun, 6 Apr 2008 10:17:23 -0700
Subject: [PATCH 1/2] add limit to log function

Limit log functionality to the first 20

---
 lib/simplegit.rb |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index 76f47bc..f9815f1 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -14,7 +14,7 @@ class SimpleGit
   end

   def log(treeish = 'master')
-    command("git log #{treeish}")
+    command("git log -n 20 #{treeish}")
   end

   def ls_tree(treeish = 'master')
--
1.6.2.rc1.20.g8c5b.dirty
</pre>

<p>
  You can also edit these patch files to add more information for the e-mail
  list that you don’t want to show up in the commit message. If you add text
  between the <code>---</code> line and the beginning of the patch (the <code>lib/simplegit.rb</code> line), then developers can read it; but applying the patch excludes it.
</p>

<p>
  To e-mail this to a mailing list, you can either paste the file into your
  e-mail program or send it via a command-line program. Pasting the text often
  causes formatting issues, especially with "smarter" clients that don’t
  preserve newlines and other whitespace appropriately. Luckily, Git provides a
  tool to help you send properly formatted patches via IMAP, which may be
  easier for you. I’ll demonstrate how to send a patch via Gmail, which happens
  to be the e-mail agent I use; you can read detailed instructions for a number
  of mail programs at the end of the aforementioned <code>Documentation/SubmittingPatches</code> file in the Git source code.
</p>

<p>
  First, you need to set up the imap section in your <code>~/.gitconfig</code> file. You can set each value separately with a series of <code>git config</code> commands, or you can add them manually; but in the end, your config file
  should look something like this:
</p>

<pre>
[imap]
  folder = "[Gmail]/Drafts"
  host = imaps://imap.gmail.com
  user = user@gmail.com
  pass = p4ssw0rd
  port = 993
  sslverify = false
</pre>

<p>
  If your IMAP server doesn’t use SSL, the last two lines probably aren’t
  necessary, and the host value will be <code>imap://</code> instead of <code>imaps://</code>. When that is set up, you can use <code>git imap-send</code> to place the patch series in the Drafts folder of the specified IMAP server:
</p>

<pre>
$ cat *.patch |git imap-send
Resolving imap.gmail.com... ok
Connecting to [74.125.142.109]:993... ok
Logging in...
sending 2 messages
100% (2/2) done
</pre>

<p>
  At this point, you should be able to go to your Drafts folder, change the To
  field to the mailing list you’re sending the patch to, possibly CC the
  maintainer or person responsible for that section, and send it off.
</p>

<p>
  You can also send the patches through an SMTP server. As before, you can set
  each value separately with a series of <code>git config</code> commands, or you can add them manually in the sendemail section in your <code>~/.gitconfig</code> file:
</p>

<pre>
[sendemail]
  smtpencryption = tls
  smtpserver = smtp.gmail.com
  smtpuser = user@gmail.com
  smtpserverport = 587
</pre>

<p>
  After this is done, you can use <code>git send-email</code> to send your patches:
</p>

<pre>
$ git send-email *.patch
0001-added-limit-to-log-function.patch
0002-changed-log-output-to-30-from-25.patch
Who should the emails appear to be from? [Jessica Smith &lt;jessica@example.com&gt;]
Emails will be sent from: Jessica Smith &lt;jessica@example.com&gt;
Who should the emails be sent to? jessica@example.com
Message-ID to be used as In-Reply-To for the first email? y
</pre>

<p>
  Then, Git spits out a bunch of log information looking something like this
  for each patch you’re sending:
</p>

<pre>
(mbox) Adding cc: Jessica Smith &lt;jessica@example.com&gt; from
  \line 'From: Jessica Smith &lt;jessica@example.com&gt;'
OK. Log says:
Sendmail: /usr/sbin/sendmail -i jessica@example.com
From: Jessica Smith &lt;jessica@example.com&gt;
To: jessica@example.com
Subject: [PATCH 1/2] added limit to log function
Date: Sat, 30 May 2009 13:29:15 -0700
Message-Id: &lt;1243715356-61726-1-git-send-email-jessica@example.com&gt;
X-Mailer: git-send-email 1.6.2.rc1.20.g8c5b.dirty
In-Reply-To: &lt;y&gt;
References: &lt;y&gt;

Result: OK
</pre>

<h3>Summary</h3>

<p>
  This section has covered a number of common workflows for dealing with
  several very different types of Git projects you’re likely to encounter and
  introduced a couple of new tools to help you manage this process. Next,
  you’ll see how to work the other side of the coin: maintaining a Git project.
  You’ll learn how to be a benevolent dictator or integration manager.
</p>

<h2>Maintaining a Project</h2>

<p>
  In addition to knowing how to effectively contribute to a project, you’ll
  likely need to know how to maintain one. This can consist of accepting and
  applying patches generated via <code>format-patch</code> and e-mailed to you, or integrating changes in remote branches for
  repositories you’ve added as remotes to your project. Whether you maintain a
  canonical repository or want to help by verifying or approving patches, you
  need to know how to accept work in a way that is clearest for other
  contributors and sustainable by you over the long run.
</p>

<h3>Working in Topic Branches</h3>

<p>
  When you’re thinking of integrating new work, it’s generally a good idea to
  try it out in a topic branch — a temporary branch specifically made to try
  out that new work. This way, it’s easy to tweak a patch individually and
  leave it if it’s not working until you have time to come back to it. If you
  create a simple branch name based on the theme of the work you’re going to
  try, such as <code>ruby_client</code> or something similarly descriptive, you can easily remember it if you have
  to abandon it for a while and come back later. The maintainer of the Git
  project tends to namespace these branches as well — such as <code>sc/ruby_client</code>, where <code>sc</code> is short for the person who contributed the work. As you’ll remember, you
  can create the branch based off your master branch like this:
</p>

<pre>
$ git branch sc/ruby_client master
</pre>

<p>
  Or, if you want to also switch to it immediately, you can use the <code>checkout -b</code> command:
</p>

<pre>
$ git checkout -b sc/ruby_client master
</pre>

<p>
  Now you’re ready to add your contributed work into this topic branch and
  determine if you want to merge it into your longer-term branches.
</p>

<h3>Applying Patches from E-mail</h3>

<p>
  If you receive a patch over e-mail that you need to integrate into your
  project, you need to apply the patch in your topic branch to evaluate it.
  There are two ways to apply an e-mailed patch: with <code>git apply</code> or with <code>git am</code>.
</p>

<p>Applying a Patch with apply</p>

<p>
  If you received the patch from someone who generated it with the <code>git diff</code> or a Unix <code>diff</code> command, you can apply it with the <code>git apply</code> command. Assuming you saved the patch at <code>/tmp/patch-ruby-client.patch</code>, you can apply the patch like this:
</p>

<pre>
$ git apply /tmp/patch-ruby-client.patch
</pre>

<p>
  This modifies the files in your working directory. It’s almost identical to
  running a <code>patch -p1</code> command to apply the patch, although it’s more paranoid and accepts fewer
  fuzzy matches than patch. It also handles file adds, deletes, and renames if
  they’re described in the <code>git diff</code> format, which <code>patch</code> won’t do. Finally, <code>git apply</code> is an "apply all or abort all" model where either everything is applied or
  nothing is, whereas <code>patch</code> can partially apply patchfiles, leaving your working directory in a weird
  state. <code>git apply</code> is overall much more paranoid than <code>patch</code>. It won’t create a commit for you — after running it, you must stage and
  commit the changes introduced manually.
</p>

<p>
  You can also use <code>git apply</code> to see if a patch applies cleanly before you try actually applying it — you
  can run <code>git apply --check</code> with the patch:
</p>

<pre>
$ git apply --check 0001-seeing-if-this-helps-the-gem.patch
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
</pre>

<p>
  If there is no output, then the patch should apply cleanly. This command also
  exits with a non-zero status if the check fails, so you can use it in scripts
  if you want.
</p>

<p>Applying a Patch with am</p>

<p>
  If the contributor is a Git user and was good enough to use the <code>format-patch</code> command to generate their patch, then your job is easier because the patch
  contains author information and a commit message for you. If you can,
  encourage your contributors to use <code>format-patch</code> instead of <code>diff</code> to generate patches for you. You should only have to use <code>git apply</code> for legacy patches and things like that.
</p>

<p>
  To apply a patch generated by <code>format-patch</code>, you use <code>git am</code>. Technically, <code>git am</code> is built to read an mbox file, which is a simple, plain-text format for
  storing one or more e-mail messages in one text file. It looks something like
  this:
</p>

<pre>
From 330090432754092d704da8e76ca5c05c198e71a8 Mon Sep 17 00:00:00 2001
From: Jessica Smith &lt;jessica@example.com&gt;
Date: Sun, 6 Apr 2008 10:17:23 -0700
Subject: [PATCH 1/2] add limit to log function

Limit log functionality to the first 20
</pre>

<p>
  This is the beginning of the output of the format-patch command that you saw
  in the previous section. This is also a valid mbox e-mail format. If someone
  has e-mailed you the patch properly using <code>git send-email</code>, and you download that into an mbox format, then you can point <code>git am</code> to that mbox file, and it will start applying all the patches it sees. If
  you run a mail client that can save several e-mails out in mbox format, you
  can save entire patch series into a file and then use <code>git am</code> to apply them one at a time.
</p>

<p>
  However, if someone uploaded a patch file generated via <code>format-patch</code> to a ticketing system or something similar, you can save the file locally
  and then pass that file saved on your disk to <code>git am</code> to apply it:
</p>

<pre>
$ git am 0001-limit-log-function.patch
Applying: add limit to log function
</pre>

<p>
  You can see that it applied cleanly and automatically created the new commit
  for you. The author information is taken from the e-mail’s <code>From</code> and <code>Date</code> headers, and the message of the commit is taken from the <code>Subject</code> and body (before the patch) of the e-mail. For example, if this patch was
  applied from the mbox example I just showed, the commit generated would look
  something like this:
</p>

<pre>
$ git log --pretty=fuller -1
commit 6c5e70b984a60b3cecd395edd5b48a7575bf58e0
Author:     Jessica Smith &lt;jessica@example.com&gt;
AuthorDate: Sun Apr 6 10:17:23 2008 -0700
Commit:     Scott Chacon &lt;schacon@gmail.com&gt;
CommitDate: Thu Apr 9 09:19:06 2009 -0700

   add limit to log function

   Limit log functionality to the first 20
</pre>

<p>
  The <code>Commit</code> information indicates the person who applied the patch and the time it was
  applied. The <code>Author</code> information is the individual who originally created the patch and when it
  was originally created.
</p>

<p>
  But it’s possible that the patch won’t apply cleanly. Perhaps your main
  branch has diverged too far from the branch the patch was built from, or the
  patch depends on another patch you haven’t applied yet. In that case, the <code>git am</code> process will fail and ask you what you want to do:
</p>

<pre>
$ git am 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
Patch failed at 0001.
When you have resolved this problem run "git am --resolved".
If you would prefer to skip this patch, instead run "git am --skip".
To restore the original branch and stop patching run "git am --abort".
</pre>

<p>
  This command puts conflict markers in any files it has issues with, much like
  a conflicted merge or rebase operation. You solve this issue much the same
  way — edit the file to resolve the conflict, stage the new file, and then run
  <code>git am --resolved</code> to continue to the next patch:
</p>

<pre>
$ (fix the file)
$ git add ticgit.gemspec
$ git am --resolved
Applying: seeing if this helps the gem
</pre>

<p>
  If you want Git to try a bit more intelligently to resolve the conflict, you
  can pass a <code>-3</code> option to it, which makes Git attempt a three-way merge. This option isn’t
  on by default because it doesn’t work if the commit the patch says it was
  based on isn’t in your repository. If you do have that commit — if the patch
  was based on a public commit — then the <code>-3</code> option is generally much smarter about applying a conflicting patch:
</p>

<pre>
$ git am -3 0001-seeing-if-this-helps-the-gem.patch
Applying: seeing if this helps the gem
error: patch failed: ticgit.gemspec:1
error: ticgit.gemspec: patch does not apply
Using index info to reconstruct a base tree...
Falling back to patching base and 3-way merge...
No changes -- Patch already applied.
</pre>

<p>
  In this case, I was trying to apply a patch I had already applied. Without
  the <code>-3</code> option, it looks like a conflict.
</p>

<p>
  If you’re applying a number of patches from an mbox, you can also run the <code>am</code> command in interactive mode, which stops at each patch it finds and asks if
  you want to apply it:
</p>

<pre>
$ git am -3 -i mbox
Commit Body is:
--------------------------
seeing if this helps the gem
--------------------------
Apply? [y]es/[n]o/[e]dit/[v]iew patch/[a]ccept all
</pre>

<p>
  This is nice if you have a number of patches saved, because you can view the
  patch first if you don’t remember what it is, or not apply the patch if
  you’ve already done so.
</p>

<p>
  When all the patches for your topic are applied and committed into your
  branch, you can choose whether and how to integrate them into a
  longer-running branch.
</p>

<h3>Checking Out Remote Branches</h3>

<p>
  If your contribution came from a Git user who set up their own repository,
  pushed a number of changes into it, and then sent you the URL to the
  repository and the name of the remote branch the changes are in, you can add
  them as a remote and do merges locally.
</p>

<p>
  For instance, if Jessica sends you an e-mail saying that she has a great new
  feature in the <code>ruby-client</code> branch of her repository, you can test it by adding the remote and checking
  out that branch locally:
</p>

<pre>
$ git remote add jessica git://github.com/jessica/myproject.git
$ git fetch jessica
$ git checkout -b rubyclient jessica/ruby-client
</pre>

<p>
  If she e-mails you again later with another branch containing another great
  feature, you can fetch and check out because you already have the remote
  setup.
</p>

<p>
  This is most useful if you’re working with a person consistently. If someone
  only has a single patch to contribute once in a while, then accepting it over
  e-mail may be less time consuming than requiring everyone to run their own
  server and having to continually add and remove remotes to get a few patches.
  You’re also unlikely to want to have hundreds of remotes, each for someone
  who contributes only a patch or two. However, scripts and hosted services may
  make this easier — it depends largely on how you develop and how your
  contributors develop.
</p>

<p>
  The other advantage of this approach is that you get the history of the
  commits as well. Although you may have legitimate merge issues, you know
  where in your history their work is based; a proper three-way merge is the
  default rather than having to supply a <code>-3</code> and hope the patch was generated off a public commit to which you have
  access.
</p>

<p>
  If you aren’t working with a person consistently but still want to pull from
  them in this way, you can provide the URL of the remote repository to the <code>git pull</code> command. This does a one-time pull and doesn’t save the URL as a remote
  reference:
</p>

<pre>
$ git pull git://github.com/onetimeguy/project.git
From git://github.com/onetimeguy/project
 * branch            HEAD       -&gt; FETCH_HEAD
Merge made by recursive.
</pre>

<h3>Determining What Is Introduced</h3>

<p>
  Now you have a topic branch that contains contributed work. At this point,
  you can determine what you’d like to do with it. This section revisits a
  couple of commands so you can see how you can use them to review exactly what
  you’ll be introducing if you merge this into your main branch.
</p>

<p>
  It’s often helpful to get a review of all the commits that are in this branch
  but that aren’t in your master branch. You can exclude commits in the master
  branch by adding the <code>--not</code> option before the branch name. For example, if your contributor sends you
  two patches and you create a branch called <code>contrib</code> and applied those patches there, you can run this:
</p>

<pre>
$ git log contrib --not master
commit 5b6235bd297351589efc4d73316f0a68d484f118
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri Oct 24 09:53:59 2008 -0700

    seeing if this helps the gem

commit 7482e0d16d04bea79d0dba8988cc78df655f16a0
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Mon Oct 22 19:38:36 2008 -0700

    updated the gemspec to hopefully work better
</pre>

<p>
  To see what changes each commit introduces, remember that you can pass the <code>-p</code> option to <code>git log</code> and it will append the diff introduced to each commit.
</p>

<p>
  To see a full diff of what would happen if you were to merge this topic
  branch with another branch, you may have to use a weird trick to get the
  correct results. You may think to run this:
</p>

<pre>
$ git diff master
</pre>

<p>
  This command gives you a diff, but it may be misleading. If your <code>master</code> branch has moved forward since you created the topic branch from it, then
  you’ll get seemingly strange results. This happens because Git directly
  compares the snapshots of the last commit of the topic branch you’re on and
  the snapshot of the last commit on the <code>master</code> branch. For example, if you’ve added a line in a file on the <code>master</code> branch, a direct comparison of the snapshots will look like the topic branch
  is going to remove that line.
</p>

<p>
  If <code>master</code> is a direct ancestor of your topic branch, this isn’t a problem; but if the
  two histories have diverged, the diff will look like you’re adding all the
  new stuff in your topic branch and removing everything unique to the <code>master</code> branch.
</p>

<p>
  What you really want to see are the changes added to the topic branch — the
  work you’ll introduce if you merge this branch with master. You do that by
  having Git compare the last commit on your topic branch with the first common
  ancestor it has with the master branch.
</p>

<p>
  Technically, you can do that by explicitly figuring out the common ancestor
  and then running your diff on it:
</p>

<pre>
$ git merge-base contrib master
36c7dba2c95e6bbb78dfa822519ecfec6e1ca649
$ git diff 36c7db
</pre>

<p>
  However, that isn’t convenient, so Git provides another shorthand for doing
  the same thing: the triple-dot syntax. In the context of the <code>diff</code> command, you can put three periods after another branch to do a <code>diff</code> between the last commit of the branch you’re on and its common ancestor with
  another branch:
</p>

<pre>
$ git diff master...contrib
</pre>

<p>
  This command shows you only the work your current topic branch has introduced
  since its common ancestor with master. That is a very useful syntax to
  remember.
</p>

<h3>Integrating Contributed Work</h3>

<p>
  When all the work in your topic branch is ready to be integrated into a more
  mainline branch, the question is how to do it. Furthermore, what overall
  workflow do you want to use to maintain your project? You have a number of
  choices, so I’ll cover a few of them.
</p>

<p>Merging Workflows</p>

<p>
  One simple workflow merges your work into your <code>master</code> branch. In this scenario, you have a <code>master</code> branch that contains basically stable code. When you have work in a topic
  branch that you’ve done or that someone has contributed and you’ve verified,
  you merge it into your master branch, delete the topic branch, and then
  continue the process.&emsp14;&emsp14;If we have a repository with work in two
  branches named <code>ruby_client</code> and <code>php_client</code> that looks like Figure 5-19 and merge <code>ruby_client</code> first and then <code>php_client</code> next, then your history will end up looking like Figure 5-20.
</p>

<p>
  Insert 18333fig0519.png Figure 5-19. History with several topic branches.
</p>

<p>Insert 18333fig0520.png Figure 5-20. After a topic branch merge.</p>

<p>
  That is probably the simplest workflow, but it’s problematic if you’re
  dealing with larger repositories or projects.
</p>

<p>
  If you have more developers or a larger project, you’ll probably want to use
  at least a two-phase merge cycle. In this scenario, you have two long-running
  branches, <code>master</code> and <code>develop</code>, in which you determine that <code>master</code> is updated only when a very stable release is cut and all new code is
  integrated into the <code>develop</code> branch. You regularly push both of these branches to the public repository.
  Each time you have a new topic branch to merge in (Figure 5-21), you merge it
  into <code>develop</code> (Figure 5-22); then, when you tag a release, you fast-forward <code>master</code> to wherever the now-stable <code>develop</code> branch is (Figure 5-23).
</p>

<p>Insert 18333fig0521.png Figure 5-21. Before a topic branch merge.</p>

<p>Insert 18333fig0522.png Figure 5-22. After a topic branch merge.</p>

<p>Insert 18333fig0523.png Figure 5-23. After a topic branch release.</p>

<p>
  This way, when people clone your project’s repository, they can either check
  out master to build the latest stable version and keep up to date on that
  easily, or they can check out develop, which is the more cutting-edge stuff.
  You can also continue this concept, having an integrate branch where all the
  work is merged together. Then, when the codebase on that branch is stable and
  passes tests, you merge it into a develop branch; and when that has proven
  itself stable for a while, you fast-forward your master branch.
</p>

<p>Large-Merging Workflows</p>

<p>
  The Git project has four long-running branches: <code>master</code>, <code>next</code>, and <code>pu</code> (proposed updates) for new work, and <code>maint</code> for maintenance backports. When new work is introduced by contributors, it’s
  collected into topic branches in the maintainer’s repository in a manner
  similar to what I’ve described (see Figure 5-24). At this point, the topics
  are evaluated to determine whether they’re safe and ready for consumption or
  whether they need more work. If they’re safe, they’re merged into <code>next</code>, and that branch is pushed up so everyone can try the topics integrated
  together.
</p>

<p>
  Insert 18333fig0524.png Figure 5-24. Managing a complex series of parallel
  contributed topic branches.
</p>

<p>
  If the topics still need work, they’re merged into <code>pu</code> instead. When it’s determined that they’re totally stable, the topics are
  re-merged into <code>master</code> and are then rebuilt from the topics that were in <code>next</code> but didn’t yet graduate to <code>master</code>. This means <code>master</code> almost always moves forward, <code>next</code> is rebased occasionally, and <code>pu</code> is rebased even more often (see Figure 5-25).
</p>

<p>
  Insert 18333fig0525.png Figure 5-25. Merging contributed topic branches into
  long-term integration branches.
</p>

<p>
  When a topic branch has finally been merged into <code>master</code>, it’s removed from the repository. The Git project also has a <code>maint</code> branch that is forked off from the last release to provide backported
  patches in case a maintenance release is required. Thus, when you clone the
  Git repository, you have four branches that you can check out to evaluate the
  project in different stages of development, depending on how cutting edge you
  want to be or how you want to contribute; and the maintainer has a structured
  workflow to help them vet new contributions.
</p>

<p>Rebasing and Cherry Picking Workflows</p>

<p>
  Other maintainers prefer to rebase or cherry-pick contributed work on top of
  their master branch, rather than merging it in, to keep a mostly linear
  history. When you have work in a topic branch and have determined that you
  want to integrate it, you move to that branch and run the rebase command to
  rebuild the changes on top of your current master (or <code>develop</code>, and so on) branch. If that works well, you can fast-forward your <code>master</code> branch, and you’ll end up with a linear project history.
</p>

<p>
  The other way to move introduced work from one branch to another is to
  cherry-pick it. A cherry-pick in Git is like a rebase for a single commit. It
  takes the patch that was introduced in a commit and tries to reapply it on
  the branch you’re currently on. This is useful if you have a number of
  commits on a topic branch and you want to integrate only one of them, or if
  you only have one commit on a topic branch and you’d prefer to cherry-pick it
  rather than run rebase. For example, suppose you have a project that looks
  like Figure 5-26.
</p>

<p>
  Insert 18333fig0526.png Figure 5-26. Example history before a cherry pick.
</p>

<p>
  If you want to pull commit <code>e43a6</code> into your master branch, you can run
</p>

<pre>
$ git cherry-pick e43a6fd3e94888d76779ad79fb568ed180e5fcdf
Finished one cherry-pick.
[master]: created a0a41a9: "More friendly message when locking the index fails."
 3 files changed, 17 insertions(+), 3 deletions(-)
</pre>

<p>
  This pulls the same change introduced in <code>e43a6</code>, but you get a new commit SHA-1 value, because the date applied is
  different. Now your history looks like Figure 5-27.
</p>

<p>
  Insert 18333fig0527.png Figure 5-27. History after cherry-picking a commit on
  a topic branch.
</p>

<p>
  Now you can remove your topic branch and drop the commits you didn’t want to
  pull in.
</p>

<h3>Tagging Your Releases</h3>

<p>
  When you’ve decided to cut a release, you’ll probably want to drop a tag so
  you can re-create that release at any point going forward. You can create a
  new tag as I discussed in Chapter 2. If you decide to sign the tag as the
  maintainer, the tagging may look something like this:
</p>

<pre>
$ git tag -s v1.5 -m 'my signed 1.5 tag'
You need a passphrase to unlock the secret key for
user: "Scott Chacon &lt;schacon@gmail.com&gt;"
1024-bit DSA key, ID F721C45A, created 2009-02-09
</pre>

<p>
  If you do sign your tags, you may have the problem of distributing the public
  PGP key used to sign your tags. The maintainer of the Git project has solved
  this issue by including their public key as a blob in the repository and then
  adding a tag that points directly to that content. To do this, you can figure
  out which key you want by running <code>gpg --list-keys</code>:
</p>

<pre>
$ gpg --list-keys
/Users/schacon/.gnupg/pubring.gpg
---------------------------------
pub   1024D/F721C45A 2009-02-09 [expires: 2010-02-09]
uid                  Scott Chacon &lt;schacon@gmail.com&gt;
sub   2048g/45D02282 2009-02-09 [expires: 2010-02-09]
</pre>

<p>
  Then, you can directly import the key into the Git database by exporting it
  and piping that through <code>git hash-object</code>, which writes a new blob with those contents into Git and gives you back the
  SHA-1 of the blob:
</p>

<pre>
$ gpg -a --export F721C45A | git hash-object -w --stdin
659ef797d181633c87ec71ac3f9ba29fe5775b92
</pre>

<p>
  Now that you have the contents of your key in Git, you can create a tag that
  points directly to it by specifying the new SHA-1 value that the <code>hash-object</code> command gave you:
</p>

<pre>
$ git tag -a maintainer-pgp-pub 659ef797d181633c87ec71ac3f9ba29fe5775b92
</pre>

<p>
  If you run <code>git push --tags</code>, the <code>maintainer-pgp-pub</code> tag will be shared with everyone. If anyone wants to verify a tag, they can
  directly import your PGP key by pulling the blob directly out of the database
  and importing it into GPG:
</p>

<pre>
$ git show maintainer-pgp-pub | gpg --import
</pre>

<p>
  They can use that key to verify all your signed tags. Also, if you include
  instructions in the tag message, running <code>git show &lt;tag></code> will let you give the end user more specific instructions about tag
  verification.
</p>

<h3>Generating a Build Number</h3>

<p>
  Because Git doesn’t have monotonically increasing numbers like 'v123' or the
  equivalent to go with each commit, if you want to have a human-readable name
  to go with a commit, you can run <code>git describe</code> on that commit. Git gives you the name of the nearest tag with the number of
  commits on top of that tag and a partial SHA-1 value of the commit you’re
  describing:
</p>

<pre>
$ git describe master
v1.6.2-rc1-20-g8c5b85c
</pre>

<p>
  This way, you can export a snapshot or build and name it something
  understandable to people. In fact, if you build Git from source code cloned
  from the Git repository, <code>git --version</code> gives you something that looks like this. If you’re describing a commit that
  you have directly tagged, it gives you the tag name.
</p>

<p>
  The <code>git describe</code> command favors annotated tags (tags created with the <code>-a</code> or <code>-s</code> flag), so release tags should be created this way if you’re using <code>git describe</code>, to ensure the commit is named properly when described. You can also use
  this string as the target of a checkout or show command, although it relies
  on the abbreviated SHA-1 value at the end, so it may not be valid forever.
  For instance, the Linux kernel recently jumped from 8 to 10 characters to
  ensure SHA-1 object uniqueness, so older <code>git describe</code> output names were invalidated.
</p>

<h3>Preparing a Release</h3>

<p>
  Now you want to release a build. One of the things you’ll want to do is
  create an archive of the latest snapshot of your code for those poor souls
  who don’t use Git. The command to do this is <code>git archive</code>:
</p>

<pre>
$ git archive master --prefix='project/' | gzip &gt; `git describe master`.tar.gz
$ ls *.tar.gz
v1.6.2-rc1-20-g8c5b85c.tar.gz
</pre>

<p>
  If someone opens that tarball, they get the latest snapshot of your project
  under a project directory. You can also create a zip archive in much the same
  way, but by passing the <code>--format=zip</code> option to <code>git archive</code>:
</p>

<pre>
$ git archive master --prefix='project/' --format=zip &gt; `git describe master`.zip
</pre>

<p>
  You now have a nice tarball and a zip archive of your project release that
  you can upload to your website or e-mail to people.
</p>

<h3>The Shortlog</h3>

<p>
  It’s time to e-mail your mailing list of people who want to know what’s
  happening in your project. A nice way of quickly getting a sort of changelog
  of what has been added to your project since your last release or e-mail is
  to use the <code>git shortlog</code> command. It summarizes all the commits in the range you give it; for
  example, the following gives you a summary of all the commits since your last
  release, if your last release was named v1.0.1:
</p>

<pre>
$ git shortlog --no-merges master --not v1.0.1
Chris Wanstrath (8):
      Add support for annotated tags to Grit::Tag
      Add packed-refs annotated tag support.
      Add Grit::Commit#to_patch
      Update version and History.txt
      Remove stray `puts`
      Make ls_tree ignore nils

Tom Preston-Werner (4):
      fix dates in history
      dynamic version method
      Version bump to 1.0.2
      Regenerated gemspec for version 1.0.2
</pre>

<p>
  You get a clean summary of all the commits since v1.0.1, grouped by author,
  that you can e-mail to your list.
</p>

<h2>Summary</h2>

<p>
  You should feel fairly comfortable contributing to a project in Git as well
  as maintaining your own project or integrating other users’ contributions.
  Congratulations on being an effective Git developer! In the next chapter,
  you’ll learn more powerful tools and tips for dealing with complex
  situations, which will truly make you a Git master.
</p>

<h1>Git Tools</h1>

<p>
  By now, you’ve learned most of the day-to-day commands and workflows that you
  need to manage or maintain a Git repository for your source code control.
  You’ve accomplished the basic tasks of tracking and committing files, and
  you’ve harnessed the power of the staging area and lightweight topic
  branching and merging.
</p>

<p>
  Now you’ll explore a number of very powerful things that Git can do that you
  may not necessarily use on a day-to-day basis but that you may need at some
  point.
</p>

<h2>Revision Selection</h2>

<p>
  Git allows you to specify specific commits or a range of commits in several
  ways. They aren’t necessarily obvious but are helpful to know.
</p>

<h3>Single Revisions</h3>

<p>
  You can obviously refer to a commit by the SHA-1 hash that it’s given, but
  there are more human-friendly ways to refer to commits as well. This section
  outlines the various ways you can refer to a single commit.
</p>

<h3>Short SHA</h3>

<p>
  Git is smart enough to figure out what commit you meant to type if you
  provide the first few characters, as long as your partial SHA-1 is at least
  four characters long and unambiguous — that is, only one object in the
  current repository begins with that partial SHA-1.
</p>

<p>
  For example, to see a specific commit, suppose you run a <code>git log</code> command and identify the commit where you added certain functionality:
</p>

<pre>
$ git log
commit 734713bc047d87bf7eac9674765ae793478c50d3
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri Jan 2 18:32:33 2009 -0800

    fixed refs handling, added gc auto, updated tests

commit d921970aadf03b3cf0e71becdaab3147ba71cdef
Merge: 1c002dd... 35cfb2b...
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Thu Dec 11 15:08:43 2008 -0800

    Merge commit 'phedders/rdocs'

commit 1c002dd4b536e7479fe34593e72e6c6c1819e53b
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Thu Dec 11 14:58:32 2008 -0800

    added some blame and merge stuff
</pre>

<p>
  In this case, choose <code>1c002dd....</code> If you <code>git show</code> that commit, the following commands are equivalent (assuming the shorter
  versions are unambiguous):
</p>

<pre>
$ git show 1c002dd4b536e7479fe34593e72e6c6c1819e53b
$ git show 1c002dd4b536e7479f
$ git show 1c002d
</pre>

<p>
  Git can figure out a short, unique abbreviation for your SHA-1 values. If you
  pass <code>--abbrev-commit</code> to the <code>git log</code> command, the output will use shorter values but keep them unique; it
  defaults to using seven characters but makes them longer if necessary to keep
  the SHA-1 unambiguous:
</p>

<pre>
$ git log --abbrev-commit --pretty=oneline
ca82a6d changed the version number
085bb3b removed unnecessary test code
a11bef0 first commit
</pre>

<p>
  Generally, eight to ten characters are more than enough to be unique within a
  project. One of the largest Git projects, the Linux kernel, is beginning to
  need 12 characters out of the possible 40 to stay unique.
</p>

<h3>A SHORT NOTE ABOUT SHA-1</h3>

<p>
  A lot of people become concerned at some point that they will, by random
  happenstance, have two objects in their repository that hash to the same
  SHA-1 value. What then?
</p>

<p>
  If you do happen to commit an object that hashes to the same SHA-1 value as a
  previous object in your repository, Git will see the previous object already
  in your Git database and assume it was already written. If you try to check
  out that object again at some point, you’ll always get the data of the first
  object.
</p>

<p>
  However, you should be aware of how ridiculously unlikely this scenario is.
  The SHA-1 digest is 20 bytes or 160 bits. The number of randomly hashed
  objects needed to ensure a 50% probability of a single collision is about
  2^80 (the formula for determining collision probability is <code>p = (n(n-1)/2) * (1/2^160)</code>). 2^80 is 1.2 x 10^24 or 1 million billion billion. That’s 1,200 times the
  number of grains of sand on the earth.
</p>

<p>
  Here’s an example to give you an idea of what it would take to get a SHA-1
  collision. If all 6.5 billion humans on Earth were programming, and every
  second, each one was producing code that was the equivalent of the entire
  Linux kernel history (1 million Git objects) and pushing it into one enormous
  Git repository, it would take 5 years until that repository contained enough
  objects to have a 50% probability of a single SHA-1 object collision. A
  higher probability exists that every member of your programming team will be
  attacked and killed by wolves in unrelated incidents on the same night.
</p>

<h3>Branch References</h3>

<p>
  The most straightforward way to specify a commit requires that it have a
  branch reference pointed at it. Then, you can use a branch name in any Git
  command that expects a commit object or SHA-1 value. For instance, if you
  want to show the last commit object on a branch, the following commands are
  equivalent, assuming that the <code>topic1</code> branch points to <code>ca82a6d</code>:
</p>

<pre>
$ git show ca82a6dff817ec66f44342007202690a93763949
$ git show topic1
</pre>

<p>
  If you want to see which specific SHA a branch points to, or if you want to
  see what any of these examples boils down to in terms of SHAs, you can use a
  Git plumbing tool called <code>rev-parse</code>. You can see Chapter 9 for more information about plumbing tools; basically,
  <code>rev-parse</code> exists for lower-level operations and isn’t designed to be used in
  day-to-day operations. However, it can be helpful sometimes when you need to
  see what’s really going on. Here you can run <code>rev-parse</code> on your branch.
</p>

<pre>
$ git rev-parse topic1
ca82a6dff817ec66f44342007202690a93763949
</pre>

<h3>RefLog Shortnames</h3>

<p>
  One of the things Git does in the background while you’re working away is
  keep a reflog — a log of where your HEAD and branch references have been for
  the last few months.
</p>

<p>You can see your reflog by using <code>git reflog</code>:</p>

<pre>
$ git reflog
734713b HEAD@{0}: commit: fixed refs handling, added gc auto, updated
d921970 HEAD@{1}: merge phedders/rdocs: Merge made by recursive.
1c002dd HEAD@{2}: commit: added some blame and merge stuff
1c36188 HEAD@{3}: rebase -i (squash): updating HEAD
95df984 HEAD@{4}: commit: # This is a combination of two commits.
1c36188 HEAD@{5}: rebase -i (squash): updating HEAD
7e05da5 HEAD@{6}: rebase -i (pick): updating HEAD
</pre>

<p>
  Every time your branch tip is updated for any reason, Git stores that
  information for you in this temporary history. And you can specify older
  commits with this data, as well. If you want to see the fifth prior value of
  the HEAD of your repository, you can use the <code>@{n}</code> reference that you see in the reflog output:
</p>

<pre>
$ git show HEAD@{5}
</pre>

<p>
  You can also use this syntax to see where a branch was some specific amount
  of time ago. For instance, to see where your <code>master</code> branch was yesterday, you can type
</p>

<pre>
$ git show master@{yesterday}
</pre>

<p>
  That shows you where the branch tip was yesterday. This technique only works
  for data that’s still in your reflog, so you can’t use it to look for commits
  older than a few months.
</p>

<p>
  To see reflog information formatted like the <code>git log</code> output, you can run <code>git log -g</code>:
</p>

<pre>
$ git log -g master
commit 734713bc047d87bf7eac9674765ae793478c50d3
Reflog: master@{0} (Scott Chacon &lt;schacon@gmail.com&gt;)
Reflog message: commit: fixed refs handling, added gc auto, updated
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri Jan 2 18:32:33 2009 -0800

    fixed refs handling, added gc auto, updated tests

commit d921970aadf03b3cf0e71becdaab3147ba71cdef
Reflog: master@{1} (Scott Chacon &lt;schacon@gmail.com&gt;)
Reflog message: merge phedders/rdocs: Merge made by recursive.
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Thu Dec 11 15:08:43 2008 -0800

    Merge commit 'phedders/rdocs'
</pre>

<p>
  It’s important to note that the reflog information is strictly local — it’s a
  log of what you’ve done in your repository. The references won’t be the same
  on someone else’s copy of the repository; and right after you initially clone
  a repository, you’ll have an empty reflog, as no activity has occurred yet in
  your repository. Running <code>git show HEAD@{2.months.ago}</code> will work only if you cloned the project at least two months ago — if you
  cloned it five minutes ago, you’ll get no results.
</p>

<h3>Ancestry References</h3>

<p>
  The other main way to specify a commit is via its ancestry. If you place a <code>^</code> at the end of a reference, Git resolves it to mean the parent of that
  commit. Suppose you look at the history of your project:
</p>

<pre>
$ git log --pretty=format:'%h %s' --graph
* 734713b fixed refs handling, added gc auto, updated tests
*   d921970 Merge commit 'phedders/rdocs'
|\
| * 35cfb2b Some rdoc changes
* | 1c002dd added some blame and merge stuff
|/
* 1c36188 ignore *.gem
* 9b29157 add open3_detach to gemspec file list
</pre>

<p>
  Then, you can see the previous commit by specifying <code>HEAD^</code>, which means "the parent of HEAD":
</p>

<pre>
$ git show HEAD^
commit d921970aadf03b3cf0e71becdaab3147ba71cdef
Merge: 1c002dd... 35cfb2b...
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Thu Dec 11 15:08:43 2008 -0800

    Merge commit 'phedders/rdocs'
</pre>

<p>
  You can also specify a number after the <code>^</code> — for example, <code>d921970^2</code> means "the second parent of d921970." This syntax is only useful for merge
  commits, which have more than one parent. The first parent is the branch you
  were on when you merged, and the second is the commit on the branch that you
  merged in:
</p>

<pre>
$ git show d921970^
commit 1c002dd4b536e7479fe34593e72e6c6c1819e53b
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Thu Dec 11 14:58:32 2008 -0800

    added some blame and merge stuff

$ git show d921970^2
commit 35cfb2b795a55793d7cc56a6cc2060b4bb732548
Author: Paul Hedderly &lt;paul+git@mjr.org&gt;
Date:   Wed Dec 10 22:22:03 2008 +0000

    Some rdoc changes
</pre>

<p>
  The other main ancestry specification is the <code>~</code>. This also refers to the first parent, so <code>HEAD~</code> and <code>HEAD^</code> are equivalent. The difference becomes apparent when you specify a number. <code>HEAD~2</code> means "the first parent of the first parent," or "the grandparent" — it
  traverses the first parents the number of times you specify. For example, in
  the history listed earlier, <code>HEAD~3</code> would be
</p>

<pre>
$ git show HEAD~3
commit 1c3618887afb5fbcbea25b7c013f4e2114448b8d
Author: Tom Preston-Werner &lt;tom@mojombo.com&gt;
Date:   Fri Nov 7 13:47:59 2008 -0500

    ignore *.gem
</pre>

<p>
  This can also be written <code>HEAD^^^</code>, which again is the first parent of the first parent of the first parent:
</p>

<pre>
$ git show HEAD^^^
commit 1c3618887afb5fbcbea25b7c013f4e2114448b8d
Author: Tom Preston-Werner &lt;tom@mojombo.com&gt;
Date:   Fri Nov 7 13:47:59 2008 -0500

    ignore *.gem
</pre>

<p>
  You can also combine these syntaxes — you can get the second parent of the
  previous reference (assuming it was a merge commit) by using <code>HEAD~3^2</code>, and so on.
</p>

<h3>Commit Ranges</h3>

<p>
  Now that you can specify individual commits, let’s see how to specify ranges
  of commits. This is particularly useful for managing your branches — if you
  have a lot of branches, you can use range specifications to answer questions
  such as, "What work is on this branch that I haven’t yet merged into my main
  branch?"
</p>

<p>Double Dot</p>

<p>
  The most common range specification is the double-dot syntax. This basically
  asks Git to resolve a range of commits that are reachable from one commit but
  aren’t reachable from another. For example, say you have a commit history
  that looks like Figure 6-1.
</p>

<p>Insert 18333fig0601.png Figure 6-1. Example history for range selection.</p>

<p>
  You want to see what is in your experiment branch that hasn’t yet been merged
  into your master branch. You can ask Git to show you a log of just those
  commits with <code>master..experiment</code> — that means "all commits reachable by experiment that aren’t reachable by
  master." For the sake of brevity and clarity in these examples, I’ll use the
  letters of the commit objects from the diagram in place of the actual log
  output in the order that they would display:
</p>

<pre>
$ git log master..experiment
D
C
</pre>

<p>
  If, on the other hand, you want to see the opposite — all commits in <code>master</code> that aren’t in <code>experiment</code> — you can reverse the branch names. <code>experiment..master</code> shows you everything in <code>master</code> not reachable from <code>experiment</code>:
</p>

<pre>
$ git log experiment..master
F
E
</pre>

<p>
  This is useful if you want to keep the <code>experiment</code> branch up to date and preview what you’re about to merge in. Another very
  frequent use of this syntax is to see what you’re about to push to a remote:
</p>

<pre>
$ git log origin/master..HEAD
</pre>

<p>
  This command shows you any commits in your current branch that aren’t in the <code>master</code> branch on your <code>origin</code> remote. If you run a <code>git push</code> and your current branch is tracking <code>origin/master</code>, the commits listed by <code>git log origin/master..HEAD</code> are the commits that will be transferred to the server. You can also leave
  off one side of the syntax to have Git assume HEAD. For example, you can get
  the same results as in the previous example by typing <code>git log origin/master..</code> — Git substitutes HEAD if one side is missing.
</p>

<p>Multiple Points</p>

<p>
  The double-dot syntax is useful as a shorthand; but perhaps you want to
  specify more than two branches to indicate your revision, such as seeing what
  commits are in any of several branches that aren’t in the branch you’re
  currently on. Git allows you to do this by using either the <code>^</code> character or <code>--not</code> before any reference from which you don’t want to see reachable commits.
  Thus these three commands are equivalent:
</p>

<pre>
$ git log refA..refB
$ git log ^refA refB
$ git log refB --not refA
</pre>

<p>
  This is nice because with this syntax you can specify more than two
  references in your query, which you cannot do with the double-dot syntax. For
  instance, if you want to see all commits that are reachable from <code>refA</code> or <code>refB</code> but not from <code>refC</code>, you can type one of these:
</p>

<pre>
$ git log refA refB ^refC
$ git log refA refB --not refC
</pre>

<p>
  This makes for a very powerful revision query system that should help you
  figure out what is in your branches.
</p>

<p>Triple Dot</p>

<p>
  The last major range-selection syntax is the triple-dot syntax, which
  specifies all the commits that are reachable by either of two references but
  not by both of them. Look back at the example commit history in Figure 6-1.
  If you want to see what is in <code>master</code> or <code>experiment</code> but not any common references, you can run
</p>

<pre>
$ git log master...experiment
F
E
D
C
</pre>

<p>
  Again, this gives you normal <code>log</code> output but shows you only the commit information for those four commits,
  appearing in the traditional commit date ordering.
</p>

<p>
  A common switch to use with the <code>log</code> command in this case is <code>--left-right</code>, which shows you which side of the range each commit is in. This helps make
  the data more useful:
</p>

<pre>
$ git log --left-right master...experiment
&lt; F
&lt; E
&gt; D
&gt; C
</pre>

<p>
  With these tools, you can much more easily let Git know what commit or
  commits you want to inspect.
</p>

<h2>Interactive Staging</h2>

<p>
  Git comes with a couple of scripts that make some command-line tasks easier.
  Here, you’ll look at a few interactive commands that can help you easily
  craft your commits to include only certain combinations and parts of files.
  These tools are very helpful if you modify a bunch of files and then decide
  that you want those changes to be in several focused commits rather than one
  big messy commit. This way, you can make sure your commits are logically
  separate changesets and can be easily reviewed by the developers working with
  you. If you run <code>git add</code> with the <code>-i</code> or <code>--interactive</code> option, Git goes into an interactive shell mode, displaying something like
  this:
</p>

<pre>
$ git add -i
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:    unchanged        +1/-1 index.html
  3:    unchanged        +5/-1 lib/simplegit.rb

*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now&gt;
</pre>

<p>
  You can see that this command shows you a much different view of your staging
  area — basically the same information you get with <code>git status</code> but a bit more succinct and informative. It lists the changes you’ve staged
  on the left and unstaged changes on the right.
</p>

<p>
  After this comes a Commands section. Here you can do a number of things,
  including staging files, unstaging files, staging parts of files, adding
  untracked files, and seeing diffs of what has been staged.
</p>

<h3>Staging and Unstaging Files</h3>

<p>
  If you type <code>2</code> or <code>u</code> at the <code>What now></code> prompt, the script prompts you for which files you want to stage:
</p>

<pre>
What now&gt; 2
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:    unchanged        +1/-1 index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Update&gt;&gt;
</pre>

<p>To stage the TODO and index.html files, you can type the numbers:</p>

<pre>
Update&gt;&gt; 1,2
           staged     unstaged path
* 1:    unchanged        +0/-1 TODO
* 2:    unchanged        +1/-1 index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Update&gt;&gt;
</pre>

<p>
  The <code>*</code> next to each file means the file is selected to be staged. If you press
  Enter after typing nothing at the <code>Update>></code> prompt, Git takes anything selected and stages it for you:
</p>

<pre>
Update&gt;&gt;
updated 2 paths

*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now&gt; 1
           staged     unstaged path
  1:        +0/-1      nothing TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
</pre>

<p>
  Now you can see that the TODO and index.html files are staged and the
  simplegit.rb file is still unstaged. If you want to unstage the TODO file at
  this point, you use the <code>3</code> or <code>r</code> (for revert) option:
</p>

<pre>
*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now&gt; 3
           staged     unstaged path
  1:        +0/-1      nothing TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Revert&gt;&gt; 1
           staged     unstaged path
* 1:        +0/-1      nothing TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
Revert&gt;&gt; [enter]
reverted one path
</pre>

<p>
  Looking at your Git status again, you can see that you’ve unstaged the TODO
  file:
</p>

<pre>
*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now&gt; 1
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:        +1/-1      nothing index.html
  3:    unchanged        +5/-1 lib/simplegit.rb
</pre>

<p>
  To see the diff of what you’ve staged, you can use the <code>6</code> or <code>d</code> (for diff) command. It shows you a list of your staged files, and you can
  select the ones for which you would like to see the staged diff. This is much
  like specifying <code>git diff --cached</code> on the command line:
</p>

<pre>
*** Commands ***
  1: status     2: update      3: revert     4: add untracked
  5: patch      6: diff        7: quit       8: help
What now&gt; 6
           staged     unstaged path
  1:        +1/-1      nothing index.html
Review diff&gt;&gt; 1
diff --git a/index.html b/index.html
index 4d07108..4335f49 100644
--- a/index.html
+++ b/index.html
@@ -16,7 +16,7 @@ Date Finder

 &lt;p id="out"&gt;...&lt;/p&gt;

-&lt;div id="footer"&gt;contact : support@github.com&lt;/div&gt;
+&lt;div id="footer"&gt;contact : email.support@github.com&lt;/div&gt;

 &lt;script type="text/javascript"&gt;
</pre>

<p>
  With these basic commands, you can use the interactive add mode to deal with
  your staging area a little more easily.
</p>

<h3>Staging Patches</h3>

<p>
  It’s also possible for Git to stage certain parts of files and not the rest.
  For example, if you make two changes to your simplegit.rb file and want to
  stage one of them and not the other, doing so is very easy in Git. From the
  interactive prompt, type <code>5</code> or <code>p</code> (for patch). Git will ask you which files you would like to partially stage;
  then, for each section of the selected files, it will display hunks of the
  file diff and ask if you would like to stage them, one by one:
</p>

<pre>
diff --git a/lib/simplegit.rb b/lib/simplegit.rb
index dd5ecc4..57399e0 100644
--- a/lib/simplegit.rb
+++ b/lib/simplegit.rb
@@ -22,7 +22,7 @@ class SimpleGit
   end

   def log(treeish = 'master')
-    command("git log -n 25 #{treeish}")
+    command("git log -n 30 #{treeish}")
   end

   def blame(path)
Stage this hunk [y,n,a,d,/,j,J,g,e,?]?
</pre>

<p>
  You have a lot of options at this point. Typing <code>?</code> shows a list of what you can do:
</p>

<pre>
Stage this hunk [y,n,a,d,/,j,J,g,e,?]? ?
y - stage this hunk
n - do not stage this hunk
a - stage this and all the remaining hunks in the file
d - do not stage this hunk nor any of the remaining hunks in the file
g - select a hunk to go to
/ - search for a hunk matching the given regex
j - leave this hunk undecided, see next undecided hunk
J - leave this hunk undecided, see next hunk
k - leave this hunk undecided, see previous undecided hunk
K - leave this hunk undecided, see previous hunk
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
</pre>

<p>
  Generally, you’ll type <code>y</code> or <code>n</code> if you want to stage each hunk, but staging all of them in certain files or
  skipping a hunk decision until later can be helpful too. If you stage one
  part of the file and leave another part unstaged, your status output will
  look like this:
</p>

<pre>
What now&gt; 1
           staged     unstaged path
  1:    unchanged        +0/-1 TODO
  2:        +1/-1      nothing index.html
  3:        +1/-1        +4/-0 lib/simplegit.rb
</pre>

<p>
  The status of the simplegit.rb file is interesting. It shows you that a
  couple of lines are staged and a couple are unstaged. You’ve partially staged
  this file. At this point, you can exit the interactive adding script and run <code>git commit</code> to commit the partially staged files.
</p>

<p>
  Finally, you don’t need to be in interactive add mode to do the partial-file
  staging — you can start the same script by using <code>git add -p</code> or <code>git add --patch</code> on the command line.
</p>

<h2>Stashing</h2>

<p>
  Often, when you’ve been working on part of your project, things are in a
  messy state and you want to switch branches for a bit to work on something
  else. The problem is, you don’t want to do a commit of half-done work just so
  you can get back to this point later. The answer to this issue is the <code>git stash</code> command.
</p>

<p>
  Stashing takes the dirty state of your working directory — that is, your
  modified tracked files and staged changes — and saves it on a stack of
  unfinished changes that you can reapply at any time.
</p>

<h3>Stashing Your Work</h3>

<p>
  To demonstrate, you’ll go into your project and start working on a couple of
  files and possibly stage one of the changes. If you run <code>git status</code>, you can see your dirty state:
</p>

<pre>
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD &lt;file&gt;..." to unstage)
#
#      modified:   index.html
#
# Changes not staged for commit:
#   (use "git add &lt;file&gt;..." to update what will be committed)
#
#      modified:   lib/simplegit.rb
#
</pre>

<p>
  Now you want to switch branches, but you don’t want to commit what you’ve
  been working on yet; so you’ll stash the changes. To push a new stash onto
  your stack, run <code>git stash</code>:
</p>

<pre>
$ git stash
Saved working directory and index state \
  "WIP on master: 049d078 added the index file"
HEAD is now at 049d078 added the index file
(To restore them type "git stash apply")
</pre>

<p>Your working directory is clean:</p>

<pre>
$ git status
# On branch master
nothing to commit, working directory clean
</pre>

<p>
  At this point, you can easily switch branches and do work elsewhere; your
  changes are stored on your stack. To see which stashes you’ve stored, you can
  use <code>git stash list</code>:
</p>

<pre>
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
</pre>

<p>
  In this case, two stashes were done previously, so you have access to three
  different stashed works. You can reapply the one you just stashed by using
  the command shown in the help output of the original stash command: <code>git stash apply</code>. If you want to apply one of the older stashes, you can specify it by naming
  it, like this: <code>git stash apply stash@{2}</code>. If you don’t specify a stash, Git assumes the most recent stash and tries
  to apply it:
</p>

<pre>
$ git stash apply
# On branch master
# Changes not staged for commit:
#   (use "git add &lt;file&gt;..." to update what will be committed)
#
#      modified:   index.html
#      modified:   lib/simplegit.rb
#
</pre>

<p>
  You can see that Git re-modifies the files you uncommitted when you saved the
  stash. In this case, you had a clean working directory when you tried to
  apply the stash, and you tried to apply it on the same branch you saved it
  from; but having a clean working directory and applying it on the same branch
  aren’t necessary to successfully apply a stash. You can save a stash on one
  branch, switch to another branch later, and try to reapply the changes. You
  can also have modified and uncommitted files in your working directory when
  you apply a stash — Git gives you merge conflicts if anything no longer
  applies cleanly.
</p>

<p>
  The changes to your files were reapplied, but the file you staged before
  wasn’t restaged. To do that, you must run the <code>git stash apply</code> command with a <code>--index</code> option to tell the command to try to reapply the staged changes. If you had
  run that instead, you’d have gotten back to your original position:
</p>

<pre>
$ git stash apply --index
# On branch master
# Changes to be committed:
#   (use "git reset HEAD &lt;file&gt;..." to unstage)
#
#      modified:   index.html
#
# Changes not staged for commit:
#   (use "git add &lt;file&gt;..." to update what will be committed)
#
#      modified:   lib/simplegit.rb
#
</pre>

<p>
  The apply option only tries to apply the stashed work — you continue to have
  it on your stack. To remove it, you can run <code>git stash drop</code> with the name of the stash to remove:
</p>

<pre>
$ git stash list
stash@{0}: WIP on master: 049d078 added the index file
stash@{1}: WIP on master: c264051 Revert "added file_size"
stash@{2}: WIP on master: 21d80a5 added number to log
$ git stash drop stash@{0}
Dropped stash@{0} (364e91f3f268f0900bc3ee613f9f733e82aaed43)
</pre>

<p>
  You can also run <code>git stash pop</code> to apply the stash and then immediately drop it from your stack.
</p>

<h3>Un-applying a Stash</h3>

<p>
  In some use case scenarios you might want to apply stashed changes, do some
  work, but then un-apply those changes that originally came from the stash.
  Git does not provide such a <code>stash unapply</code> command, but it is possible to achieve the effect by simply retrieving the
  patch associated with a stash and applying it in reverse:
</p>

<pre>
$ git stash show -p stash@{0} | git apply -R
</pre>

<p>Again, if you don’t specify a stash, Git assumes the most recent stash:</p>

<pre>
$ git stash show -p | git apply -R
</pre>

<p>
  You may want to create an alias and effectively add a <code>stash-unapply</code> command to your Git. For example:
</p>

<pre>
$ git config --global alias.stash-unapply '!git stash show -p | git apply -R'
$ git stash apply
$ #... work work work
$ git stash-unapply
</pre>

<h3>Creating a Branch from a Stash</h3>

<p>
  If you stash some work, leave it there for a while, and continue on the
  branch from which you stashed the work, you may have a problem reapplying the
  work. If the apply tries to modify a file that you’ve since modified, you’ll
  get a merge conflict and will have to try to resolve it. If you want an
  easier way to test the stashed changes again, you can run <code>git stash branch</code>, which creates a new branch for you, checks out the commit you were on when
  you stashed your work, reapplies your work there, and then drops the stash if
  it applies successfully:
</p>

<pre>
$ git stash branch testchanges
Switched to a new branch "testchanges"
# On branch testchanges
# Changes to be committed:
#   (use "git reset HEAD &lt;file&gt;..." to unstage)
#
#      modified:   index.html
#
# Changes not staged for commit:
#   (use "git add &lt;file&gt;..." to update what will be committed)
#
#      modified:   lib/simplegit.rb
#
Dropped refs/stash@{0} (f0dfc4d5dc332d1cee34a634182e168c4efc3359)
</pre>

<p>
  This is a nice shortcut to recover stashed work easily and work on it in a
  new branch.
</p>

<h2>Rewriting History</h2>

<p>
  Many times, when working with Git, you may want to revise your commit history
  for some reason. One of the great things about Git is that it allows you to
  make decisions at the last possible moment. You can decide what files go into
  which commits right before you commit with the staging area, you can decide
  that you didn’t mean to be working on something yet with the stash command,
  and you can rewrite commits that already happened so they look like they
  happened in a different way. This can involve changing the order of the
  commits, changing messages or modifying files in a commit, squashing together
  or splitting apart commits, or removing commits entirely — all before you
  share your work with others.
</p>

<p>
  In this section, you’ll cover how to accomplish these very useful tasks so
  that you can make your commit history look the way you want before you share
  it with others.
</p>

<h3>Changing the Last Commit</h3>

<p>
  Changing your last commit is probably the most common rewriting of history
  that you’ll do. You’ll often want to do two basic things to your last commit:
  change the commit message, or change the snapshot you just recorded by
  adding, changing and removing files.
</p>

<p>If you only want to modify your last commit message, it’s very simple:</p>

<pre>
$ git commit --amend
</pre>

<p>
  That drops you into your text editor, which has your last commit message in
  it, ready for you to modify the message. When you save and close the editor,
  the editor writes a new commit containing that message and makes it your new
  last commit.
</p>

<p>
  If you’ve committed and then you want to change the snapshot you committed by
  adding or changing files, possibly because you forgot to add a newly created
  file when you originally committed, the process works basically the same way.
  You stage the changes you want by editing a file and running <code>git add</code> on it or <code>git rm</code> to a tracked file, and the subsequent <code>git commit --amend</code> takes your current staging area and makes it the snapshot for the new
  commit.
</p>

<p>
  You need to be careful with this technique because amending changes the SHA-1
  of the commit. It’s like a very small rebase — don’t amend your last commit
  if you’ve already pushed it.
</p>

<h3>Changing Multiple Commit Messages</h3>

<p>
  To modify a commit that is farther back in your history, you must move to
  more complex tools. Git doesn’t have a modify-history tool, but you can use
  the rebase tool to rebase a series of commits onto the HEAD they were
  originally based on instead of moving them to another one. With the
  interactive rebase tool, you can then stop after each commit you want to
  modify and change the message, add files, or do whatever you wish. You can
  run rebase interactively by adding the <code>-i</code> option to <code>git rebase</code>. You must indicate how far back you want to rewrite commits by telling the
  command which commit to rebase onto.
</p>

<p>
  For example, if you want to change the last three commit messages, or any of
  the commit messages in that group, you supply as an argument to <code>git rebase -i</code> the parent of the last commit you want to edit, which is <code>HEAD~2^</code> or <code>HEAD~3</code>. It may be easier to remember the <code>~3</code> because you’re trying to edit the last three commits; but keep in mind that
  you’re actually designating four commits ago, the parent of the last commit
  you want to edit:
</p>

<pre>
$ git rebase -i HEAD~3
</pre>

<p>
  Remember again that this is a rebasing command — every commit included in the
  range <code>HEAD~3..HEAD</code> will be rewritten, whether you change the message or not. Don’t include any
  commit you’ve already pushed to a central server — doing so will confuse
  other developers by providing an alternate version of the same change.
</p>

<p>
  Running this command gives you a list of commits in your text editor that
  looks something like this:
</p>

<pre>
pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

# Rebase 710f0f8..a5f4a0d onto 710f0f8
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
</pre>

<p>
  It’s important to note that these commits are listed in the opposite order
  than you normally see them using the <code>log</code> command. If you run a <code>log</code>, you see something like this:
</p>

<pre>
$ git log --pretty=format:"%h %s" HEAD~3..HEAD
a5f4a0d added cat-file
310154e updated README formatting and added blame
f7f3f6d changed my name a bit
</pre>

<p>
  Notice the reverse order. The interactive rebase gives you a script that it’s
  going to run. It will start at the commit you specify on the command line (<code>HEAD~3</code>) and replay the changes introduced in each of these commits from top to
  bottom. It lists the oldest at the top, rather than the newest, because
  that’s the first one it will replay.
</p>

<p>
  You need to edit the script so that it stops at the commit you want to edit.
  To do so, change the word pick to the word edit for each of the commits you
  want the script to stop after. For example, to modify only the third commit
  message, you change the file to look like this:
</p>

<pre>
edit f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file
</pre>

<p>
  When you save and exit the editor, Git rewinds you back to the last commit in
  that list and drops you on the command line with the following message:
</p>

<pre>
$ git rebase -i HEAD~3
Stopped at 7482e0d... updated the gemspec to hopefully work better
You can amend the commit now, with

       git commit --amend

Once you’re satisfied with your changes, run

       git rebase --continue
</pre>

<p>These instructions tell you exactly what to do. Type</p>

<pre>
$ git commit --amend
</pre>

<p>Change the commit message, and exit the editor. Then, run</p>

<pre>
$ git rebase --continue
</pre>

<p>
  This command will apply the other two commits automatically, and then you’re
  done. If you change pick to edit on more lines, you can repeat these steps
  for each commit you change to edit. Each time, Git will stop, let you amend
  the commit, and continue when you’re finished.
</p>

<h3>Reordering Commits</h3>

<p>
  You can also use interactive rebases to reorder or remove commits entirely.
  If you want to remove the "added cat-file" commit and change the order in
  which the other two commits are introduced, you can change the rebase script
  from this
</p>

<pre>
pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file
</pre>

<p>to this:</p>

<pre>
pick 310154e updated README formatting and added blame
pick f7f3f6d changed my name a bit
</pre>

<p>
  When you save and exit the editor, Git rewinds your branch to the parent of
  these commits, applies <code>310154e</code> and then <code>f7f3f6d</code>, and then stops. You effectively change the order of those commits and
  remove the "added cat-file" commit completely.
</p>

<h3>Squashing Commits</h3>

<p>
  It’s also possible to take a series of commits and squash them down into a
  single commit with the interactive rebasing tool. The script puts helpful
  instructions in the rebase message:
</p>

<pre>
#
# Commands:
#  p, pick = use commit
#  r, reword = use commit, but edit the commit message
#  e, edit = use commit, but stop for amending
#  s, squash = use commit, but meld into previous commit
#  f, fixup = like "squash", but discard this commit's log message
#  x, exec = run command (the rest of the line) using shell
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
</pre>

<p>
  If, instead of "pick" or "edit", you specify "squash", Git applies both that
  change and the change directly before it and makes you merge the commit
  messages together. So, if you want to make a single commit from these three
  commits, you make the script look like this:
</p>

<pre>
pick f7f3f6d changed my name a bit
squash 310154e updated README formatting and added blame
squash a5f4a0d added cat-file
</pre>

<p>
  When you save and exit the editor, Git applies all three changes and then
  puts you back into the editor to merge the three commit messages:
</p>

<pre>
# This is a combination of 3 commits.
# The first commit's message is:
changed my name a bit

# This is the 2nd commit message:

updated README formatting and added blame

# This is the 3rd commit message:

added cat-file
</pre>

<p>
  When you save that, you have a single commit that introduces the changes of
  all three previous commits.
</p>

<h3>Splitting a Commit</h3>

<p>
  Splitting a commit undoes a commit and then partially stages and commits as
  many times as commits you want to end up with. For example, suppose you want
  to split the middle commit of your three commits. Instead of "updated README
  formatting and added blame", you want to split it into two commits: "updated
  README formatting" for the first, and "added blame" for the second. You can
  do that in the <code>rebase -i</code> script by changing the instruction on the commit you want to split to
  "edit":
</p>

<pre>
pick f7f3f6d changed my name a bit
edit 310154e updated README formatting and added blame
pick a5f4a0d added cat-file
</pre>

<p>
  When you save and exit the editor, Git rewinds to the parent of the first
  commit in your list, applies the first commit (<code>f7f3f6d</code>), applies the second (<code>310154e</code>), and drops you to the console. There, you can do a mixed reset of that
  commit with <code>git reset HEAD^</code>, which effectively undoes that commit and leaves the modified files
  unstaged. Now you can take the changes that have been reset, and create
  multiple commits out of them. Simply stage and commit files until you have
  several commits, and run <code>git rebase --continue</code> when you’re done:
</p>

<pre>
$ git reset HEAD^
$ git add README
$ git commit -m 'updated README formatting'
$ git add lib/simplegit.rb
$ git commit -m 'added blame'
$ git rebase --continue
</pre>

<p>
  Git applies the last commit (<code>a5f4a0d</code>) in the script, and your history looks like this:
</p>

<pre>
$ git log -4 --pretty=format:"%h %s"
1c002dd added cat-file
9b29157 added blame
35cfb2b updated README formatting
f3cc40e changed my name a bit
</pre>

<p>
  Once again, this changes the SHAs of all the commits in your list, so make
  sure no commit shows up in that list that you’ve already pushed to a shared
  repository.
</p>

<h3>The Nuclear Option: filter-branch</h3>

<p>
  There is another history-rewriting option that you can use if you need to
  rewrite a larger number of commits in some scriptable way — for instance,
  changing your e-mail address globally or removing a file from every
  commit.&emsp14;&emsp14;The command is <code>filter-branch</code>, and it can rewrite huge swaths of your history, so you probably shouldn’t
  use it unless your project isn’t yet public and other people haven’t based
  work off the commits you’re about to rewrite.&emsp14;&emsp14;However, it can
  be very useful. You’ll learn a few of the common uses so you can get an idea
  of some of the things it’s capable of.
</p>

<p>Removing a File from Every Commit</p>

<p>
  This occurs fairly commonly. Someone accidentally commits a huge binary file
  with a thoughtless <code>git add .</code>, and you want to remove it everywhere. Perhaps you accidentally committed a
  file that contained a password, and you want to make your project open
  source. <code>filter-branch</code> is the tool you probably want to use to scrub your entire history. To remove
  a file named passwords.txt from your entire history, you can use the <code>--tree-filter</code> option to <code>filter-branch</code>:
</p>

<pre>
$ git filter-branch --tree-filter 'rm -f passwords.txt' HEAD
Rewrite 6b9b3cf04e7c5686a9cb838c3f36a8cb6a0fc2bd (21/21)
Ref 'refs/heads/master' was rewritten
</pre>

<p>
  The <code>--tree-filter</code> option runs the specified command after each checkout of the project and
  then recommits the results. In this case, you remove a file called
  passwords.txt from every snapshot, whether it exists or not. If you want to
  remove all accidentally committed editor backup files, you can run something
  like <code>git filter-branch --tree-filter "rm -f *~" HEAD</code>.
</p>

<p>
  You’ll be able to watch Git rewriting trees and commits and then move the
  branch pointer at the end. It’s generally a good idea to do this in a testing
  branch and then hard-reset your master branch after you’ve determined the
  outcome is what you really want. To run <code>filter-branch</code> on all your branches, you can pass <code>--all</code> to the command.
</p>

<p>Making a Subdirectory the New Root</p>

<p>
  Suppose you’ve done an import from another source control system and have
  subdirectories that make no sense (trunk, tags, and so on). If you want to
  make the <code>trunk</code> subdirectory be the new project root for every commit, <code>filter-branch</code> can help you do that, too:
</p>

<pre>
$ git filter-branch --subdirectory-filter trunk HEAD
Rewrite 856f0bf61e41a27326cdae8f09fe708d679f596f (12/12)
Ref 'refs/heads/master' was rewritten
</pre>

<p>
  Now your new project root is what was in the <code>trunk</code> subdirectory each time. Git will also automatically remove commits that did
  not affect the subdirectory.
</p>

<p>Changing E-Mail Addresses Globally</p>

<p>
  Another common case is that you forgot to run <code>git config</code> to set your name and e-mail address before you started working, or perhaps
  you want to open-source a project at work and change all your work e-mail
  addresses to your personal address. In any case, you can change e-mail
  addresses in multiple commits in a batch with <code>filter-branch</code> as well. You need to be careful to change only the e-mail addresses that are
  yours, so you use <code>--commit-filter</code>:
</p>

<pre>
$ git filter-branch --commit-filter '
        if [ "$GIT_AUTHOR_EMAIL" = "schacon@localhost" ];
        then
                GIT_AUTHOR_NAME="Scott Chacon";
                GIT_AUTHOR_EMAIL="schacon@example.com";
                git commit-tree "$@";
        else
                git commit-tree "$@";
        fi' HEAD
</pre>

<p>
  This goes through and rewrites every commit to have your new address. Because
  commits contain the SHA-1 values of their parents, this command changes every
  commit SHA in your history, not just those that have the matching e-mail
  address.
</p>

<h3>The Very Fast Nuclear Option: Big Friendly Giant Repo Cleaner (BFG)</h3>

<p>
  <a href="https://github.com/rtyley">Roberto Tyley</a> has written a similar tool to <code>filter-branch</code> called the BFG. BFG cannot do as much as <code>filter-branch</code>, but it is <i>very</i> fast and on a large repository this can make a big difference. If the change
  you want to make is in the scope of BFG capability, and you have performance
  issues, then you should consider using it.
</p>

<p>
  See the <a href="http://rtyley.github.io/bfg-repo-cleaner/">BFG</a> website for details.
</p>

<h2>Debugging with Git</h2>

<p>
  Git also provides a couple of tools to help you debug issues in your
  projects. Because Git is designed to work with nearly any type of project,
  these tools are pretty generic, but they can often help you hunt for a bug or
  culprit when things go wrong.
</p>

<h3>File Annotation</h3>

<p>
  If you track down a bug in your code and want to know when it was introduced
  and why, file annotation is often your best tool. It shows you what commit
  was the last to modify each line of any file. So, if you see that a method in
  your code is buggy, you can annotate the file with <code>git blame</code> to see when each line of the method was last edited and by whom. This
  example uses the <code>-L</code> option to limit the output to lines 12 through 22:
</p>

<pre>
$ git blame -L 12,22 simplegit.rb
^4832fe2 (Scott Chacon  2008-03-15 10:31:28 -0700 12)  def show(tree = 'master')
^4832fe2 (Scott Chacon  2008-03-15 10:31:28 -0700 13)   command("git show #{tree}")
^4832fe2 (Scott Chacon  2008-03-15 10:31:28 -0700 14)  end
^4832fe2 (Scott Chacon  2008-03-15 10:31:28 -0700 15)
9f6560e4 (Scott Chacon  2008-03-17 21:52:20 -0700 16)  def log(tree = 'master')
79eaf55d (Scott Chacon  2008-04-06 10:15:08 -0700 17)   command("git log #{tree}")
9f6560e4 (Scott Chacon  2008-03-17 21:52:20 -0700 18)  end
9f6560e4 (Scott Chacon  2008-03-17 21:52:20 -0700 19)
42cf2861 (Magnus Chacon 2008-04-13 10:45:01 -0700 20)  def blame(path)
42cf2861 (Magnus Chacon 2008-04-13 10:45:01 -0700 21)   command("git blame #{path}")
42cf2861 (Magnus Chacon 2008-04-13 10:45:01 -0700 22)  end
</pre>

<p>
  Notice that the first field is the partial SHA-1 of the commit that last
  modified that line. The next two fields are values extracted from that
  commit—the author name and the authored date of that commit — so you can
  easily see who modified that line and when. After that come the line number
  and the content of the file. Also note the <code>^4832fe2</code> commit lines, which designate that those lines were in this file’s original
  commit. That commit is when this file was first added to this project, and
  those lines have been unchanged since. This is a tad confusing, because now
  you’ve seen at least three different ways that Git uses the <code>^</code> to modify a commit SHA, but that is what it means here.
</p>

<p>
  Another cool thing about Git is that it doesn’t track file renames
  explicitly. It records the snapshots and then tries to figure out what was
  renamed implicitly, after the fact. One of the interesting features of this
  is that you can ask it to figure out all sorts of code movement as well. If
  you pass <code>-C</code> to <code>git blame</code>, Git analyzes the file you’re annotating and tries to figure out where
  snippets of code within it originally came from if they were copied from
  elsewhere. Recently, I was refactoring a file named <code>GITServerHandler.m</code> into multiple files, one of which was <code>GITPackUpload.m</code>. By blaming <code>GITPackUpload.m</code> with the <code>-C</code> option, I could see where sections of the code originally came from:
</p>

<pre>
$ git blame -C -L 141,153 GITPackUpload.m
f344f58d GITServerHandler.m (Scott 2009-01-04 141)
f344f58d GITServerHandler.m (Scott 2009-01-04 142) - (void) gatherObjectShasFromC
f344f58d GITServerHandler.m (Scott 2009-01-04 143) {
70befddd GITServerHandler.m (Scott 2009-03-22 144)         //NSLog(@"GATHER COMMI
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 145)
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 146)         NSString *parentSha;
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 147)         GITCommit *commit = [g
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 148)
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 149)         //NSLog(@"GATHER COMMI
ad11ac80 GITPackUpload.m    (Scott 2009-03-24 150)
56ef2caf GITServerHandler.m (Scott 2009-01-05 151)         if(commit) {
56ef2caf GITServerHandler.m (Scott 2009-01-05 152)                 [refDict setOb
56ef2caf GITServerHandler.m (Scott 2009-01-05 153)
</pre>

<p>
  This is really useful. Normally, you get as the original commit the commit
  where you copied the code over, because that is the first time you touched
  those lines in this file. Git tells you the original commit where you wrote
  those lines, even if it was in another file.
</p>

<h3>Binary Search</h3>

<p>
  Annotating a file helps if you know where the issue is to begin with. If you
  don’t know what is breaking, and there have been dozens or hundreds of
  commits since the last state where you know the code worked, you’ll likely
  turn to <code>git bisect</code> for help. The <code>bisect</code> command does a binary search through your commit history to help you
  identify as quickly as possible which commit introduced an issue.
</p>

<p>
  Let’s say you just pushed out a release of your code to a production
  environment, you’re getting bug reports about something that wasn’t happening
  in your development environment, and you can’t imagine why the code is doing
  that. You go back to your code, and it turns out you can reproduce the issue,
  but you can’t figure out what is going wrong. You can bisect the code to find
  out. First you run <code>git bisect start</code> to get things going, and then you use <code>git bisect bad</code> to tell the system that the current commit you’re on is broken. Then, you
  must tell bisect when the last known good state was, using <code>git bisect good [good_commit]</code>:
</p>

<pre>
$ git bisect start
$ git bisect bad
$ git bisect good v1.0
Bisecting: 6 revisions left to test after this
[ecb6e1bc347ccecc5f9350d878ce677feb13d3b2] error handling on repo
</pre>

<p>
  Git figured out that about 12 commits came between the commit you marked as
  the last good commit (v1.0) and the current bad version, and it checked out
  the middle one for you. At this point, you can run your test to see if the
  issue exists as of this commit. If it does, then it was introduced sometime
  before this middle commit; if it doesn’t, then the problem was introduced
  sometime after the middle commit. It turns out there is no issue here, and
  you tell Git that by typing <code>git bisect good</code> and continue your journey:
</p>

<pre>
$ git bisect good
Bisecting: 3 revisions left to test after this
[b047b02ea83310a70fd603dc8cd7a6cd13d15c04] secure this thing
</pre>

<p>
  Now you’re on another commit, halfway between the one you just tested and
  your bad commit. You run your test again and find that this commit is broken,
  so you tell Git that with <code>git bisect bad</code>:
</p>

<pre>
$ git bisect bad
Bisecting: 1 revisions left to test after this
[f71ce38690acf49c1f3c9bea38e09d82a5ce6014] drop exceptions table
</pre>

<p>
  This commit is fine, and now Git has all the information it needs to
  determine where the issue was introduced. It tells you the SHA-1 of the first
  bad commit and show some of the commit information and which files were
  modified in that commit so you can figure out what happened that may have
  introduced this bug:
</p>

<pre>
$ git bisect good
b047b02ea83310a70fd603dc8cd7a6cd13d15c04 is first bad commit
commit b047b02ea83310a70fd603dc8cd7a6cd13d15c04
Author: PJ Hyett &lt;pjhyett@example.com&gt;
Date:   Tue Jan 27 14:48:32 2009 -0800

    secure this thing

:040000 040000 40ee3e7821b895e52c1695092db9bdc4c61d1730
f24d3c6ebcfc639b1a3814550e62d60b8e68a8e4 M  config
</pre>

<p>
  When you’re finished, you should run <code>git bisect reset</code> to reset your HEAD to where you were before you started, or you’ll end up in
  a weird state:
</p>

<pre>
$ git bisect reset
</pre>

<p>
  This is a powerful tool that can help you check hundreds of commits for an
  introduced bug in minutes. In fact, if you have a script that will exit 0 if
  the project is good or non-0 if the project is bad, you can fully automate <code>git bisect</code>. First, you again tell it the scope of the bisect by providing the known bad
  and good commits. You can do this by listing them with the <code>bisect start</code> command if you want, listing the known bad commit first and the known good
  commit second:
</p>

<pre>
$ git bisect start HEAD v1.0
$ git bisect run test-error.sh
</pre>

<p>
  Doing so automatically runs <code>test-error.sh</code> on each checked-out commit until Git finds the first broken commit. You can
  also run something like <code>make</code> or <code>make tests</code> or whatever you have that runs automated tests for you.
</p>

<h2>Submodules</h2>

<p>
  It often happens that while working on one project, you need to use another
  project from within it. Perhaps it’s a library that a third party developed
  or that you’re developing separately and using in multiple parent projects. A
  common issue arises in these scenarios: you want to be able to treat the two
  projects as separate yet still be able to use one from within the other.
</p>

<p>
  Here’s an example. Suppose you’re developing a web site and creating Atom
  feeds. Instead of writing your own Atom-generating code, you decide to use a
  library. You’re likely to have to either include this code from a shared
  library like a CPAN install or Ruby gem, or copy the source code into your
  own project tree. The issue with including the library is that it’s difficult
  to customize the library in any way and often more difficult to deploy it,
  because you need to make sure every client has that library available. The
  issue with vendoring the code into your own project is that any custom
  changes you make are difficult to merge when upstream changes become
  available.
</p>

<p>
  Git addresses this issue using submodules. Submodules allow you to keep a Git
  repository as a subdirectory of another Git repository. This lets you clone
  another repository into your project and keep your commits separate.
</p>

<h3>Starting with Submodules</h3>

<p>
  Suppose you want to add the Rack library (a Ruby web server gateway
  interface) to your project, possibly maintain your own changes to it, but
  continue to merge in upstream changes. The first thing you should do is clone
  the external repository into your subdirectory. You add external projects as
  submodules with the <code>git submodule add</code> command:
</p>

<pre>
$ git submodule add git://github.com/chneukirchen/rack.git rack
Initialized empty Git repository in /opt/subtest/rack/.git/
remote: Counting objects: 3181, done.
remote: Compressing objects: 100% (1534/1534), done.
remote: Total 3181 (delta 1951), reused 2623 (delta 1603)
Receiving objects: 100% (3181/3181), 675.42 KiB | 422 KiB/s, done.
Resolving deltas: 100% (1951/1951), done.
</pre>

<p>
  Now you have the Rack project under a subdirectory named <code>rack</code> within your project. You can go into that subdirectory, make changes, add
  your own writable remote repository to push your changes into, fetch and
  merge from the original repository, and more. If you run <code>git status</code> right after you add the submodule, you see two things:
</p>

<pre>
$ git status
# On branch master
# Changes to be committed:
#   (use "git reset HEAD &lt;file&gt;..." to unstage)
#
#      new file:   .gitmodules
#      new file:   rack
#
</pre>

<p>
  First you notice the <code>.gitmodules</code> file. This is a configuration file that stores the mapping between the
  project’s URL and the local subdirectory you’ve pulled it into:
</p>

<pre>
$ cat .gitmodules
[submodule "rack"]
      path = rack
      url = git://github.com/chneukirchen/rack.git
</pre>

<p>
  If you have multiple submodules, you’ll have multiple entries in this file.
  It’s important to note that this file is version-controlled with your other
  files, like your <code>.gitignore</code> file. It’s pushed and pulled with the rest of your project. This is how
  other people who clone this project know where to get the submodule projects
  from.
</p>

<p>
  The other listing in the <code>git status</code> output is the rack entry. If you run <code>git diff</code> on that, you see something interesting:
</p>

<pre>
$ git diff --cached rack
diff --git a/rack b/rack
new file mode 160000
index 0000000..08d709f
--- /dev/null
+++ b/rack
@@ -0,0 +1 @@
+Subproject commit 08d709f78b8c5b0fbeb7821e37fa53e69afcf433
</pre>

<p>
  Although <code>rack</code> is a subdirectory in your working directory, Git sees it as a submodule and
  doesn’t track its contents when you’re not in that directory. Instead, Git
  records it as a particular commit from that repository. When you make changes
  and commit in that subdirectory, the superproject notices that the HEAD there
  has changed and records the exact commit you’re currently working off of;
  that way, when others clone this project, they can re-create the environment
  exactly.
</p>

<p>
  This is an important point with submodules: you record them as the exact
  commit they’re at. You can’t record a submodule at <code>master</code> or some other symbolic reference.
</p>

<p>When you commit, you see something like this:</p>

<pre>
$ git commit -m 'first commit with submodule rack'
[master 0550271] first commit with submodule rack
 2 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 rack
</pre>

<p>
  Notice the 160000 mode for the rack entry. That is a special mode in Git that
  basically means you’re recording a commit as a directory entry rather than a
  subdirectory or a file.
</p>

<p>
  You can treat the <code>rack</code> directory as a separate project and then update your superproject from time
  to time with a pointer to the latest commit in that subproject. All the Git
  commands work independently in the two directories:
</p>

<pre>
$ git log -1
commit 0550271328a0038865aad6331e620cd7238601bb
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Thu Apr 9 09:03:56 2009 -0700

    first commit with submodule rack
$ cd rack/
$ git log -1
commit 08d709f78b8c5b0fbeb7821e37fa53e69afcf433
Author: Christian Neukirchen &lt;chneukirchen@gmail.com&gt;
Date:   Wed Mar 25 14:49:04 2009 +0100

    Document version change
</pre>

<h3>Cloning a Project with Submodules</h3>

<p>
  Here you’ll clone a project with a submodule in it. When you receive such a
  project, you get the directories that contain submodules, but none of the
  files yet:
</p>

<pre>
$ git clone git://github.com/schacon/myproject.git
Initialized empty Git repository in /opt/myproject/.git/
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
$ cd myproject
$ ls -l
total 8
-rw-r--r--  1 schacon  admin   3 Apr  9 09:11 README
drwxr-xr-x  2 schacon  admin  68 Apr  9 09:11 rack
$ ls rack/
$
</pre>

<p>
  The <code>rack</code> directory is there, but empty. You must run two commands: <code>git submodule init</code> to initialize your local configuration file, and <code>git submodule update</code> to fetch all the data from that project and check out the appropriate commit
  listed in your superproject:
</p>

<pre>
$ git submodule init
Submodule 'rack' (git://github.com/chneukirchen/rack.git) registered for path 'rack'
$ git submodule update
Initialized empty Git repository in /opt/myproject/rack/.git/
remote: Counting objects: 3181, done.
remote: Compressing objects: 100% (1534/1534), done.
remote: Total 3181 (delta 1951), reused 2623 (delta 1603)
Receiving objects: 100% (3181/3181), 675.42 KiB | 173 KiB/s, done.
Resolving deltas: 100% (1951/1951), done.
Submodule path 'rack': checked out '08d709f78b8c5b0fbeb7821e37fa53e69afcf433'
</pre>

<p>
  Now your <code>rack</code> subdirectory is at the exact state it was in when you committed earlier. If
  another developer makes changes to the rack code and commits, and you pull
  that reference down and merge it in, you get something a bit odd:
</p>

<pre>
$ git merge origin/master
Updating 0550271..85a3eee
Fast forward
 rack |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
[master*]$ git status
# On branch master
# Changes not staged for commit:
#   (use "git add &lt;file&gt;..." to update what will be committed)
#   (use "git checkout -- &lt;file&gt;..." to discard changes in working directory)
#
#      modified:   rack
#
</pre>

<p>
  You merged in what is basically a change to the pointer for your submodule;
  but it doesn’t update the code in the submodule directory, so it looks like
  you have a dirty state in your working directory:
</p>

<pre>
$ git diff
diff --git a/rack b/rack
index 6c5e70b..08d709f 160000
--- a/rack
+++ b/rack
@@ -1 +1 @@
-Subproject commit 6c5e70b984a60b3cecd395edd5b48a7575bf58e0
+Subproject commit 08d709f78b8c5b0fbeb7821e37fa53e69afcf433
</pre>

<p>
  This is the case because the pointer you have for the submodule isn’t what is
  actually in the submodule directory. To fix this, you must run <code>git submodule update</code> again:
</p>

<pre>
$ git submodule update
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 2 (delta 0)
Unpacking objects: 100% (3/3), done.
From git@github.com:schacon/rack
   08d709f..6c5e70b  master     -&gt; origin/master
Submodule path 'rack': checked out '6c5e70b984a60b3cecd395edd5b48a7575bf58e0'
</pre>

<p>
  You have to do this every time you pull down a submodule change in the main
  project. It’s strange, but it works.
</p>

<p>
  One common problem happens when a developer makes a change locally in a
  submodule but doesn’t push it to a public server. Then, they commit a pointer
  to that non-public state and push up the superproject. When other developers
  try to run <code>git submodule update</code>, the submodule system can’t find the commit that is referenced, because it
  exists only on the first developer’s system. If that happens, you see an
  error like this:
</p>

<pre>
$ git submodule update
fatal: reference isn’t a tree: 6c5e70b984a60b3cecd395edd5b48a7575bf58e0
Unable to checkout '6c5e70b984a60b3cecd395edd5ba7575bf58e0' in submodule path 'rack'
</pre>

<p>You have to see who last changed the submodule:</p>

<pre>
$ git log -1 rack
commit 85a3eee996800fcfa91e2119372dd4172bf76678
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Thu Apr 9 09:19:14 2009 -0700

    added a submodule reference I will never make public. hahahahaha!
</pre>

<p>Then, you e-mail that guy and yell at him.</p>

<h3>Superprojects</h3>

<p>
  Sometimes, developers want to get a combination of a large project’s
  subdirectories, depending on what team they’re on. This is common if you’re
  coming from CVS or Subversion, where you’ve defined a module or collection of
  subdirectories, and you want to keep this type of workflow.
</p>

<p>
  A good way to do this in Git is to make each of the subdirectories a separate
  Git repository and then create superproject Git repositories that contain
  multiple submodules. A benefit of this approach is that you can more
  specifically define the relationships between the projects with tags and
  branches in the superprojects.
</p>

<h3>Issues with Submodules</h3>

<p>
  Using submodules isn’t without hiccups, however. First, you must be
  relatively careful when working in the submodule directory. When you run <code>git submodule update</code>, it checks out the specific version of the project, but not within a branch.
  This is called having a detached HEAD — it means the HEAD file points
  directly to a commit, not to a symbolic reference. The issue is that you
  generally don’t want to work in a detached HEAD environment, because it’s
  easy to lose changes. If you do an initial <code>submodule update</code>, commit in that submodule directory without creating a branch to work in,
  and then run <code>git submodule update</code> again from the superproject without committing in the meantime, Git will
  overwrite your changes without telling you.&emsp14;&emsp14;Technically you
  won’t lose the work, but you won’t have a branch pointing to it, so it will
  be somewhat difficult to retrieve.
</p>

<p>
  To avoid this issue, create a branch when you work in a submodule directory
  with <code>git checkout -b work</code> or something equivalent. When you do the submodule update a second time, it
  will still revert your work, but at least you have a pointer to get back to.
</p>

<p>
  Switching branches with submodules in them can also be tricky. If you create
  a new branch, add a submodule there, and then switch back to a branch without
  that submodule, you still have the submodule directory as an untracked
  directory:
</p>

<pre>
$ git checkout -b rack
Switched to a new branch "rack"
$ git submodule add git@github.com:schacon/rack.git rack
Initialized empty Git repository in /opt/myproj/rack/.git/
...
Receiving objects: 100% (3184/3184), 677.42 KiB | 34 KiB/s, done.
Resolving deltas: 100% (1952/1952), done.
$ git commit -am 'added rack submodule'
[rack cc49a69] added rack submodule
 2 files changed, 4 insertions(+), 0 deletions(-)
 create mode 100644 .gitmodules
 create mode 160000 rack
$ git checkout master
Switched to branch "master"
$ git status
# On branch master
# Untracked files:
#   (use "git add &lt;file&gt;..." to include in what will be committed)
#
#      rack/
</pre>

<p>
  You have to either move it out of the way or remove it, in which case you
  have to clone it again when you switch back—and you may lose local changes or
  branches that you didn’t push up.
</p>

<p>
  The last main caveat that many people run into involves switching from
  subdirectories to submodules. If you’ve been tracking files in your project
  and you want to move them out into a submodule, you must be careful or Git
  will get angry at you. Assume that you have the rack files in a subdirectory
  of your project, and you want to switch it to a submodule. If you delete the
  subdirectory and then run <code>submodule add</code>, Git yells at you:
</p>

<pre>
$ rm -Rf rack/
$ git submodule add git@github.com:schacon/rack.git rack
'rack' already exists in the index
</pre>

<p>
  You have to unstage the <code>rack</code> directory first. Then you can add the submodule:
</p>

<pre>
$ git rm -r rack
$ git submodule add git@github.com:schacon/rack.git rack
Initialized empty Git repository in /opt/testsub/rack/.git/
remote: Counting objects: 3184, done.
remote: Compressing objects: 100% (1465/1465), done.
remote: Total 3184 (delta 1952), reused 2770 (delta 1675)
Receiving objects: 100% (3184/3184), 677.42 KiB | 88 KiB/s, done.
Resolving deltas: 100% (1952/1952), done.
</pre>

<p>
  Now suppose you did that in a branch. If you try to switch back to a branch
  where those files are still in the actual tree rather than a submodule — you
  get this error:
</p>

<pre>
$ git checkout master
error: Untracked working tree file 'rack/AUTHORS' would be overwritten by merge.
</pre>

<p>
  You have to move the <code>rack</code> submodule directory out of the way before you can switch to a branch that
  doesn’t have it:
</p>

<pre>
$ mv rack /tmp/
$ git checkout master
Switched to branch "master"
$ ls
README	rack
</pre>

<p>
  Then, when you switch back, you get an empty <code>rack</code> directory. You can either run <code>git submodule update</code> to reclone, or you can move your <code>/tmp/rack</code> directory back into the empty directory.
</p>

<h2>Subtree Merging</h2>

<p>
  Now that you’ve seen the difficulties of the submodule system, let’s look at
  an alternate way to solve the same problem. When Git merges, it looks at what
  it has to merge together and then chooses an appropriate merging strategy to
  use. If you’re merging two branches, Git uses a <i>recursive</i> strategy. If you’re merging more than two branches, Git picks the <i>octopus</i> strategy. These strategies are automatically chosen for you because the
  recursive strategy can handle complex three-way merge situations — for
  example, more than one common ancestor — but it can only handle merging two
  branches. The octopus merge can handle multiple branches but is more cautious
  to avoid difficult conflicts, so it’s chosen as the default strategy if
  you’re trying to merge more than two branches.
</p>

<p>
  However, there are other strategies you can choose as well. One of them is
  the <i>subtree</i> merge, and you can use it to deal with the subproject issue. Here you’ll see
  how to do the same rack embedding as in the last section, but using subtree
  merges instead.
</p>

<p>
  The idea of the subtree merge is that you have two projects, and one of the
  projects maps to a subdirectory of the other one and vice versa. When you
  specify a subtree merge, Git is smart enough to figure out that one is a
  subtree of the other and merge appropriately — it’s pretty amazing.
</p>

<p>
  You first add the Rack application to your project. You add the Rack project
  as a remote reference in your own project and then check it out into its own
  branch:
</p>

<pre>
$ git remote add rack_remote git@github.com:schacon/rack.git
$ git fetch rack_remote
warning: no common commits
remote: Counting objects: 3184, done.
remote: Compressing objects: 100% (1465/1465), done.
remote: Total 3184 (delta 1952), reused 2770 (delta 1675)
Receiving objects: 100% (3184/3184), 677.42 KiB | 4 KiB/s, done.
Resolving deltas: 100% (1952/1952), done.
From git@github.com:schacon/rack
 * [new branch]      build      -&gt; rack_remote/build
 * [new branch]      master     -&gt; rack_remote/master
 * [new branch]      rack-0.4   -&gt; rack_remote/rack-0.4
 * [new branch]      rack-0.9   -&gt; rack_remote/rack-0.9
$ git checkout -b rack_branch rack_remote/master
Branch rack_branch set up to track remote branch refs/remotes/rack_remote/master.
Switched to a new branch "rack_branch"
</pre>

<p>
  Now you have the root of the Rack project in your <code>rack_branch</code> branch and your own project in the <code>master</code> branch. If you check out one and then the other, you can see that they have
  different project roots:
</p>

<pre>
$ ls
AUTHORS	       KNOWN-ISSUES   Rakefile      contrib	       lib
COPYING	       README         bin           example	       test
$ git checkout master
Switched to branch "master"
$ ls
README
</pre>

<p>
  You want to pull the Rack project into your <code>master</code> project as a subdirectory. You can do that in Git with <code>git read-tree</code>. You’ll learn more about <code>read-tree</code> and its friends in Chapter 9, but for now know that it reads the root tree
  of one branch into your current staging area and working directory. You just
  switched back to your <code>master</code> branch, and you pull the <code>rack_branch</code> branch into the <code>rack</code> subdirectory of your <code>master</code> branch of your main project:
</p>

<pre>
$ git read-tree --prefix=rack/ -u rack_branch
</pre>

<p>
  When you commit, it looks like you have all the Rack files under that
  subdirectory — as though you copied them in from a tarball. What gets
  interesting is that you can fairly easily merge changes from one of the
  branches to the other. So, if the Rack project updates, you can pull in
  upstream changes by switching to that branch and pulling:
</p>

<pre>
$ git checkout rack_branch
$ git pull
</pre>

<p>
  Then, you can merge those changes back into your master branch. You can use <code>git merge -s subtree</code> and it will work fine; but Git will also merge the histories together, which
  you probably don’t want. To pull in the changes and prepopulate the commit
  message, use the <code>--squash</code> and <code>--no-commit</code> options as well as the <code>-s subtree</code> strategy option:
</p>

<pre>
$ git checkout master
$ git merge --squash -s subtree --no-commit rack_branch
Squash commit -- not updating HEAD
Automatic merge went well; stopped before committing as requested
</pre>

<p>
  All the changes from your Rack project are merged in and ready to be
  committed locally. You can also do the opposite — make changes in the <code>rack</code> subdirectory of your master branch and then merge them into your <code>rack_branch</code> branch later to submit them to the maintainers or push them upstream.
</p>

<p>
  To get a diff between what you have in your <code>rack</code> subdirectory and the code in your <code>rack_branch</code> branch — to see if you need to merge them — you can’t use the normal <code>diff</code> command. Instead, you must run <code>git diff-tree</code> with the branch you want to compare to:
</p>

<pre>
$ git diff-tree -p rack_branch
</pre>

<p>
  Or, to compare what is in your <code>rack</code> subdirectory with what the <code>master</code> branch on the server was the last time you fetched, you can run
</p>

<pre>
$ git diff-tree -p rack_remote/master
</pre>

<h2>Summary</h2>

<p>
  You’ve seen a number of advanced tools that allow you to manipulate your
  commits and staging area more precisely. When you notice issues, you should
  be able to easily figure out what commit introduced them, when, and by whom.
  If you want to use subprojects in your project, you’ve learned a few ways to
  accommodate those needs. At this point, you should be able to do most of the
  things in Git that you’ll need on the command line day to day and feel
  comfortable doing so.
</p>

<h1>Customizing Git</h1>

<p>
  So far, I’ve covered the basics of how Git works and how to use it, and I’ve
  introduced a number of tools that Git provides to help you use it easily and
  efficiently. In this chapter, I’ll go through some operations that you can
  use to make Git operate in a more customized fashion by introducing several
  important configuration settings and the hooks system. With these tools, it’s
  easy to get Git to work exactly the way you, your company, or your group
  needs it to.
</p>

<h2>Git Configuration</h2>

<p>
  As you briefly saw in Chapter 1, you can specify Git configuration settings
  with the <code>git config</code> command. One of the first things you did was set up your name and e-mail
  address:
</p>

<pre>
$ git config --global user.name "John Doe"
$ git config --global user.email johndoe@example.com
</pre>

<p>
  Now you’ll learn a few of the more interesting options that you can set in
  this manner to customize your Git usage.
</p>

<p>
  You saw some simple Git configuration details in the first chapter, but I’ll
  go over them again quickly here. Git uses a series of configuration files to
  determine non-default behavior that you may want. The first place Git looks
  for these values is in an <code>/etc/gitconfig</code> file, which contains values for every user on the system and all of their
  repositories. If you pass the option <code>--system</code> to <code>git config</code>, it reads and writes from this file specifically.
</p>

<p>
  The next place Git looks is the <code>~/.gitconfig</code> file, which is specific to each user. You can make Git read and write to
  this file by passing the <code>--global</code> option.
</p>

<p>
  Finally, Git looks for configuration values in the config file in the Git
  directory (<code>.git/config</code>) of whatever repository you’re currently using. These values are specific to
  that single repository. Each level overwrites values in the previous level,
  so values in <code>.git/config</code> trump those in <code>/etc/gitconfig</code>, for instance. You can also set these values by manually editing the file
  and inserting the correct syntax, but it’s generally easier to run the <code>git config</code> command.
</p>

<h3>Basic Client Configuration</h3>

<p>
  The configuration options recognized by Git fall into two categories: client
  side and server side. The majority of the options are client side—configuring
  your personal working preferences. Although tons of options are available,
  I’ll only cover the few that either are commonly used or can significantly
  affect your workflow. Many options are useful only in edge cases that I won’t
  go over here. If you want to see a list of all the options your version of
  Git recognizes, you can run
</p>

<pre>
$ git config --help
</pre>

<p>
  The manual page for <code>git config</code> lists all the available options in quite a bit of detail.
</p>

<p>core.editor</p>

<p>
  By default, Git uses whatever you’ve set as your default text editor or else
  falls back to the Vi editor to create and edit your commit and tag messages.
  To change that default to something else, you can use the <code>core.editor</code> setting:
</p>

<pre>
$ git config --global core.editor emacs
</pre>

<p>
  Now, no matter what is set as your default shell editor variable, Git will
  fire up Emacs to edit messages.
</p>

<p>commit.template</p>

<p>
  If you set this to the path of a file on your system, Git will use that file
  as the default message when you commit. For instance, suppose you create a
  template file at <code>$HOME/.gitmessage.txt</code> that looks like this:
</p>

<pre>
subject line

what happened

[ticket: X]
</pre>

<p>
  To tell Git to use it as the default message that appears in your editor when
  you run <code>git commit</code>, set the <code>commit.template</code> configuration value:
</p>

<pre>
$ git config --global commit.template $HOME/.gitmessage.txt
$ git commit
</pre>

<p>
  Then, your editor will open to something like this for your placeholder
  commit message when you commit:
</p>

<pre>
subject line

what happened

[ticket: X]
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch master
# Changes to be committed:
#   (use "git reset HEAD &lt;file&gt;..." to unstage)
#
# modified:   lib/test.rb
#
~
~
".git/COMMIT_EDITMSG" 14L, 297C
</pre>

<p>
  If you have a commit-message policy in place, then putting a template for
  that policy on your system and configuring Git to use it by default can help
  increase the chance of that policy being followed regularly.
</p>

<p>core.pager</p>

<p>
  The core.pager setting determines what pager is used when Git pages output
  such as <code>log</code> and <code>diff</code>. You can set it to <code>more</code> or to your favorite pager (by default, it’s <code>less</code>), or you can turn it off by setting it to a blank string:
</p>

<pre>
$ git config --global core.pager ''
</pre>

<p>
  If you run that, Git will page the entire output of all commands, no matter
  how long it is.
</p>

<p>user.signingkey</p>

<p>
  If you’re making signed annotated tags (as discussed in Chapter 2), setting
  your GPG signing key as a configuration setting makes things easier. Set your
  key ID like so:
</p>

<pre>
$ git config --global user.signingkey &lt;gpg-key-id&gt;
</pre>

<p>
  Now, you can sign tags without having to specify your key every time with the
  <code>git tag</code> command:
</p>

<pre>
$ git tag -s &lt;tag-name&gt;
</pre>

<p>core.excludesfile</p>

<p>
  You can put patterns in your project’s <code>.gitignore</code> file to have Git not see them as untracked files or try to stage them when
  you run <code>git add</code> on them, as discussed in Chapter 2. However, if you want another file
  outside of your project to hold those values or have extra values, you can
  tell Git where that file is with the <code>core.excludesfile</code> setting. Simply set it to the path of a file that has content similar to
  what a <code>.gitignore</code> file would have.
</p>

<p>help.autocorrect</p>

<p>
  This option is available only in Git 1.6.1 and later. If you mistype a
  command in Git, it shows you something like this:
</p>

<pre>
$ git com
git: 'com' is not a git-command. See 'git --help'.

Did you mean this?
     commit
</pre>

<p>
  If you set <code>help.autocorrect</code> to 1, Git will automatically run the command if it has only one match under
  this scenario.
</p>

<h3>Colors in Git</h3>

<p>
  Git can color its output to your terminal, which can help you visually parse
  the output quickly and easily. A number of options can help you set the
  coloring to your preference.
</p>

<p>color.ui</p>

<p>
  Git automatically colors most of its output if you ask it to. You can get
  very specific about what you want colored and how; but to turn on all the
  default terminal coloring, set <code>color.ui</code> to true:
</p>

<pre>
$ git config --global color.ui true
</pre>

<p>
  When that value is set, Git colors its output if the output goes to a
  terminal. Other possible settings are false, which never colors the output,
  and always, which sets colors all the time, even if you’re redirecting Git
  commands to a file or piping them to another command.
</p>

<p>
  You’ll rarely want <code>color.ui = always</code>. In most scenarios, if you want color codes in your redirected output, you
  can instead pass a <code>--color</code> flag to the Git command to force it to use color codes. The <code>color.ui = true</code> setting is almost always what you’ll want to use.
</p>

<p><code>color.*</code></p>

<p>
  If you want to be more specific about which commands are colored and how, Git
  provides verb-specific coloring settings. Each of these can be set to <code>true</code>, <code>false</code>, or <code>always</code>:
</p>

<pre>
color.branch
color.diff
color.interactive
color.status
</pre>

<p>
  In addition, each of these has subsettings you can use to set specific colors
  for parts of the output, if you want to override each color. For example, to
  set the meta information in your diff output to blue foreground, black
  background, and bold text, you can run
</p>

<pre>
$ git config --global color.diff.meta "blue black bold"
</pre>

<p>
  You can set the color to any of the following values: <code>normal</code>, <code>black</code>, <code>red</code>, <code>green</code>, <code>yellow</code>, <code>blue</code>, <code>magenta</code>, <code>cyan</code>, or <code>white</code>, or, if your terminal supports more than 16 colors, an arbitrary numeric
  color value (between 0 and 255 on a 256-color terminal). If you want an
  attribute like bold in the previous example, you can choose from <code>bold</code>, <code>dim</code>, <code>ul</code>, <code>blink</code>, and <code>reverse</code>.
</p>

<p>
  See the <code>git config</code> manpage for all the subsettings you can configure, if you want to do that.
</p>

<h3>External Merge and Diff Tools</h3>

<p>
  Although Git has an internal implementation of diff, which is what you’ve
  been using, you can set up an external tool instead. You can also set up a
  graphical merge conflict-resolution tool instead of having to resolve
  conflicts manually. I’ll demonstrate setting up the Perforce Visual Merge
  Tool (P4Merge) to do your diffs and merge resolutions, because it’s a nice
  graphical tool and it’s free.
</p>

<p>
  If you want to try this out, P4Merge works on all major platforms, so you
  should be able to do so. I’ll use path names in the examples that work on Mac
  and Linux systems; for Windows, you’ll have to change <code>/usr/local/bin</code> to an executable path in your environment.
</p>

<p>You can download P4Merge here:</p>

<pre>
http://www.perforce.com/product/components/perforce-visual-merge-and-diff-tools
</pre>

<p>
  To begin, you’ll set up external wrapper scripts to run your commands. I’ll
  use the Mac path for the executable; in other systems, it will be where your <code>p4merge</code> binary is installed. Set up a merge wrapper script named <code>extMerge</code> that calls your binary with all the arguments provided:
</p>

<pre>
$ cat /usr/local/bin/extMerge
#!/bin/sh
/Applications/p4merge.app/Contents/MacOS/p4merge $*
</pre>

<p>
  The diff wrapper checks to make sure seven arguments are provided and passes
  two of them to your merge script. By default, Git passes the following
  arguments to the diff program:
</p>

<pre>
path old-file old-hex old-mode new-file new-hex new-mode
</pre>

<p>
  Because you only want the <code>old-file</code> and <code>new-file</code> arguments, you use the wrapper script to pass the ones you need.
</p>

<pre>
$ cat /usr/local/bin/extDiff
#!/bin/sh
[ $# -eq 7 ] &amp;&amp; /usr/local/bin/extMerge "$2" "$5"
</pre>

<p>You also need to make sure these tools are executable:</p>

<pre>
$ sudo chmod +x /usr/local/bin/extMerge
$ sudo chmod +x /usr/local/bin/extDiff
</pre>

<p>
  Now you can set up your config file to use your custom merge resolution and
  diff tools. This takes a number of custom settings: <code>merge.tool</code> to tell Git what strategy to use, <code>mergetool.*.cmd</code> to specify how to run the command, <code>mergetool.trustExitCode</code> to tell Git if the exit code of that program indicates a successful merge
  resolution or not, and <code>diff.external</code> to tell Git what command to run for diffs. So, you can either run four
  config commands
</p>

<pre>
$ git config --global merge.tool extMerge
$ git config --global mergetool.extMerge.cmd \
    'extMerge "$BASE" "$LOCAL" "$REMOTE" "$MERGED"'
$ git config --global mergetool.trustExitCode false
$ git config --global diff.external extDiff
</pre>

<p>or you can edit your <code>~/.gitconfig</code> file to add these lines:</p>

<pre>
[merge]
  tool = extMerge
[mergetool "extMerge"]
  cmd = extMerge \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"
  trustExitCode = false
[diff]
  external = extDiff
</pre>

<p>After all this is set, if you run diff commands such as this:</p>

<pre>
$ git diff 32d1776b1^ 32d1776b1
</pre>

<p>
  Instead of getting the diff output on the command line, Git fires up P4Merge,
  which looks something like Figure 7-1.
</p>

<p>Insert 18333fig0701.png Figure 7-1. P4Merge.</p>

<p>
  If you try to merge two branches and subsequently have merge conflicts, you
  can run the command <code>git mergetool</code>; it starts P4Merge to let you resolve the conflicts through that GUI tool.
</p>

<p>
  The nice thing about this wrapper setup is that you can change your diff and
  merge tools easily. For example, to change your <code>extDiff</code> and <code>extMerge</code> tools to run the KDiff3 tool instead, all you have to do is edit your <code>extMerge</code> file:
</p>

<pre>
$ cat /usr/local/bin/extMerge
#!/bin/sh
/Applications/kdiff3.app/Contents/MacOS/kdiff3 $*
</pre>

<p>
  Now, Git will use the KDiff3 tool for diff viewing and merge conflict
  resolution.
</p>

<p>
  Git comes preset to use a number of other merge-resolution tools without your
  having to set up the cmd configuration. You can set your merge tool to
  kdiff3, opendiff, tkdiff, meld, xxdiff, emerge, vimdiff, or gvimdiff. If
  you’re not interested in using KDiff3 for diff but rather want to use it just
  for merge resolution, and the kdiff3 command is in your path, then you can
  run
</p>

<pre>
$ git config --global merge.tool kdiff3
</pre>

<p>
  If you run this instead of setting up the <code>extMerge</code> and <code>extDiff</code> files, Git will use KDiff3 for merge resolution and the normal Git diff tool
  for diffs.
</p>

<h3>Formatting and Whitespace</h3>

<p>
  Formatting and whitespace issues are some of the more frustrating and subtle
  problems that many developers encounter when collaborating, especially
  cross-platform. It’s very easy for patches or other collaborated work to
  introduce subtle whitespace changes because editors silently introduce them
  or Windows programmers add carriage returns at the end of lines they touch in
  cross-platform projects. Git has a few configuration options to help with
  these issues.
</p>

<p>core.autocrlf</p>

<p>
  If you’re programming on Windows or using another system but working with
  people who are programming on Windows, you’ll probably run into line-ending
  issues at some point. This is because Windows uses both a carriage-return
  character and a linefeed character for newlines in its files, whereas Mac and
  Linux systems use only the linefeed character. This is a subtle but
  incredibly annoying fact of cross-platform work.
</p>

<p>
  Git can handle this by auto-converting CRLF line endings into LF when you
  commit, and vice versa when it checks out code onto your filesystem. You can
  turn on this functionality with the <code>core.autocrlf</code> setting. If you’re on a Windows machine, set it to <code>true</code> — this converts LF endings into CRLF when you check out code:
</p>

<pre>
$ git config --global core.autocrlf true
</pre>

<p>
  If you’re on a Linux or Mac system that uses LF line endings, then you don’t
  want Git to automatically convert them when you check out files; however, if
  a file with CRLF endings accidentally gets introduced, then you may want Git
  to fix it. You can tell Git to convert CRLF to LF on commit but not the other
  way around by setting <code>core.autocrlf</code> to input:
</p>

<pre>
$ git config --global core.autocrlf input
</pre>

<p>
  This setup should leave you with CRLF endings in Windows checkouts but LF
  endings on Mac and Linux systems and in the repository.
</p>

<p>
  If you’re a Windows programmer doing a Windows-only project, then you can
  turn off this functionality, recording the carriage returns in the repository
  by setting the config value to <code>false</code>:
</p>

<pre>
$ git config --global core.autocrlf false
</pre>

<p>core.whitespace</p>

<p>
  Git comes preset to detect and fix some whitespace issues. It can look for
  four primary whitespace issues — two are enabled by default and can be turned
  off, and two aren’t enabled by default but can be activated.
</p>

<p>
  The two that are turned on by default are <code>trailing-space</code>, which looks for spaces at the end of a line, and <code>space-before-tab</code>, which looks for spaces before tabs at the beginning of a line.
</p>

<p>
  The two that are disabled by default but can be turned on are <code>indent-with-non-tab</code>, which looks for lines that begin with eight or more spaces instead of tabs,
  and <code>cr-at-eol</code>, which tells Git that carriage returns at the end of lines are OK.
</p>

<p>
  You can tell Git which of these you want enabled by setting <code>core.whitespace</code> to the values you want on or off, separated by commas. You can disable
  settings by either leaving them out of the setting string or prepending a <code>-</code> in front of the value. For example, if you want all but <code>cr-at-eol</code> to be set, you can do this:
</p>

<pre>
$ git config --global core.whitespace \
    trailing-space,space-before-tab,indent-with-non-tab
</pre>

<p>
  Git will detect these issues when you run a <code>git diff</code> command and try to color them so you can possibly fix them before you
  commit. It will also use these values to help you when you apply patches with
  <code>git apply</code>. When you’re applying patches, you can ask Git to warn you if it’s applying
  patches with the specified whitespace issues:
</p>

<pre>
$ git apply --whitespace=warn &lt;patch&gt;
</pre>

<p>
  Or you can have Git try to automatically fix the issue before applying the
  patch:
</p>

<pre>
$ git apply --whitespace=fix &lt;patch&gt;
</pre>

<p>
  These options apply to the <code>git rebase</code> command as well. If you’ve committed whitespace issues but haven’t yet
  pushed upstream, you can run a <code>rebase</code> with the <code>--whitespace=fix</code> option to have Git automatically fix whitespace issues as it’s rewriting the
  patches.
</p>

<h3>Server Configuration</h3>

<p>
  Not nearly as many configuration options are available for the server side of
  Git, but there are a few interesting ones you may want to take note of.
</p>

<p>receive.fsckObjects</p>

<p>
  By default, Git doesn’t check for consistency all the objects it receives
  during a push. Although Git can check to make sure each object still matches
  its SHA-1 checksum and points to valid objects, it doesn’t do that by default
  on every push. This is a relatively expensive operation and may add a lot of
  time to each push, depending on the size of the repository or the push. If
  you want Git to check object consistency on every push, you can force it to
  do so by setting <code>receive.fsckObjects</code> to true:
</p>

<pre>
$ git config --system receive.fsckObjects true
</pre>

<p>
  Now, Git will check the integrity of your repository before each push is
  accepted to make sure faulty clients aren’t introducing corrupt data.
</p>

<p>receive.denyNonFastForwards</p>

<p>
  If you rebase commits that you’ve already pushed and then try to push again,
  or otherwise try to push a commit to a remote branch that doesn’t contain the
  commit that the remote branch currently points to, you’ll be denied. This is
  generally good policy; but in the case of the rebase, you may determine that
  you know what you’re doing and can force-update the remote branch with a <code>-f</code> flag to your push command.
</p>

<p>
  To disable the ability to force-update remote branches to non-fast-forward
  references, set <code>receive.denyNonFastForwards</code>:
</p>

<pre>
$ git config --system receive.denyNonFastForwards true
</pre>

<p>
  The other way you can do this is via server-side receive hooks, which I’ll
  cover in a bit. That approach lets you do more complex things like deny
  non-fast-forwards to a certain subset of users.
</p>

<p>receive.denyDeletes</p>

<p>
  One of the workarounds to the <code>denyNonFastForwards</code> policy is for the user to delete the branch and then push it back up with
  the new reference. In newer versions of Git (beginning with version 1.6.1),
  you can set <code>receive.denyDeletes</code> to true:
</p>

<pre>
$ git config --system receive.denyDeletes true
</pre>

<p>
  This denies branch and tag deletion over a push across the board — no user
  can do it. To remove remote branches, you must remove the ref files from the
  server manually. There are also more interesting ways to do this on a
  per-user basis via ACLs, as you’ll learn at the end of this chapter.
</p>

<h2>Git Attributes</h2>

<p>
  Some of these settings can also be specified for a path, so that Git applies
  those settings only for a subdirectory or subset of files. These
  path-specific settings are called Git attributes and are set either in a <code>.gitattributes</code> file in one of your directories (normally the root of your project) or in
  the <code>.git/info/attributes</code> file if you don’t want the attributes file committed with your project.
</p>

<p>
  Using attributes, you can do things like specify separate merge strategies
  for individual files or directories in your project, tell Git how to diff
  non-text files, or have Git filter content before you check it into or out of
  Git. In this section, you’ll learn about some of the attributes you can set
  on your paths in your Git project and see a few examples of using this
  feature in practice.
</p>

<h3>Binary Files</h3>

<p>
  One cool trick for which you can use Git attributes is telling Git which
  files are binary (in cases it otherwise may not be able to figure out) and
  giving Git special instructions about how to handle those files. For
  instance, some text files may be machine generated and not diffable, whereas
  some binary files can be diffed — you’ll see how to tell Git which is which.
</p>

<p>Identifying Binary Files</p>

<p>
  Some files look like text files but for all intents and purposes are to be
  treated as binary data. For instance, Xcode projects on the Mac contain a
  file that ends in <code>.pbxproj</code>, which is basically a JSON (plain text javascript data format) dataset
  written out to disk by the IDE that records your build settings and so on.
  Although it’s technically a text file, because it’s all ASCII, you don’t want
  to treat it as such because it’s really a lightweight database — you can’t
  merge the contents if two people changed it, and diffs generally aren’t
  helpful. The file is meant to be consumed by a machine. In essence, you want
  to treat it like a binary file.
</p>

<p>
  To tell Git to treat all <code>pbxproj</code> files as binary data, add the following line to your <code>.gitattributes</code> file:
</p>

<pre>
*.pbxproj -crlf -diff
</pre>

<p>
  Now, Git won’t try to convert or fix CRLF issues; nor will it try to compute
  or print a diff for changes in this file when you run <code>git show</code> or <code>git diff</code> on your project. You can also use a built-in macro <code>binary</code> that means <code>-crlf -diff</code>:
</p>

<pre>
*.pbxproj binary
</pre>

<p>Diffing Binary Files</p>

<p>
  In Git, you can use the attributes functionality to effectively diff binary
  files. You do this by telling Git how to convert your binary data to a text
  format that can be compared via the normal diff. But the question is how do
  you convert <i>binary</i> data to a text? The best solution is to find some tool that does conversion
  for your binary format to a text representation. Unfortunately, very few
  binary formats can be represented as human readable text (imagine trying to
  convert audio data to a text). If this is the case and you failed to get a
  text presentation of your file's contents, it's often relatively easy to get
  a human readable description of that content, or metadata. Metadata won't
  give you a full representation of your file's content, but in any case it's
  better than nothing.
</p>

<p>
  We'll make use of the both described approaches to get usable diffs for some
  widely used binary formats.
</p>

<p>
  Side note: There are different kinds of binary formats with a text content,
  which are hard to find usable converter for. In such a case you could try to
  extract a text from your file with the <code>strings</code> program. Some of these files may use an UTF-16 encoding or other "codepages"
  and <code>strings</code> won’t find anything useful in there. Your mileage may vary. However, <code>strings</code> is available on most Mac and Linux systems, so it may be a good first try to
  do this with many binary formats.
</p>

<p>MS Word files</p>

<p>
  First, you’ll use the described technique to solve one of the most annoying
  problems known to humanity: version-controlling Word documents. Everyone
  knows that Word is the most horrific editor around; but, oddly, everyone uses
  it. If you want to version-control Word documents, you can stick them in a
  Git repository and commit every once in a while; but what good does that do?
  If you run <code>git diff</code> normally, you only see something like this:
</p>

<pre>
$ git diff
diff --git a/chapter1.doc b/chapter1.doc
index 88839c4..4afcb7c 100644
Binary files a/chapter1.doc and b/chapter1.doc differ
</pre>

<p>
  You can’t directly compare two versions unless you check them out and scan
  them manually, right? It turns out you can do this fairly well using Git
  attributes. Put the following line in your <code>.gitattributes</code> file:
</p>

<pre>
*.doc diff=word
</pre>

<p>
  This tells Git that any file that matches this pattern (.doc) should use the
  "word" filter when you try to view a diff that contains changes. What is the
  "word" filter? You have to set it up. Here you’ll configure Git to use the <code>catdoc</code> program, which was written specifically for extracting text from a binary MS
  Word documents (you can get it from <code>http://www.wagner.pp.ru/~vitus/software/catdoc/</code>), to convert Word documents into readable text files, which it will then
  diff properly:
</p>

<pre>
$ git config diff.word.textconv catdoc
</pre>

<p>
  This command adds a section to your <code>.git/config</code> that looks like this:
</p>

<pre>
[diff "word"]
	textconv = catdoc
</pre>

<p>
  Now Git knows that if it tries to do a diff between two snapshots, and any of
  the files end in <code>.doc</code>, it should run those files through the "word" filter, which is defined as
  the <code>catdoc</code> program. This effectively makes nice text-based versions of your Word files
  before attempting to diff them.
</p>

<p>
  Here’s an example. I put Chapter 1 of this book into Git, added some text to
  a paragraph, and saved the document. Then, I ran <code>git diff</code> to see what changed:
</p>

<pre>
$ git diff
diff --git a/chapter1.doc b/chapter1.doc
index c1c8a0a..b93c9e4 100644
--- a/chapter1.doc
+++ b/chapter1.doc
@@ -128,7 +128,7 @@ and data size)
 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.
+system for non-linear development (See Chapter 3).
</pre>

<p>
  Git successfully and succinctly tells me that I added the string "(See
  Chapter 3)", which is correct. Works perfectly!
</p>

<p>OpenDocument Text files</p>

<p>
  The same approach that we used for MS Word files (<code>*.doc</code>) can be used for OpenDocument Text files (<code>*.odt</code>) created by OpenOffice.org.
</p>

<p>Add the following line to your <code>.gitattributes</code> file:</p>

<pre>
*.odt diff=odt
</pre>

<p>Now set up the <code>odt</code> diff filter in <code>.git/config</code>:</p>

<pre>
[diff "odt"]
	binary = true
	textconv = /usr/local/bin/odt-to-txt
</pre>

<p>
  OpenDocument files are actually zip’ped directories containing multiple files
  (the content in an XML format, stylesheets, images, etc.). We’ll need to
  write a script to extract the content and return it as plain text. Create a
  file <code>/usr/local/bin/odt-to-txt</code> (you are free to put it into a different directory) with the following
  content:
</p>

<pre>
#! /usr/bin/env perl
# Simplistic OpenDocument Text (.odt) to plain text converter.
# Author: Philipp Kempgen

if (! defined($ARGV[0])) {
	print STDERR "No filename given!\n";
	print STDERR "Usage: $0 filename\n";
	exit 1;
}

my $content = '';
open my $fh, '-|', 'unzip', '-qq', '-p', $ARGV[0], 'content.xml' or die $!;
{
	local $/ = undef;  # slurp mode
	$content = &lt;$fh&gt;;
}
close $fh;
$_ = $content;
s/&lt;text:span\b[^&gt;]*&gt;//g;           # remove spans
s/&lt;text:h\b[^&gt;]*&gt;/\n\n*****  /g;   # headers
s/&lt;text:list-item\b[^&gt;]*&gt;\s*&lt;text:p\b[^&gt;]*&gt;/\n    --  /g;  # list items
s/&lt;text:list\b[^&gt;]*&gt;/\n\n/g;       # lists
s/&lt;text:p\b[^&gt;]*&gt;/\n  /g;          # paragraphs
s/&lt;[^&gt;]+&gt;//g;                      # remove all XML tags
s/\n{2,}/\n\n/g;                   # remove multiple blank lines
s/\A\n+//;                         # remove leading blank lines
print "\n", $_, "\n\n";
</pre>

<p>And make it executable</p>

<pre>
chmod +x /usr/local/bin/odt-to-txt
</pre>

<p>
  Now <code>git diff</code> will be able to tell you what changed in <code>.odt</code> files.
</p>

<p>Image files</p>

<p>
  Another interesting problem you can solve this way involves diffing image
  files. One way to do this is to run PNG files through a filter that extracts
  their EXIF information — metadata that is recorded with most image formats.
  If you download and install the <code>exiftool</code> program, you can use it to convert your images into text about the metadata,
  so at least the diff will show you a textual representation of any changes
  that happened:
</p>

<pre>
$ echo '*.png diff=exif' &gt;&gt; .gitattributes
$ git config diff.exif.textconv exiftool
</pre>

<p>
  If you replace an image in your project and run <code>git diff</code>, you see something like this:
</p>

<pre>
diff --git a/image.png b/image.png
index 88839c4..4afcb7c 100644
--- a/image.png
+++ b/image.png
@@ -1,12 +1,12 @@
 ExifTool Version Number         : 7.74
-File Size                       : 70 kB
-File Modification Date/Time     : 2009:04:17 10:12:35-07:00
+File Size                       : 94 kB
+File Modification Date/Time     : 2009:04:21 07:02:43-07:00
 File Type                       : PNG
 MIME Type                       : image/png
-Image Width                     : 1058
-Image Height                    : 889
+Image Width                     : 1056
+Image Height                    : 827
 Bit Depth                       : 8
 Color Type                      : RGB with Alpha
</pre>

<p>
  You can easily see that the file size and image dimensions have both changed.
</p>

<h3>Keyword Expansion</h3>

<p>
  SVN- or CVS-style keyword expansion is often requested by developers used to
  those systems. The main problem with this in Git is that you can’t modify a
  file with information about the commit after you’ve committed, because Git
  checksums the file first. However, you can inject text into a file when it’s
  checked out and remove it again before it’s added to a commit. Git attributes
  offers you two ways to do this.
</p>

<p>
  First, you can inject the SHA-1 checksum of a blob into an <code>$Id$</code> field in the file automatically. If you set this attribute on a file or set
  of files, then the next time you check out that branch, Git will replace that
  field with the SHA-1 of the blob. It’s important to notice that it isn’t the
  SHA of the commit, but of the blob itself:
</p>

<pre>
$ echo '*.txt ident' &gt;&gt; .gitattributes
$ echo '$Id$' &gt; test.txt
</pre>

<p>The next time you check out this file, Git injects the SHA of the blob:</p>

<pre>
$ rm test.txt
$ git checkout -- test.txt
$ cat test.txt
$Id: 42812b7653c7b88933f8a9d6cad0ca16714b9bb3 $
</pre>

<p>
  However, that result is of limited use. If you’ve used keyword substitution
  in CVS or Subversion, you can include a datestamp — the SHA isn’t all that
  helpful, because it’s fairly random and you can’t tell if one SHA is older or
  newer than another.
</p>

<p>
  It turns out that you can write your own filters for doing substitutions in
  files on commit/checkout. These are the "clean" and "smudge" filters. In the <code>.gitattributes</code> file, you can set a filter for particular paths and then set up scripts that
  will process files just before they’re checked out ("smudge", see Figure 7-2)
  and just before they’re committed ("clean", see Figure 7-3). These filters
  can be set to do all sorts of fun things.
</p>

<p>
  Insert 18333fig0702.png Figure 7-2. The “smudge” filter is run on checkout.
</p>

<p>
  Insert 18333fig0703.png Figure 7-3. The “clean” filter is run when files are
  staged.
</p>

<p>
  The original commit message for this functionality gives a simple example of
  running all your C source code through the <code>indent</code> program before committing. You can set it up by setting the filter attribute
  in your <code>.gitattributes</code> file to filter <code>*.c</code> files with the "indent" filter:
</p>

<pre>
*.c     filter=indent
</pre>

<p>Then, tell Git what the "indent" filter does on smudge and clean:</p>

<pre>
$ git config --global filter.indent.clean indent
$ git config --global filter.indent.smudge cat
</pre>

<p>
  In this case, when you commit files that match <code>*.c</code>, Git will run them through the indent program before it commits them and
  then run them through the <code>cat</code> program before it checks them back out onto disk. The <code>cat</code> program is basically a no-op: it spits out the same data that it gets in.
  This combination effectively filters all C source code files through <code>indent</code> before committing.
</p>

<p>
  Another interesting example gets <code>$Date$</code> keyword expansion, RCS style. To do this properly, you need a small script
  that takes a filename, figures out the last commit date for this project, and
  inserts the date into the file. Here is a small Ruby script that does that:
</p>

<pre>
#! /usr/bin/env ruby
data = STDIN.read
last_date = `git log --pretty=format:"%ad" -1`
puts data.gsub('$Date$', '$Date: ' + last_date.to_s + '$')
</pre>

<p>
  All the script does is get the latest commit date from the <code>git log</code> command, stick that into any <code>$Date$</code> strings it sees in stdin, and print the results — it should be simple to do
  in whatever language you’re most comfortable in. You can name this file <code>expand_date</code> and put it in your path. Now, you need to set up a filter in Git (call it <code>dater</code>) and tell it to use your <code>expand_date</code> filter to smudge the files on checkout. You’ll use a Perl expression to
  clean that up on commit:
</p>

<pre>
$ git config filter.dater.smudge expand_date
$ git config filter.dater.clean 'perl -pe "s/\\\$Date[^\\\$]*\\\$/\\\$Date\\\$/"'
</pre>

<p>
  This Perl snippet strips out anything it sees in a <code>$Date$</code> string, to get back to where you started. Now that your filter is ready, you
  can test it by setting up a file with your <code>$Date$</code> keyword and then setting up a Git attribute for that file that engages the
  new filter:
</p>

<pre>
$ echo '# $Date$' &gt; date_test.txt
$ echo 'date*.txt filter=dater' &gt;&gt; .gitattributes
</pre>

<p>
  If you commit those changes and check out the file again, you see the keyword
  properly substituted:
</p>

<pre>
$ git add date_test.txt .gitattributes
$ git commit -m "Testing date expansion in Git"
$ rm date_test.txt
$ git checkout date_test.txt
$ cat date_test.txt
# $Date: Tue Apr 21 07:26:52 2009 -0700$
</pre>

<p>
  You can see how powerful this technique can be for customized applications.
  You have to be careful, though, because the <code>.gitattributes</code> file is committed and passed around with the project but the driver (in this
  case, <code>dater</code>) isn’t; so, it won’t work everywhere. When you design these filters, they
  should be able to fail gracefully and have the project still work properly.
</p>

<h3>Exporting Your Repository</h3>

<p>
  Git attribute data also allows you to do some interesting things when
  exporting an archive of your project.
</p>

<p>export-ignore</p>

<p>
  You can tell Git not to export certain files or directories when generating
  an archive. If there is a subdirectory or file that you don’t want to include
  in your archive file but that you do want checked into your project, you can
  determine those files via the <code>export-ignore</code> attribute.
</p>

<p>
  For example, say you have some test files in a <code>test/</code> subdirectory, and it doesn’t make sense to include them in the tarball
  export of your project. You can add the following line to your Git attributes
  file:
</p>

<pre>
test/ export-ignore
</pre>

<p>
  Now, when you run <code>git archive</code> to create a tarball of your project, that directory won’t be included in the
  archive.
</p>

<p>export-subst</p>

<p>
  Another thing you can do for your archives is some simple keyword
  substitution. Git lets you put the string <code>$Format:$</code> in any file with any of the <code>--pretty=format</code> formatting shortcodes, many of which you saw in Chapter 2. For instance, if
  you want to include a file named <code>LAST_COMMIT</code> in your project, and the last commit date was automatically injected into it
  when <code>git archive</code> ran, you can set up the file like this:
</p>

<pre>
$ echo 'Last commit date: $Format:%cd$' &gt; LAST_COMMIT
$ echo "LAST_COMMIT export-subst" &gt;&gt; .gitattributes
$ git add LAST_COMMIT .gitattributes
$ git commit -am 'adding LAST_COMMIT file for archives'
</pre>

<p>
  When you run <code>git archive</code>, the contents of that file when people open the archive file will look like
  this:
</p>

<pre>
$ cat LAST_COMMIT
Last commit date: $Format:Tue Apr 21 08:38:48 2009 -0700$
</pre>

<h3>Merge Strategies</h3>

<p>
  You can also use Git attributes to tell Git to use different merge strategies
  for specific files in your project. One very useful option is to tell Git to
  not try to merge specific files when they have conflicts, but rather to use
  your side of the merge over someone else’s.
</p>

<p>
  This is helpful if a branch in your project has diverged or is specialized,
  but you want to be able to merge changes back in from it, and you want to
  ignore certain files. Say you have a database settings file called
  database.xml that is different in two branches, and you want to merge in your
  other branch without messing up the database file. You can set up an
  attribute like this:
</p>

<pre>
database.xml merge=ours
</pre>

<p>And then define a dummy <code>ours</code> merge strategy with:</p>

<pre>
git config --global merge.ours.driver true
</pre>

<p>
  If you merge in the other branch, instead of having merge conflicts with the
  database.xml file, you see something like this:
</p>

<pre>
$ git merge topic
Auto-merging database.xml
Merge made by recursive.
</pre>

<p>In this case, database.xml stays at whatever version you originally had.</p>

<h2>Git Hooks</h2>

<p>
  Like many other Version Control Systems, Git has a way to fire off custom
  scripts when certain important actions occur. There are two groups of these
  hooks: client side and server side. The client-side hooks are for client
  operations such as committing and merging. The server-side hooks are for Git
  server operations such as receiving pushed commits. You can use these hooks
  for all sorts of reasons, and you’ll learn about a few of them here.
</p>

<h3>Installing a Hook</h3>

<p>
  The hooks are all stored in the <code>hooks</code> subdirectory of the Git directory. In most projects, that’s <code>.git/hooks</code>. By default, Git populates this directory with a bunch of example scripts,
  many of which are useful by themselves; but they also document the input
  values of each script. All the examples are written as shell scripts, with
  some Perl thrown in, but any properly named executable scripts will work fine
  — you can write them in Ruby or Python or what have you. These example hook
  files end with .sample; you’ll need to rename them.
</p>

<p>
  To enable a hook script, put a file in the <code>hooks</code> subdirectory of your Git directory that is named appropriately and is
  executable. From that point forward, it should be called. I’ll cover most of
  the major hook filenames here.
</p>

<h3>Client-Side Hooks</h3>

<p>
  There are a lot of client-side hooks. This section splits them into
  committing-workflow hooks, e-mail-workflow scripts, and the rest of the
  client-side scripts.
</p>

<p>Committing-Workflow Hooks</p>

<p>
  The first four hooks have to do with the committing process. The <code>pre-commit</code> hook is run first, before you even type in a commit message. It’s used to
  inspect the snapshot that’s about to be committed, to see if you’ve forgotten
  something, to make sure tests run, or to examine whatever you need to inspect
  in the code. Exiting non-zero from this hook aborts the commit, although you
  can bypass it with <code>git commit --no-verify</code>. You can do things like check for code style (run lint or something
  equivalent), check for trailing whitespace (the default hook does exactly
  that), or check for appropriate documentation on new methods.
</p>

<p>
  The <code>prepare-commit-msg</code> hook is run before the commit message editor is fired up but after the
  default message is created. It lets you edit the default message before the
  commit author sees it. This hook takes a few options: the path to the file
  that holds the commit message so far, the type of commit, and the commit
  SHA-1 if this is an amended commit. This hook generally isn’t useful for
  normal commits; rather, it’s good for commits where the default message is
  auto-generated, such as templated commit messages, merge commits, squashed
  commits, and amended commits. You may use it in conjunction with a commit
  template to programmatically insert information.
</p>

<p>
  The <code>commit-msg</code> hook takes one parameter, which again is the path to a temporary file that
  contains the current commit message. If this script exits non-zero, Git
  aborts the commit process, so you can use it to validate your project state
  or commit message before allowing a commit to go through. In the last section
  of this chapter, I’ll demonstrate using this hook to check that your commit
  message is conformant to a required pattern.
</p>

<p>
  After the entire commit process is completed, the <code>post-commit</code> hook runs. It doesn’t take any parameters, but you can easily get the last
  commit by running <code>git log -1 HEAD</code>. Generally, this script is used for notification or something similar.
</p>

<p>
  The committing-workflow client-side scripts can be used in just about any
  workflow. They’re often used to enforce certain policies, although it’s
  important to note that these scripts aren’t transferred during a clone. You
  can enforce policy on the server side to reject pushes of commits that don’t
  conform to some policy, but it’s entirely up to the developer to use these
  scripts on the client side. So, these are scripts to help developers, and
  they must be set up and maintained by them, although they can be overridden
  or modified by them at any time.
</p>

<p>E-mail Workflow Hooks</p>

<p>
  You can set up three client-side hooks for an e-mail-based workflow. They’re
  all invoked by the <code>git am</code> command, so if you aren’t using that command in your workflow, you can
  safely skip to the next section. If you’re taking patches over e-mail
  prepared by <code>git format-patch</code>, then some of these may be helpful to you.
</p>

<p>
  The first hook that is run is <code>applypatch-msg</code>. It takes a single argument: the name of the temporary file that contains
  the proposed commit message. Git aborts the patch if this script exits
  non-zero. You can use this to make sure a commit message is properly
  formatted or to normalize the message by having the script edit it in place.
</p>

<p>
  The next hook to run when applying patches via <code>git am</code> is <code>pre-applypatch</code>. It takes no arguments and is run after the patch is applied, so you can use
  it to inspect the snapshot before making the commit. You can run tests or
  otherwise inspect the working tree with this script. If something is missing
  or the tests don’t pass, exiting non-zero also aborts the <code>git am</code> script without committing the patch.
</p>

<p>
  The last hook to run during a <code>git am</code> operation is <code>post-applypatch</code>. You can use it to notify a group or the author of the patch you pulled in
  that you’ve done so. You can’t stop the patching process with this script.
</p>

<p>Other Client Hooks</p>

<p>
  The <code>pre-rebase</code> hook runs before you rebase anything and can halt the process by exiting
  non-zero. You can use this hook to disallow rebasing any commits that have
  already been pushed. The example <code>pre-rebase</code> hook that Git installs does this, although it assumes that next is the name
  of the branch you publish. You’ll likely need to change that to whatever your
  stable, published branch is.
</p>

<p>
  After you run a successful <code>git checkout</code>, the <code>post-checkout</code> hook runs; you can use it to set up your working directory properly for your
  project environment. This may mean moving in large binary files that you
  don’t want source controlled, auto-generating documentation, or something
  along those lines.
</p>

<p>
  Finally, the <code>post-merge</code> hook runs after a successful <code>merge</code> command. You can use it to restore data in the working tree that Git can’t
  track, such as permissions data. This hook can likewise validate the presence
  of files external to Git control that you may want copied in when the working
  tree changes.
</p>

<h3>Server-Side Hooks</h3>

<p>
  In addition to the client-side hooks, you can use a couple of important
  server-side hooks as a system administrator to enforce nearly any kind of
  policy for your project. These scripts run before and after pushes to the
  server. The pre hooks can exit non-zero at any time to reject the push as
  well as print an error message back to the client; you can set up a push
  policy that’s as complex as you wish.
</p>

<p>pre-receive and post-receive</p>

<p>
  The first script to run when handling a push from a client is <code>pre-receive</code>. It takes a list of references that are being pushed from stdin; if it exits
  non-zero, none of them are accepted. You can use this hook to do things like
  make sure none of the updated references are non-fast-forwards; or to check
  that the user doing the pushing has create, delete, or push access or access
  to push updates to all the files they’re modifying with the push.
</p>

<p>
  The <code>post-receive</code> hook runs after the entire process is completed and can be used to update
  other services or notify users. It takes the same stdin data as the <code>pre-receive</code> hook. Examples include e-mailing a list, notifying a continuous integration
  server, or updating a ticket-tracking system — you can even parse the commit
  messages to see if any tickets need to be opened, modified, or closed. This
  script can’t stop the push process, but the client doesn’t disconnect until
  it has completed; so, be careful when you try to do anything that may take a
  long time.
</p>

<p>update</p>

<p>
  The update script is very similar to the <code>pre-receive</code> script, except that it’s run once for each branch the pusher is trying to
  update. If the pusher is trying to push to multiple branches, <code>pre-receive</code> runs only once, whereas update runs once per branch they’re pushing to.
  Instead of reading from stdin, this script takes three arguments: the name of
  the reference (branch), the SHA-1 that reference pointed to before the push,
  and the SHA-1 the user is trying to push. If the update script exits
  non-zero, only that reference is rejected; other references can still be
  updated.
</p>

<h2>An Example Git-Enforced Policy</h2>

<p>
  In this section, you’ll use what you’ve learned to establish a Git workflow
  that checks for a custom commit message format, enforces fast-forward-only
  pushes, and allows only certain users to modify certain subdirectories in a
  project. You’ll build client scripts that help the developer know if their
  push will be rejected and server scripts that actually enforce the policies.
</p>

<p>
  I used Ruby to write these, both because it’s my preferred scripting language
  and because I feel it’s the most pseudocode-looking of the scripting
  languages; thus you should be able to roughly follow the code even if you
  don’t use Ruby. However, any language will work fine. All the sample hook
  scripts distributed with Git are in either Perl or Bash scripting, so you can
  also see plenty of examples of hooks in those languages by looking at the
  samples.
</p>

<h3>Server-Side Hook</h3>

<p>
  All the server-side work will go into the update file in your hooks
  directory. The update file runs once per branch being pushed and takes the
  reference being pushed to, the old revision where that branch was, and the
  new revision being pushed. You also have access to the user doing the pushing
  if the push is being run over SSH. If you’ve allowed everyone to connect with
  a single user (like "git") via public-key authentication, you may have to
  give that user a shell wrapper that determines which user is connecting based
  on the public key, and set an environment variable specifying that user. Here
  I assume the connecting user is in the <code>$USER</code> environment variable, so your update script begins by gathering all the
  information you need:
</p>

<pre>
#!/usr/bin/env ruby

refname = ARGV[0]
oldrev  = ARGV[1]
newrev  = ARGV[2]
user    = ENV['USER']

puts "Enforcing Policies... \n(#{refname}) (#{oldrev[0,6]}) (#{newrev[0,6]})"
</pre>

<p>Enforcing a Specific Commit-Message Format</p>

<p>
  Your first challenge is to enforce that each commit message must adhere to a
  particular format. Just to have a target, assume that each message has to
  include a string that looks like "ref: 1234" because you want each commit to
  link to a work item in your ticketing system. You must look at each commit
  being pushed up, see if that string is in the commit message, and, if the
  string is absent from any of the commits, exit non-zero so the push is
  rejected.
</p>

<p>
  You can get a list of the SHA-1 values of all the commits that are being
  pushed by taking the <code>$newrev</code> and <code>$oldrev</code> values and passing them to a Git plumbing command called <code>git rev-list</code>. This is basically the <code>git log</code> command, but by default it prints out only the SHA-1 values and no other
  information. So, to get a list of all the commit SHAs introduced between one
  commit SHA and another, you can run something like this:
</p>

<pre>
$ git rev-list 538c33..d14fc7
d14fc7c847ab946ec39590d87783c69b031bdfb7
9f585da4401b0a3999e84113824d15245c13f0be
234071a1be950e2a8d078e6141f5cd20c1e61ad3
dfa04c9ef3d5197182f13fb5b9b1fb7717d2222a
17716ec0f1ff5c77eff40b7fe912f9f6cfd0e475
</pre>

<p>
  You can take that output, loop through each of those commit SHAs, grab the
  message for it, and test that message against a regular expression that looks
  for a pattern.
</p>

<p>
  You have to figure out how to get the commit message from each of these
  commits to test. To get the raw commit data, you can use another plumbing
  command called <code>git cat-file</code>. I’ll go over all these plumbing commands in detail in Chapter 9; but for
  now, here’s what that command gives you:
</p>

<pre>
$ git cat-file commit ca82a6
tree cfda3bf379e4f8dba8717dee55aab78aef7f4daf
parent 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
author Scott Chacon &lt;schacon@gmail.com&gt; 1205815931 -0700
committer Scott Chacon &lt;schacon@gmail.com&gt; 1240030591 -0700

changed the version number
</pre>

<p>
  A simple way to get the commit message from a commit when you have the SHA-1
  value is to go to the first blank line and take everything after that. You
  can do so with the <code>sed</code> command on Unix systems:
</p>

<pre>
$ git cat-file commit ca82a6 | sed '1,/^$/d'
changed the version number
</pre>

<p>
  You can use that incantation to grab the commit message from each commit that
  is trying to be pushed and exit if you see anything that doesn’t match. To
  exit the script and reject the push, exit non-zero. The whole method looks
  like this:
</p>

<pre>
$regex = /\[ref: (\d+)\]/

# enforced custom commit message format
def check_message_format
  missed_revs = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
  missed_revs.each do |rev|
    message = `git cat-file commit #{rev} | sed '1,/^$/d'`
    if !$regex.match(message)
      puts "[POLICY] Your message is not formatted correctly"
      exit 1
    end
  end
end
check_message_format
</pre>

<p>
  Putting that in your <code>update</code> script will reject updates that contain commits that have messages that
  don’t adhere to your rule.
</p>

<p>Enforcing a User-Based ACL System</p>

<p>
  Suppose you want to add a mechanism that uses an access control list (ACL)
  that specifies which users are allowed to push changes to which parts of your
  projects. Some people have full access, and others only have access to push
  changes to certain subdirectories or specific files. To enforce this, you’ll
  write those rules to a file named <code>acl</code> that lives in your bare Git repository on the server. You’ll have the <code>update</code> hook look at those rules, see what files are being introduced for all the
  commits being pushed, and determine whether the user doing the push has
  access to update all those files.
</p>

<p>
  The first thing you’ll do is write your ACL. Here you’ll use a format very
  much like the CVS ACL mechanism: it uses a series of lines, where the first
  field is <code>avail</code> or <code>unavail</code>, the next field is a comma-delimited list of the users to which the rule
  applies, and the last field is the path to which the rule applies (blank
  meaning open access). All of these fields are delimited by a pipe (<code>|</code>) character.
</p>

<p>
  In this case, you have a couple of administrators, some documentation writers
  with access to the <code>doc</code> directory, and one developer who only has access to the <code>lib</code> and <code>tests</code> directories, and your ACL file looks like this:
</p>

<pre>
avail|nickh,pjhyett,defunkt,tpw
avail|usinclair,cdickens,ebronte|doc
avail|schacon|lib
avail|schacon|tests
</pre>

<p>
  You begin by reading this data into a structure that you can use. In this
  case, to keep the example simple, you’ll only enforce the <code>avail</code> directives. Here is a method that gives you an associative array where the
  key is the user name and the value is an array of paths to which the user has
  write access:
</p>

<pre>
def get_acl_access_data(acl_file)
  # read in ACL data
  acl_file = File.read(acl_file).split("\n").reject { |line| line == '' }
  access = {}
  acl_file.each do |line|
    avail, users, path = line.split('|')
    next unless avail == 'avail'
    users.split(',').each do |user|
      access[user] ||= []
      access[user] &lt;&lt; path
    end
  end
  access
end
</pre>

<p>
  On the ACL file you looked at earlier, this <code>get_acl_access_data</code> method returns a data structure that looks like this:
</p>

<pre>
{"defunkt"=&gt;[nil],
 "tpw"=&gt;[nil],
 "nickh"=&gt;[nil],
 "pjhyett"=&gt;[nil],
 "schacon"=&gt;["lib", "tests"],
 "cdickens"=&gt;["doc"],
 "usinclair"=&gt;["doc"],
 "ebronte"=&gt;["doc"]}
</pre>

<p>
  Now that you have the permissions sorted out, you need to determine what
  paths the commits being pushed have modified, so you can make sure the user
  who’s pushing has access to all of them.
</p>

<p>
  You can pretty easily see what files have been modified in a single commit
  with the <code>--name-only</code> option to the <code>git log</code> command (mentioned briefly in Chapter 2):
</p>

<pre>
$ git log -1 --name-only --pretty=format:'' 9f585d

README
lib/test.rb
</pre>

<p>
  If you use the ACL structure returned from the <code>get_acl_access_data</code> method and check it against the listed files in each of the commits, you can
  determine whether the user has access to push all of their commits:
</p>

<pre>
# only allows certain users to modify certain subdirectories in a project
def check_directory_perms
  access = get_acl_access_data('acl')

  # see if anyone is trying to push something they can't
  new_commits = `git rev-list #{$oldrev}..#{$newrev}`.split("\n")
  new_commits.each do |rev|
    files_modified = `git log -1 --name-only --pretty=format:'' #{rev}`.split("\n")
    files_modified.each do |path|
      next if path.size == 0
      has_file_access = false
      access[$user].each do |access_path|
        if !access_path || # user has access to everything
          (path.index(access_path) == 0) # access to this path
          has_file_access = true
        end
      end
      if !has_file_access
        puts "[POLICY] You do not have access to push to #{path}"
        exit 1
      end
    end
  end
end

check_directory_perms
</pre>

<p>
  Most of that should be easy to follow. You get a list of new commits being
  pushed to your server with <code>git rev-list</code>. Then, for each of those, you find which files are modified and make sure
  the user who’s pushing has access to all the paths being modified. One
  Rubyism that may not be clear is <code>path.index(access_path) == 0</code>, which is true if path begins with <code>access_path</code> — this ensures that <code>access_path</code> is not just in one of the allowed paths, but an allowed path begins with
  each accessed path.
</p>

<p>
  Now your users can’t push any commits with badly formed messages or with
  modified files outside of their designated paths.
</p>

<p>Enforcing Fast-Forward-Only Pushes</p>

<p>
  The only thing left is to enforce fast-forward-only pushes. To do so, you can
  simply set the <code>receive.denyDeletes</code> and <code>receive.denyNonFastForwards</code> settings. But enforcing this with a hook will also work, and you can modify
  it to do so only for certain users or whatever else you come up with later.
</p>

<p>
  The logic for checking this is to see if any commits are reachable from the
  older revision that aren’t reachable from the newer one. If there are none,
  then it was a fast-forward push; otherwise, you deny it:
</p>

<pre>
# enforces fast-forward only pushes
def check_fast_forward
  missed_refs = `git rev-list #{$newrev}..#{$oldrev}`
  missed_ref_count = missed_refs.split("\n").size
  if missed_ref_count &gt; 0
    puts "[POLICY] Cannot push a non fast-forward reference"
    exit 1
  end
end

check_fast_forward
</pre>

<p>
  Everything is set up. If you run <code>chmod u+x .git/hooks/update</code>, which is the file into which you should have put all this code, and then
  try to push a non-fast-forward reference, you’ll get something like this:
</p>

<pre>
$ git push -f origin master
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 323 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
Enforcing Policies...
(refs/heads/master) (8338c5) (c5b616)
[POLICY] Cannot push a non fast-forward reference
error: hooks/update exited with error code 1
error: hook declined to update refs/heads/master
To git@gitserver:project.git
 ! [remote rejected] master -&gt; master (hook declined)
error: failed to push some refs to 'git@gitserver:project.git'
</pre>

<p>
  There are a couple of interesting things here. First, you see this where the
  hook starts running.
</p>

<pre>
Enforcing Policies...
(refs/heads/master) (8338c5) (c5b616)
</pre>

<p>
  Notice that you printed that out to stdout at the very beginning of your
  update script. It’s important to note that anything your script prints to
  stdout will be transferred to the client.
</p>

<p>The next thing you’ll notice is the error message.</p>

<pre>
[POLICY] Cannot push a non fast-forward reference
error: hooks/update exited with error code 1
error: hook declined to update refs/heads/master
</pre>

<p>
  The first line was printed out by you, the other two were Git telling you
  that the update script exited non-zero and that is what is declining your
  push. Lastly, you have this:
</p>

<pre>
To git@gitserver:project.git
 ! [remote rejected] master -&gt; master (hook declined)
error: failed to push some refs to 'git@gitserver:project.git'
</pre>

<p>
  You’ll see a remote rejected message for each reference that your hook
  declined, and it tells you that it was declined specifically because of a
  hook failure.
</p>

<p>
  Furthermore, if the ref marker isn’t there in any of your commits, you’ll see
  the error message you’re printing out for that.
</p>

<pre>
[POLICY] Your message is not formatted correctly
</pre>

<p>
  Or if someone tries to edit a file they don’t have access to and push a
  commit containing it, they will see something similar. For instance, if a
  documentation author tries to push a commit modifying something in the <code>lib</code> directory, they see
</p>

<pre>
[POLICY] You do not have access to push to lib/test.rb
</pre>

<p>
  That’s all. From now on, as long as that <code>update</code> script is there and executable, your repository will never be rewound and
  will never have a commit message without your pattern in it, and your users
  will be sandboxed.
</p>

<h3>Client-Side Hooks</h3>

<p>
  The downside to this approach is the whining that will inevitably result when
  your users’ commit pushes are rejected. Having their carefully crafted work
  rejected at the last minute can be extremely frustrating and confusing; and
  furthermore, they will have to edit their history to correct it, which isn’t
  always for the faint of heart.
</p>

<p>
  The answer to this dilemma is to provide some client-side hooks that users
  can use to notify them when they’re doing something that the server is likely
  to reject. That way, they can correct any problems before committing and
  before those issues become more difficult to fix. Because hooks aren’t
  transferred with a clone of a project, you must distribute these scripts some
  other way and then have your users copy them to their <code>.git/hooks</code> directory and make them executable. You can distribute these hooks within
  the project or in a separate project, but there is no way to set them up
  automatically.
</p>

<p>
  To begin, you should check your commit message just before each commit is
  recorded, so you know the server won’t reject your changes due to badly
  formatted commit messages. To do this, you can add the <code>commit-msg</code> hook. If you have it read the message from the file passed as the first
  argument and compare that to the pattern, you can force Git to abort the
  commit if there is no match:
</p>

<pre>
#!/usr/bin/env ruby
message_file = ARGV[0]
message = File.read(message_file)

$regex = /\[ref: (\d+)\]/

if !$regex.match(message)
  puts "[POLICY] Your message is not formatted correctly"
  exit 1
end
</pre>

<p>
  If that script is in place (in <code>.git/hooks/commit-msg</code>) and executable, and you commit with a message that isn’t properly
  formatted, you see this:
</p>

<pre>
$ git commit -am 'test'
[POLICY] Your message is not formatted correctly
</pre>

<p>
  No commit was completed in that instance. However, if your message contains
  the proper pattern, Git allows you to commit:
</p>

<pre>
$ git commit -am 'test [ref: 132]'
[master e05c914] test [ref: 132]
 1 files changed, 1 insertions(+), 0 deletions(-)
</pre>

<p>
  Next, you want to make sure you aren’t modifying files that are outside your
  ACL scope. If your project’s <code>.git</code> directory contains a copy of the ACL file you used previously, then the
  following <code>pre-commit</code> script will enforce those constraints for you:
</p>

<pre>
#!/usr/bin/env ruby

$user    = ENV['USER']

# [ insert acl_access_data method from above ]

# only allows certain users to modify certain subdirectories in a project
def check_directory_perms
  access = get_acl_access_data('.git/acl')

  files_modified = `git diff-index --cached --name-only HEAD`.split("\n")
  files_modified.each do |path|
    next if path.size == 0
    has_file_access = false
    access[$user].each do |access_path|
    if !access_path || (path.index(access_path) == 0)
      has_file_access = true
    end
    if !has_file_access
      puts "[POLICY] You do not have access to push to #{path}"
      exit 1
    end
  end
end

check_directory_perms
</pre>

<p>
  This is roughly the same script as the server-side part, but with two
  important differences. First, the ACL file is in a different place, because
  this script runs from your working directory, not from your Git directory.
  You have to change the path to the ACL file from this
</p>

<pre>
access = get_acl_access_data('acl')
</pre>

<p>to this:</p>

<pre>
access = get_acl_access_data('.git/acl')
</pre>

<p>
  The other important difference is the way you get a listing of the files that
  have been changed. Because the server-side method looks at the log of
  commits, and, at this point, the commit hasn’t been recorded yet, you must
  get your file listing from the staging area instead. Instead of
</p>

<pre>
files_modified = `git log -1 --name-only --pretty=format:'' #{ref}`
</pre>

<p>you have to use</p>

<pre>
files_modified = `git diff-index --cached --name-only HEAD`
</pre>

<p>
  But those are the only two differences — otherwise, the script works the same
  way. One caveat is that it expects you to be running locally as the same user
  you push as to the remote machine. If that is different, you must set the <code>$user</code> variable manually.
</p>

<p>
  The last thing you have to do is check that you’re not trying to push
  non-fast-forwarded references, but that is a bit less common. To get a
  reference that isn’t a fast-forward, you either have to rebase past a commit
  you’ve already pushed up or try pushing a different local branch up to the
  same remote branch.
</p>

<p>
  Because the server will tell you that you can’t push a non-fast-forward
  anyway, and the hook prevents forced pushes, the only accidental thing you
  can try to catch is rebasing commits that have already been pushed.
</p>

<p>
  Here is an example pre-rebase script that checks for that. It gets a list of
  all the commits you’re about to rewrite and checks whether they exist in any
  of your remote references. If it sees one that is reachable from one of your
  remote references, it aborts the rebase:
</p>

<pre>
#!/usr/bin/env ruby

base_branch = ARGV[0]
if ARGV[1]
  topic_branch = ARGV[1]
else
  topic_branch = "HEAD"
end

target_shas = `git rev-list #{base_branch}..#{topic_branch}`.split("\n")
remote_refs = `git branch -r`.split("\n").map { |r| r.strip }

target_shas.each do |sha|
  remote_refs.each do |remote_ref|
    shas_pushed = `git rev-list ^#{sha}^@ refs/remotes/#{remote_ref}`
    if shas_pushed.split("\n").include?(sha)
      puts "[POLICY] Commit #{sha} has already been pushed to #{remote_ref}"
      exit 1
    end
  end
end
</pre>

<p>
  This script uses a syntax that wasn’t covered in the Revision Selection
  section of Chapter 6. You get a list of commits that have already been pushed
  up by running this:
</p>

<pre>
git rev-list ^#{sha}^@ refs/remotes/#{remote_ref}
</pre>

<p>
  The <code>SHA^@</code> syntax resolves to all the parents of that commit. You’re looking for any
  commit that is reachable from the last commit on the remote and that isn’t
  reachable from any parent of any of the SHAs you’re trying to push up —
  meaning it’s a fast-forward.
</p>

<p>
  The main drawback to this approach is that it can be very slow and is often
  unnecessary — if you don’t try to force the push with <code>-f</code>, the server will warn you and not accept the push. However, it’s an
  interesting exercise and can in theory help you avoid a rebase that you might
  later have to go back and fix.
</p>

<h2>Summary</h2>

<p>
  You’ve covered most of the major ways that you can customize your Git client
  and server to best fit your workflow and projects. You’ve learned about all
  sorts of configuration settings, file-based attributes, and event hooks, and
  you’ve built an example policy-enforcing server. You should now be able to
  make Git fit nearly any workflow you can dream up.
</p>

<h1>Git and Other Systems</h1>

<p>
  The world isn’t perfect. Usually, you can’t immediately switch every project
  you come in contact with to Git. Sometimes you’re stuck on a project using
  another VCS, and many times that system is Subversion. You’ll spend the first
  part of this chapter learning about <code>git svn</code>, the bidirectional Subversion gateway tool in Git.
</p>

<p>
  At some point, you may want to convert your existing project to Git. The
  second part of this chapter covers how to migrate your project into Git:
  first from Subversion, then from Perforce, and finally via a custom import
  script for a nonstandard importing case.
</p>

<h2>Git and Subversion</h2>

<p>
  Currently, the majority of open source development projects and a large
  number of corporate projects use Subversion to manage their source code. It’s
  the most popular open source VCS and has been around for nearly a decade.
  It’s also very similar in many ways to CVS, which was the big boy of the
  source-control world before that.
</p>

<p>
  One of Git’s great features is a bidirectional bridge to Subversion called <code>git svn</code>. This tool allows you to use Git as a valid client to a Subversion server,
  so you can use all the local features of Git and then push to a Subversion
  server as if you were using Subversion locally. This means you can do local
  branching and merging, use the staging area, use rebasing and cherry-picking,
  and so on, while your collaborators continue to work in their dark and
  ancient ways. It’s a good way to sneak Git into the corporate environment and
  help your fellow developers become more efficient while you lobby to get the
  infrastructure changed to support Git fully. The Subversion bridge is the
  gateway drug to the DVCS world.
</p>

<h3>git svn</h3>

<p>
  The base command in Git for all the Subversion bridging commands is <code>git svn</code>. You preface everything with that. It takes quite a few commands, so you’ll
  learn about the common ones while going through a few small workflows.
</p>

<p>
  It’s important to note that when you’re using <code>git svn</code>, you’re interacting with Subversion, which is a system that is far less
  sophisticated than Git. Although you can easily do local branching and
  merging, it’s generally best to keep your history as linear as possible by
  rebasing your work and avoiding doing things like simultaneously interacting
  with a Git remote repository.
</p>

<p>
  Don’t rewrite your history and try to push again, and don’t push to a
  parallel Git repository to collaborate with fellow Git developers at the same
  time. Subversion can have only a single linear history, and confusing it is
  very easy. If you’re working with a team, and some are using SVN and others
  are using Git, make sure everyone is using the SVN server to collaborate —
  doing so will make your life easier.
</p>

<h3>Setting Up</h3>

<p>
  To demonstrate this functionality, you need a typical SVN repository that you
  have write access to. If you want to copy these examples, you’ll have to make
  a writeable copy of my test repository. In order to do that easily, you can
  use a tool called <code>svnsync</code> that comes with more recent versions of Subversion — it should be
  distributed with at least 1.4. For these tests, I created a new Subversion
  repository on Google code that was a partial copy of the <code>protobuf</code> project, which is a tool that encodes structured data for network
  transmission.
</p>

<p>
  To follow along, you first need to create a new local Subversion repository:
</p>

<pre>
$ mkdir /tmp/test-svn
$ svnadmin create /tmp/test-svn
</pre>

<p>
  Then, enable all users to change revprops — the easy way is to add a
  pre-revprop-change script that always exits 0:
</p>

<pre>
$ cat /tmp/test-svn/hooks/pre-revprop-change
#!/bin/sh
exit 0;
$ chmod +x /tmp/test-svn/hooks/pre-revprop-change
</pre>

<p>
  You can now sync this project to your local machine by calling <code>svnsync init</code> with the to and from repositories.
</p>

<pre>
$ svnsync init file:///tmp/test-svn http://progit-example.googlecode.com/svn/
</pre>

<p>
  This sets up the properties to run the sync. You can then clone the code by
  running
</p>

<pre>
$ svnsync sync file:///tmp/test-svn
Committed revision 1.
Copied properties for revision 1.
Committed revision 2.
Copied properties for revision 2.
Committed revision 3.
...
</pre>

<p>
  Although this operation may take only a few minutes, if you try to copy the
  original repository to another remote repository instead of a local one, the
  process will take nearly an hour, even though there are fewer than 100
  commits. Subversion has to clone one revision at a time and then push it back
  into another repository — it’s ridiculously inefficient, but it’s the only
  easy way to do this.
</p>

<h3>Getting Started</h3>

<p>
  Now that you have a Subversion repository to which you have write access, you
  can go through a typical workflow. You’ll start with the <code>git svn clone</code> command, which imports an entire Subversion repository into a local Git
  repository. Remember that if you’re importing from a real hosted Subversion
  repository, you should replace the <code>file:///tmp/test-svn</code> here with the URL of your Subversion repository:
</p>

<pre>
$ git svn clone file:///tmp/test-svn -T trunk -b branches -t tags
Initialized empty Git repository in /Users/schacon/projects/testsvnsync/svn/.git/
r1 = b4e387bc68740b5af56c2a5faf4003ae42bd135c (trunk)
      A    m4/acx_pthread.m4
      A    m4/stl_hash.m4
...
r75 = d1957f3b307922124eec6314e15bcda59e3d9610 (trunk)
Found possible branch point: file:///tmp/test-svn/trunk =&gt; \
    file:///tmp/test-svn /branches/my-calc-branch, 75
Found branch parent: (my-calc-branch) d1957f3b307922124eec6314e15bcda59e3d9610
Following parent with do_switch
Successfully followed parent
r76 = 8624824ecc0badd73f40ea2f01fce51894189b01 (my-calc-branch)
Checked out HEAD:
 file:///tmp/test-svn/branches/my-calc-branch r76
</pre>

<p>
  This runs the equivalent of two commands — <code>git svn init</code> followed by <code>git svn fetch</code> — on the URL you provide. This can take a while. The test project has only
  about 75 commits and the codebase isn’t that big, so it takes just a few
  minutes. However, Git has to check out each version, one at a time, and
  commit it individually. For a project with hundreds or thousands of commits,
  this can literally take hours or even days to finish.
</p>

<p>
  The <code>-T trunk -b branches -t tags</code> part tells Git that this Subversion repository follows the basic branching
  and tagging conventions. If you name your trunk, branches, or tags
  differently, you can change these options. Because this is so common, you can
  replace this entire part with <code>-s</code>, which means standard layout and implies all those options. The following
  command is equivalent:
</p>

<pre>
$ git svn clone file:///tmp/test-svn -s
</pre>

<p>
  At this point, you should have a valid Git repository that has imported your
  branches and tags:
</p>

<pre>
$ git branch -a
* master
  my-calc-branch
  tags/2.0.2
  tags/release-2.0.1
  tags/release-2.0.2
  tags/release-2.0.2rc1
  trunk
</pre>

<p>
  It’s important to note how this tool namespaces your remote references
  differently. When you’re cloning a normal Git repository, you get all the
  branches on that remote server available locally as something like <code>origin/[branch]</code> - namespaced by the name of the remote. However, <code>git svn</code> assumes that you won’t have multiple remotes and saves all its references to
  points on the remote server with no namespacing. You can use the Git plumbing
  command <code>show-ref</code> to look at all your full reference names:
</p>

<pre>
$ git show-ref
1cbd4904d9982f386d87f88fce1c24ad7c0f0471 refs/heads/master
aee1ecc26318164f355a883f5d99cff0c852d3c4 refs/remotes/my-calc-branch
03d09b0e2aad427e34a6d50ff147128e76c0e0f5 refs/remotes/tags/2.0.2
50d02cc0adc9da4319eeba0900430ba219b9c376 refs/remotes/tags/release-2.0.1
4caaa711a50c77879a91b8b90380060f672745cb refs/remotes/tags/release-2.0.2
1c4cb508144c513ff1214c3488abe66dcb92916f refs/remotes/tags/release-2.0.2rc1
1cbd4904d9982f386d87f88fce1c24ad7c0f0471 refs/remotes/trunk
</pre>

<p>A normal Git repository looks more like this:</p>

<pre>
$ git show-ref
83e38c7a0af325a9722f2fdc56b10188806d83a1 refs/heads/master
3e15e38c198baac84223acfc6224bb8b99ff2281 refs/remotes/gitserver/master
0a30dd3b0c795b80212ae723640d4e5d48cabdff refs/remotes/origin/master
25812380387fdd55f916652be4881c6f11600d6f refs/remotes/origin/testing
</pre>

<p>
  You have two remote servers: one named <code>gitserver</code> with a <code>master</code> branch; and another named <code>origin</code> with two branches, <code>master</code> and <code>testing</code>.
</p>

<p>
  Notice how in the example of remote references imported from <code>git svn</code>, tags are added as remote branches, not as real Git tags. Your Subversion
  import looks like it has a remote named tags with branches under it.
</p>

<h3>Committing Back to Subversion</h3>

<p>
  Now that you have a working repository, you can do some work on the project
  and push your commits back upstream, using Git effectively as a SVN client.
  If you edit one of the files and commit it, you have a commit that exists in
  Git locally that doesn’t exist on the Subversion server:
</p>

<pre>
$ git commit -am 'Adding git-svn instructions to the README'
[master 97031e5] Adding git-svn instructions to the README
 1 files changed, 1 insertions(+), 1 deletions(-)
</pre>

<p>
  Next, you need to push your change upstream. Notice how this changes the way
  you work with Subversion — you can do several commits offline and then push
  them all at once to the Subversion server. To push to a Subversion server,
  you run the <code>git svn dcommit</code> command:
</p>

<pre>
$ git svn dcommit
Committing to file:///tmp/test-svn/trunk ...
       M      README.txt
Committed r79
       M      README.txt
r79 = 938b1a547c2cc92033b74d32030e86468294a5c8 (trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
</pre>

<p>
  This takes all the commits you’ve made on top of the Subversion server code,
  does a Subversion commit for each, and then rewrites your local Git commit to
  include a unique identifier. This is important because it means that all the
  SHA-1 checksums for your commits change. Partly for this reason, working with
  Git-based remote versions of your projects concurrently with a Subversion
  server isn’t a good idea. If you look at the last commit, you can see the new
  <code>git-svn-id</code> that was added:
</p>

<pre>
$ git log -1
commit 938b1a547c2cc92033b74d32030e86468294a5c8
Author: schacon &lt;schacon@4c93b258-373f-11de-be05-5f7a86268029&gt;
Date:   Sat May 2 22:06:44 2009 +0000

    Adding git-svn instructions to the README

    git-svn-id: file:///tmp/test-svn/trunk@79 4c93b258-373f-11de-be05-5f7a86268029
</pre>

<p>
  Notice that the SHA checksum that originally started with <code>97031e5</code> when you committed now begins with <code>938b1a5</code>. If you want to push to both a Git server and a Subversion server, you have
  to push (<code>dcommit</code>) to the Subversion server first, because that action changes your commit
  data.
</p>

<h3>Pulling in New Changes</h3>

<p>
  If you’re working with other developers, then at some point one of you will
  push, and then the other one will try to push a change that conflicts. That
  change will be rejected until you merge in their work. In <code>git svn</code>, it looks like this:
</p>

<pre>
$ git svn dcommit
Committing to file:///tmp/test-svn/trunk ...
Merge conflict during commit: Your file or directory 'README.txt' is probably \
out-of-date: resource out of date; try updating at /Users/schacon/libexec/git-\
core/git-svn line 482
</pre>

<p>
  To resolve this situation, you can run <code>git svn rebase</code>, which pulls down any changes on the server that you don’t have yet and
  rebases any work you have on top of what is on the server:
</p>

<pre>
$ git svn rebase
       M      README.txt
r80 = ff829ab914e8775c7c025d741beb3d523ee30bc4 (trunk)
First, rewinding head to replay your work on top of it...
Applying: first user change
</pre>

<p>
  Now, all your work is on top of what is on the Subversion server, so you can
  successfully <code>dcommit</code>:
</p>

<pre>
$ git svn dcommit
Committing to file:///tmp/test-svn/trunk ...
       M      README.txt
Committed r81
       M      README.txt
r81 = 456cbe6337abe49154db70106d1836bc1332deed (trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
</pre>

<p>
  It’s important to remember that unlike Git, which requires you to merge
  upstream work you don’t yet have locally before you can push, <code>git svn</code> makes you do that only if the changes conflict. If someone else pushes a
  change to one file and then you push a change to another file, your <code>dcommit</code> will work fine:
</p>

<pre>
$ git svn dcommit
Committing to file:///tmp/test-svn/trunk ...
       M      configure.ac
Committed r84
       M      autogen.sh
r83 = 8aa54a74d452f82eee10076ab2584c1fc424853b (trunk)
       M      configure.ac
r84 = cdbac939211ccb18aa744e581e46563af5d962d0 (trunk)
W: d2f23b80f67aaaa1f6f5aaef48fce3263ac71a92 and refs/remotes/trunk differ, \
  using rebase:
:100755 100755 efa5a59965fbbb5b2b0a12890f1b351bb5493c18 \
  015e4c98c482f0fa71e4d5434338014530b37fa6 M   autogen.sh
First, rewinding head to replay your work on top of it...
Nothing to do.
</pre>

<p>
  This is important to remember, because the outcome is a project state that
  didn’t exist on either of your computers when you pushed. If the changes are
  incompatible but don’t conflict, you may get issues that are difficult to
  diagnose. This is different than using a Git server — in Git, you can fully
  test the state on your client system before publishing it, whereas in SVN,
  you can’t ever be certain that the states immediately before commit and after
  commit are identical.
</p>

<p>
  You should also run this command to pull in changes from the Subversion
  server, even if you’re not ready to commit yourself. You can run <code>git svn fetch</code> to grab the new data, but <code>git svn rebase</code> does the fetch and then updates your local commits.
</p>

<pre>
$ git svn rebase
       M      generate_descriptor_proto.sh
r82 = bd16df9173e424c6f52c337ab6efa7f7643282f1 (trunk)
First, rewinding head to replay your work on top of it...
Fast-forwarded master to refs/remotes/trunk.
</pre>

<p>
  Running <code>git svn rebase</code> every once in a while makes sure your code is always up to date. You need to
  be sure your working directory is clean when you run this, though. If you
  have local changes, you must either stash your work or temporarily commit it
  before running <code>git svn rebase</code> — otherwise, the command will stop if it sees that the rebase will result in
  a merge conflict.
</p>

<h3>Git Branching Issues</h3>

<p>
  When you’ve become comfortable with a Git workflow, you’ll likely create
  topic branches, do work on them, and then merge them in. If you’re pushing to
  a Subversion server via <code>git svn</code>, you may want to rebase your work onto a single branch each time instead of
  merging branches together. The reason to prefer rebasing is that Subversion
  has a linear history and doesn’t deal with merges like Git does, so <code>git svn</code> follows only the first parent when converting the snapshots into Subversion
  commits.
</p>

<p>
  Suppose your history looks like the following: you created an <code>experiment</code> branch, did two commits, and then merged them back into <code>master</code>. When you <code>dcommit</code>, you see output like this:
</p>

<pre>
$ git svn dcommit
Committing to file:///tmp/test-svn/trunk ...
       M      CHANGES.txt
Committed r85
       M      CHANGES.txt
r85 = 4bfebeec434d156c36f2bcd18f4e3d97dc3269a2 (trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
COPYING.txt: locally modified
INSTALL.txt: locally modified
       M      COPYING.txt
       M      INSTALL.txt
Committed r86
       M      INSTALL.txt
       M      COPYING.txt
r86 = 2647f6b86ccfcaad4ec58c520e369ec81f7c283c (trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
</pre>

<p>
  Running <code>dcommit</code> on a branch with merged history works fine, except that when you look at
  your Git project history, it hasn’t rewritten either of the commits you made
  on the <code>experiment</code> branch — instead, all those changes appear in the SVN version of the single
  merge commit.
</p>

<p>
  When someone else clones that work, all they see is the merge commit with all
  the work squashed into it; they don’t see the commit data about where it came
  from or when it was committed.
</p>

<h3>Subversion Branching</h3>

<p>
  Branching in Subversion isn’t the same as branching in Git; if you can avoid
  using it much, that’s probably best. However, you can create and commit to
  branches in Subversion using <code>git svn</code>.
</p>

<p>Creating a New SVN Branch</p>

<p>
  To create a new branch in Subversion, you run <code>git svn branch [branchname]</code>:
</p>

<pre>
$ git svn branch opera
Copying file:///tmp/test-svn/trunk at r87 to file:///tmp/test-svn/branches/opera...
Found possible branch point: file:///tmp/test-svn/trunk =&gt; \
  file:///tmp/test-svn/branches/opera, 87
Found branch parent: (opera) 1f6bfe471083cbca06ac8d4176f7ad4de0d62e5f
Following parent with do_switch
Successfully followed parent
r89 = 9b6fe0b90c5c9adf9165f700897518dbc54a7cbf (opera)
</pre>

<p>
  This does the equivalent of the <code>svn copy trunk branches/opera</code> command in Subversion and operates on the Subversion server. It’s important
  to note that it doesn’t check you out into that branch; if you commit at this
  point, that commit will go to <code>trunk</code> on the server, not <code>opera</code>.
</p>

<h3>Switching Active Branches</h3>

<p>
  Git figures out what branch your dcommits go to by looking for the tip of any
  of your Subversion branches in your history — you should have only one, and
  it should be the last one with a <code>git-svn-id</code> in your current branch history.
</p>

<p>
  If you want to work on more than one branch simultaneously, you can set up
  local branches to <code>dcommit</code> to specific Subversion branches by starting them at the imported Subversion
  commit for that branch. If you want an <code>opera</code> branch that you can work on separately, you can run
</p>

<pre>
$ git branch opera remotes/opera
</pre>

<p>
  Now, if you want to merge your <code>opera</code> branch into <code>trunk</code> (your <code>master</code> branch), you can do so with a normal <code>git merge</code>. But you need to provide a descriptive commit message (via <code>-m</code>), or the merge will say "Merge branch opera" instead of something useful.
</p>

<p>
  Remember that although you’re using <code>git merge</code> to do this operation, and the merge likely will be much easier than it would
  be in Subversion (because Git will automatically detect the appropriate merge
  base for you), this isn’t a normal Git merge commit. You have to push this
  data back to a Subversion server that can’t handle a commit that tracks more
  than one parent; so, after you push it up, it will look like a single commit
  that squashed in all the work of another branch under a single commit. After
  you merge one branch into another, you can’t easily go back and continue
  working on that branch, as you normally can in Git. The <code>dcommit</code> command that you run erases any information that says what branch was merged
  in, so subsequent merge-base calculations will be wrong — the dcommit makes
  your <code>git merge</code> result look like you ran <code>git merge --squash</code>. Unfortunately, there’s no good way to avoid this situation — Subversion
  can’t store this information, so you’ll always be crippled by its limitations
  while you’re using it as your server. To avoid issues, you should delete the
  local branch (in this case, <code>opera</code>) after you merge it into trunk.
</p>

<h3>Subversion Commands</h3>

<p>
  The <code>git svn</code> toolset provides a number of commands to help ease the transition to Git by
  providing some functionality that’s similar to what you had in Subversion.
  Here are a few commands that give you what Subversion used to.
</p>

<p>SVN Style History</p>

<p>
  If you’re used to Subversion and want to see your history in SVN output
  style, you can run <code>git svn log</code> to view your commit history in SVN formatting:
</p>

<pre>
$ git svn log
------------------------------------------------------------------------
r87 | schacon | 2009-05-02 16:07:37 -0700 (Sat, 02 May 2009) | 2 lines

autogen change

------------------------------------------------------------------------
r86 | schacon | 2009-05-02 16:00:21 -0700 (Sat, 02 May 2009) | 2 lines

Merge branch 'experiment'

------------------------------------------------------------------------
r85 | schacon | 2009-05-02 16:00:09 -0700 (Sat, 02 May 2009) | 2 lines

updated the changelog
</pre>

<p>
  You should know two important things about <code>git svn log</code>. First, it works offline, unlike the real <code>svn log</code> command, which asks the Subversion server for the data. Second, it only
  shows you commits that have been committed up to the Subversion server. Local
  Git commits that you haven’t dcommited don’t show up; neither do commits that
  people have made to the Subversion server in the meantime. It’s more like the
  last known state of the commits on the Subversion server.
</p>

<p>SVN Annotation</p>

<p>
  Much as the <code>git svn log</code> command simulates the <code>svn log</code> command offline, you can get the equivalent of <code>svn annotate</code> by running <code>git svn blame [FILE]</code>. The output looks like this:
</p>

<pre>
$ git svn blame README.txt
 2   temporal Protocol Buffers - Google's data interchange format
 2   temporal Copyright 2008 Google Inc.
 2   temporal http://code.google.com/apis/protocolbuffers/
 2   temporal
22   temporal C++ Installation - Unix
22   temporal =======================
 2   temporal
79    schacon Committing in git-svn.
78    schacon
 2   temporal To build and install the C++ Protocol Buffer runtime and the Protocol
 2   temporal Buffer compiler (protoc) execute the following:
 2   temporal
</pre>

<p>
  Again, it doesn’t show commits that you did locally in Git or that have been
  pushed to Subversion in the meantime.
</p>

<p>SVN Server Information</p>

<p>
  You can also get the same sort of information that <code>svn info</code> gives you by running <code>git svn info</code>:
</p>

<pre>
$ git svn info
Path: .
URL: https://schacon-test.googlecode.com/svn/trunk
Repository Root: https://schacon-test.googlecode.com/svn
Repository UUID: 4c93b258-373f-11de-be05-5f7a86268029
Revision: 87
Node Kind: directory
Schedule: normal
Last Changed Author: schacon
Last Changed Rev: 87
Last Changed Date: 2009-05-02 16:07:37 -0700 (Sat, 02 May 2009)
</pre>

<p>
  This is like <code>blame</code> and <code>log</code> in that it runs offline and is up to date only as of the last time you
  communicated with the Subversion server.
</p>

<p>
  Tip: If your build scripts expect to be able to run <code>svn info</code> then providing a wrapper around git will often work. Here is example to
  experiment with
</p>

<pre>
#!/usr/bin/env bash

if git rev-parse --git-dir &gt; /dev/null 2&gt;&amp;1 &amp;&amp; [[ $1 == "info" ]] ; then
  git svn info
else 
  /usr/local/bin/svn "$@"
fi
</pre>

<p>Ignoring What Subversion Ignores</p>

<p>
  If you clone a Subversion repository that has <code>svn:ignore</code> properties set anywhere, you’ll likely want to set corresponding <code>.gitignore</code> files so you don’t accidentally commit files that you shouldn’t. <code>git svn</code> has two commands to help with this issue. The first is <code>git svn create-ignore</code>, which automatically creates corresponding <code>.gitignore</code> files for you so your next commit can include them.
</p>

<p>
  The second command is <code>git svn show-ignore</code>, which prints to stdout the lines you need to put in a <code>.gitignore</code> file so you can redirect the output into your project exclude file:
</p>

<pre>
$ git svn show-ignore &gt; .git/info/exclude
</pre>

<p>
  That way, you don’t litter the project with <code>.gitignore</code> files. This is a good option if you’re the only Git user on a Subversion
  team, and your teammates don’t want <code>.gitignore</code> files in the project.
</p>

<h3>Git-Svn Summary</h3>

<p>
  The <code>git svn</code> tools are useful if you’re stuck with a Subversion server for now or are
  otherwise in a development environment that necessitates running a Subversion
  server. You should consider it crippled Git, however, or you’ll hit issues in
  translation that may confuse you and your collaborators. To stay out of
  trouble, try to follow these guidelines:
</p>

<ul>
  <li>
    <p>
      Keep a linear Git history that doesn’t contain merge commits made by <code>git merge</code>. Rebase any work you do outside of your mainline branch back onto it;
      don’t merge it in.
    </p>
  </li>

  <li>
    <p>
      Don’t set up and collaborate on a separate Git server. Possibly have one
      to speed up clones for new developers, but don’t push anything to it that
      doesn’t have a <code>git-svn-id</code> entry. You may even want to add a <code>pre-receive</code> hook that checks each commit message for a <code>git-svn-id</code> and rejects pushes that contain commits without it.
    </p>
  </li>
</ul>

<p>
  If you follow those guidelines, working with a Subversion server can be more
  bearable. However, if it’s possible to move to a real Git server, doing so
  can gain your team a lot more.
</p>

<h2>Migrating to Git</h2>

<p>
  If you have an existing codebase in another VCS but you’ve decided to start
  using Git, you must migrate your project one way or another. This section
  goes over some importers that are included with Git for common systems and
  then demonstrates how to develop your own custom importer.
</p>

<h3>Importing</h3>

<p>
  You’ll learn how to import data from two of the bigger professionally used
  SCM systems — Subversion and Perforce — both because they make up the
  majority of users I hear of who are currently switching, and because
  high-quality tools for both systems are distributed with Git.
</p>

<h3>Subversion</h3>

<p>
  If you read the previous section about using <code>git svn</code>, you can easily use those instructions to <code>git svn clone</code> a repository; then, stop using the Subversion server, push to a new Git
  server, and start using that. If you want the history, you can accomplish
  that as quickly as you can pull the data out of the Subversion server (which
  may take a while).
</p>

<p>
  However, the import isn’t perfect; and because it will take so long, you may
  as well do it right. The first problem is the author information. In
  Subversion, each person committing has a user on the system who is recorded
  in the commit information. The examples in the previous section show <code>schacon</code> in some places, such as the <code>blame</code> output and the <code>git svn log</code>. If you want to map this to better Git author data, you need a mapping from
  the Subversion users to the Git authors. Create a file called <code>users.txt</code> that has this mapping in a format like this:
</p>

<pre>
schacon = Scott Chacon &lt;schacon@geemail.com&gt;
selse = Someo Nelse &lt;selse@geemail.com&gt;
</pre>

<p>To get a list of the author names that SVN uses, you can run this:</p>

<pre>
$ svn log ^/ --xml | grep -P "^&lt;author" | sort -u | \
      perl -pe 's/&lt;author&gt;(.*?)&lt;\/author&gt;/$1 = /' &gt; users.txt
</pre>

<p>
  That gives you the log output in XML format — you can look for the authors,
  create a unique list, and then strip out the XML. (Obviously this only works
  on a machine with <code>grep</code>, <code>sort</code>, and <code>perl</code> installed.) Then, redirect that output into your users.txt file so you can
  add the equivalent Git user data next to each entry.
</p>

<p>
  You can provide this file to <code>git svn</code> to help it map the author data more accurately. You can also tell <code>git svn</code> not to include the metadata that Subversion normally imports, by passing <code>--no-metadata</code> to the <code>clone</code> or <code>init</code> command. This makes your <code>import</code> command look like this:
</p>

<pre>
$ git svn clone http://my-project.googlecode.com/svn/ \
      --authors-file=users.txt --no-metadata -s my_project
</pre>

<p>
  Now you should have a nicer Subversion import in your <code>my_project</code> directory. Instead of commits that look like this
</p>

<pre>
commit 37efa680e8473b615de980fa935944215428a35a
Author: schacon &lt;schacon@4c93b258-373f-11de-be05-5f7a86268029&gt;
Date:   Sun May 3 00:12:22 2009 +0000

    fixed install - go to trunk

    git-svn-id: https://my-project.googlecode.com/svn/trunk@94 4c93b258-373f-11de-
    be05-5f7a86268029
</pre>

<p>they look like this:</p>

<pre>
commit 03a8785f44c8ea5cdb0e8834b7c8e6c469be2ff2
Author: Scott Chacon &lt;schacon@geemail.com&gt;
Date:   Sun May 3 00:12:22 2009 +0000

    fixed install - go to trunk
</pre>

<p>
  Not only does the Author field look a lot better, but the <code>git-svn-id</code> is no longer there, either.
</p>

<p>
  You need to do a bit of <code>post-import</code> cleanup. For one thing, you should clean up the weird references that <code>git svn</code> set up. First you’ll move the tags so they’re actual tags rather than
  strange remote branches, and then you’ll move the rest of the branches so
  they’re local.
</p>

<p>To move the tags to be proper Git tags, run</p>

<pre>
$ git for-each-ref refs/remotes/tags | cut -d / -f 4- | grep -v @ | while read tagname; do git tag "$tagname" "tags/$tagname"; git branch -r -d "tags/$tagname"; done
</pre>

<p>
  This takes the references that were remote branches that started with <code>tag/</code> and makes them real (lightweight) tags.
</p>

<p>
  Next, move the rest of the references under <code>refs/remotes</code> to be local branches:
</p>

<pre>
$ git for-each-ref refs/remotes | cut -d / -f 3- | grep -v @ | while read branchname; do git branch "$branchname" "refs/remotes/$branchname"; git branch -r -d "$branchname"; done
</pre>

<p>
  Now all the old branches are real Git branches and all the old tags are real
  Git tags. The last thing to do is add your new Git server as a remote and
  push to it. Here is an example of adding your server as a remote:
</p>

<pre>
$ git remote add origin git@my-git-server:myrepository.git
</pre>

<p>
  Because you want all your branches and tags to go up, you can now run this:
</p>

<pre>
$ git push origin --all
$ git push origin --tags
</pre>

<p>
  All your branches and tags should be on your new Git server in a nice, clean
  import.
</p>

<h3>Perforce</h3>

<p>
  The next system you’ll look at importing from is Perforce. A Perforce
  importer is also distributed with Git. If you have a version of Git earlier
  than 1.7.11, then the importer is only available in the <code>contrib</code> section of the source code. In that case you must get the Git source code,
  which you can download from git.kernel.org:
</p>

<pre>
$ git clone git://git.kernel.org/pub/scm/git/git.git
$ cd git/contrib/fast-import
</pre>

<p>
  In this <code>fast-import</code> directory, you should find an executable Python script named <code>git-p4</code>. You must have Python and the <code>p4</code> tool installed on your machine for this import to work. For example, you’ll
  import the Jam project from the Perforce Public Depot. To set up your client,
  you must export the P4PORT environment variable to point to the Perforce
  depot:
</p>

<pre>
$ export P4PORT=public.perforce.com:1666
</pre>

<p>
  Run the <code>git-p4 clone</code> command to import the Jam project from the Perforce server, supplying the
  depot and project path and the path into which you want to import the
  project:
</p>

<pre>
$ git-p4 clone //public/jam/src@all /opt/p4import
Importing from //public/jam/src@all into /opt/p4import
Reinitialized existing Git repository in /opt/p4import/.git/
Import destination: refs/remotes/p4/master
Importing revision 4409 (100%)
</pre>

<p>
  If you go to the <code>/opt/p4import</code> directory and run <code>git log</code>, you can see your imported work:
</p>

<pre>
$ git log -2
commit 1fd4ec126171790efd2db83548b85b1bbbc07dc2
Author: Perforce staff &lt;support@perforce.com&gt;
Date:   Thu Aug 19 10:18:45 2004 -0800

    Drop 'rc3' moniker of jam-2.5.  Folded rc2 and rc3 RELNOTES into
    the main part of the document.  Built new tar/zip balls.

    Only 16 months later.

    [git-p4: depot-paths = "//public/jam/src/": change = 4409]

commit ca8870db541a23ed867f38847eda65bf4363371d
Author: Richard Geiger &lt;rmg@perforce.com&gt;
Date:   Tue Apr 22 20:51:34 2003 -0800

    Update derived jamgram.c

    [git-p4: depot-paths = "//public/jam/src/": change = 3108]
</pre>

<p>
  You can see the <code>git-p4</code> identifier in each commit. It’s fine to keep that identifier there, in case
  you need to reference the Perforce change number later. However, if you’d
  like to remove the identifier, now is the time to do so — before you start
  doing work on the new repository. You can use <code>git filter-branch</code> to remove the identifier strings en masse:
</p>

<pre>
$ git filter-branch --msg-filter '
        sed -e "/^\[git-p4:/d"
'
Rewrite 1fd4ec126171790efd2db83548b85b1bbbc07dc2 (123/123)
Ref 'refs/heads/master' was rewritten
</pre>

<p>
  If you run <code>git log</code>, you can see that all the SHA-1 checksums for the commits have changed, but
  the <code>git-p4</code> strings are no longer in the commit messages:
</p>

<pre>
$ git log -2
commit 10a16d60cffca14d454a15c6164378f4082bc5b0
Author: Perforce staff &lt;support@perforce.com&gt;
Date:   Thu Aug 19 10:18:45 2004 -0800

    Drop 'rc3' moniker of jam-2.5.  Folded rc2 and rc3 RELNOTES into
    the main part of the document.  Built new tar/zip balls.

    Only 16 months later.

commit 2b6c6db311dd76c34c66ec1c40a49405e6b527b2
Author: Richard Geiger &lt;rmg@perforce.com&gt;
Date:   Tue Apr 22 20:51:34 2003 -0800

    Update derived jamgram.c
</pre>

<p>Your import is ready to push up to your new Git server.</p>

<h3>A Custom Importer</h3>

<p>
  If your system isn’t Subversion or Perforce, you should look for an importer
  online — quality importers are available for CVS, Clear Case, Visual Source
  Safe, even a directory of archives. If none of these tools works for you, you
  have a rarer tool, or you otherwise need a more custom importing process, you
  should use <code>git fast-import</code>. This command reads simple instructions from stdin to write specific Git
  data. It’s much easier to create Git objects this way than to run the raw Git
  commands or try to write the raw objects (see Chapter 9 for more
  information). This way, you can write an import script that reads the
  necessary information out of the system you’re importing from and prints
  straightforward instructions to stdout. You can then run this program and
  pipe its output through <code>git fast-import</code>.
</p>

<p>
  To quickly demonstrate, you’ll write a simple importer. Suppose you work in
  current, you back up your project by occasionally copying the directory into
  a time-stamped <code>back_YYYY_MM_DD</code> backup directory, and you want to import this into Git. Your directory
  structure looks like this:
</p>

<pre>
$ ls /opt/import_from
back_2009_01_02
back_2009_01_04
back_2009_01_14
back_2009_02_03
current
</pre>

<p>
  In order to import a Git directory, you need to review how Git stores its
  data. As you may remember, Git is fundamentally a linked list of commit
  objects that point to a snapshot of content. All you have to do is tell <code>fast-import</code> what the content snapshots are, what commit data points to them, and the
  order they go in. Your strategy will be to go through the snapshots one at a
  time and create commits with the contents of each directory, linking each
  commit back to the previous one.
</p>

<p>
  As you did in the "An Example Git Enforced Policy" section of Chapter 7,
  we’ll write this in Ruby, because it’s what I generally work with and it
  tends to be easy to read. You can write this example pretty easily in
  anything you’re familiar with — it just needs to print the appropriate
  information to stdout. And, if you are running on Windows, this means you’ll
  need to take special care to not introduce carriage returns at the end your
  lines — <code>git fast-import</code> is very particular about just wanting line feeds (LF) not the carriage
  return line feeds (CRLF) that Windows uses.
</p>

<p>
  To begin, you’ll change into the target directory and identify every
  subdirectory, each of which is a snapshot that you want to import as a
  commit. You’ll change into each subdirectory and print the commands necessary
  to export it. Your basic main loop looks like this:
</p>

<pre>
last_mark = nil

# loop through the directories
Dir.chdir(ARGV[0]) do
  Dir.glob("*").each do |dir|
    next if File.file?(dir)

    # move into the target directory
    Dir.chdir(dir) do
      last_mark = print_export(dir, last_mark)
    end
  end
end
</pre>

<p>
  You run <code>print_export</code> inside each directory, which takes the manifest and mark of the previous
  snapshot and returns the manifest and mark of this one; that way, you can
  link them properly. "Mark" is the <code>fast-import</code> term for an identifier you give to a commit; as you create commits, you give
  each one a mark that you can use to link to it from other commits. So, the
  first thing to do in your <code>print_export</code> method is generate a mark from the directory name:
</p>

<pre>
mark = convert_dir_to_mark(dir)
</pre>

<p>
  You’ll do this by creating an array of directories and using the index value
  as the mark, because a mark must be an integer. Your method looks like this:
</p>

<pre>
$marks = []
def convert_dir_to_mark(dir)
  if !$marks.include?(dir)
    $marks &lt;&lt; dir
  end
  ($marks.index(dir) + 1).to_s
end
</pre>

<p>
  Now that you have an integer representation of your commit, you need a date
  for the commit metadata. Because the date is expressed in the name of the
  directory, you’ll parse it out. The next line in your <code>print_export</code> file is
</p>

<pre>
date = convert_dir_to_date(dir)
</pre>

<p>where <code>convert_dir_to_date</code> is defined as</p>

<pre>
def convert_dir_to_date(dir)
  if dir == 'current'
    return Time.now().to_i
  else
    dir = dir.gsub('back_', '')
    (year, month, day) = dir.split('_')
    return Time.local(year, month, day).to_i
  end
end
</pre>

<p>
  That returns an integer value for the date of each directory. The last piece
  of meta-information you need for each commit is the committer data, which you
  hardcode in a global variable:
</p>

<pre>
$author = 'Scott Chacon &lt;schacon@example.com&gt;'
</pre>

<p>
  Now you’re ready to begin printing out the commit data for your importer. The
  initial information states that you’re defining a commit object and what
  branch it’s on, followed by the mark you’ve generated, the committer
  information and commit message, and then the previous commit, if any. The
  code looks like this:
</p>

<pre>
# print the import information
puts 'commit refs/heads/master'
puts 'mark :' + mark
puts "committer #{$author} #{date} -0700"
export_data('imported from ' + dir)
puts 'from :' + last_mark if last_mark
</pre>

<p>
  You hardcode the time zone (-0700) because doing so is easy. If you’re
  importing from another system, you must specify the time zone as an offset.
  The commit message must be expressed in a special format:
</p>

<pre>
data (size)\n(contents)
</pre>

<p>
  The format consists of the word data, the size of the data to be read, a
  newline, and finally the data. Because you need to use the same format to
  specify the file contents later, you create a helper method, <code>export_data</code>:
</p>

<pre>
def export_data(string)
  print "data #{string.size}\n#{string}"
end
</pre>

<p>
  All that’s left is to specify the file contents for each snapshot. This is
  easy, because you have each one in a directory — you can print out the <code>deleteall</code> command followed by the contents of each file in the directory. Git will
  then record each snapshot appropriately:
</p>

<pre>
puts 'deleteall'
Dir.glob("**/*").each do |file|
  next if !File.file?(file)
  inline_data(file)
end
</pre>

<p>
  Note: Because many systems think of their revisions as changes from one
  commit to another, fast-import can also take commands with each commit to
  specify which files have been added, removed, or modified and what the new
  contents are. You could calculate the differences between snapshots and
  provide only this data, but doing so is more complex — you may as well give
  Git all the data and let it figure it out. If this is better suited to your
  data, check the <code>fast-import</code> man page for details about how to provide your data in this manner.
</p>

<p>
  The format for listing the new file contents or specifying a modified file
  with the new contents is as follows:
</p>

<pre>
M 644 inline path/to/file
data (size)
(file contents)
</pre>

<p>
  Here, 644 is the mode (if you have executable files, you need to detect and
  specify 755 instead), and inline says you’ll list the contents immediately
  after this line. Your <code>inline_data</code> method looks like this:
</p>

<pre>
def inline_data(file, code = 'M', mode = '644')
  content = File.read(file)
  puts "#{code} #{mode} inline #{file}"
  export_data(content)
end
</pre>

<p>
  You reuse the <code>export_data</code> method you defined earlier, because it’s the same as the way you specified
  your commit message data.
</p>

<p>
  The last thing you need to do is to return the current mark so it can be
  passed to the next iteration:
</p>

<pre>
return mark
</pre>

<p>
  NOTE: If you are running on Windows you’ll need to make sure that you add one
  extra step. As mentioned before, Windows uses CRLF for new line characters
  while <code>git fast-import</code> expects only LF. To get around this problem and make <code>git fast-import</code> happy, you need to tell ruby to use LF instead of CRLF:
</p>

<pre>
$stdout.binmode
</pre>

<p>
  That’s it. If you run this script, you’ll get content that looks something
  like this:
</p>

<pre>
$ ruby import.rb /opt/import_from
commit refs/heads/master
mark :1
committer Scott Chacon &lt;schacon@geemail.com&gt; 1230883200 -0700
data 29
imported from back_2009_01_02deleteall
M 644 inline file.rb
data 12
version two
commit refs/heads/master
mark :2
committer Scott Chacon &lt;schacon@geemail.com&gt; 1231056000 -0700
data 29
imported from back_2009_01_04from :1
deleteall
M 644 inline file.rb
data 14
version three
M 644 inline new.rb
data 16
new version one
(...)
</pre>

<p>
  To run the importer, pipe this output through <code>git fast-import</code> while in the Git repository you want to import into. You can create a new
  directory and then run <code>git init</code> in it for a starting point, and then run your script:
</p>

<pre>
$ git init
Initialized empty Git repository in /opt/import_to/.git/
$ ruby import.rb /opt/import_from | git fast-import
git-fast-import statistics:
---------------------------------------------------------------------
Alloc'd objects:       5000
Total objects:           18 (         1 duplicates                  )
      blobs  :            7 (         1 duplicates          0 deltas)
      trees  :            6 (         0 duplicates          1 deltas)
      commits:            5 (         0 duplicates          0 deltas)
      tags   :            0 (         0 duplicates          0 deltas)
Total branches:           1 (         1 loads     )
      marks:           1024 (         5 unique    )
      atoms:              3
Memory total:          2255 KiB
       pools:          2098 KiB
     objects:           156 KiB
---------------------------------------------------------------------
pack_report: getpagesize()            =       4096
pack_report: core.packedGitWindowSize =   33554432
pack_report: core.packedGitLimit      =  268435456
pack_report: pack_used_ctr            =          9
pack_report: pack_mmap_calls          =          5
pack_report: pack_open_windows        =          1 /          1
pack_report: pack_mapped              =       1356 /       1356
---------------------------------------------------------------------
</pre>

<p>
  As you can see, when it completes successfully, it gives you a bunch of
  statistics about what it accomplished. In this case, you imported 18 objects
  total for 5 commits into 1 branch. Now, you can run <code>git log</code> to see your new history:
</p>

<pre>
$ git log -2
commit 10bfe7d22ce15ee25b60a824c8982157ca593d41
Author: Scott Chacon &lt;schacon@example.com&gt;
Date:   Sun May 3 12:57:39 2009 -0700

    imported from current

commit 7e519590de754d079dd73b44d695a42c9d2df452
Author: Scott Chacon &lt;schacon@example.com&gt;
Date:   Tue Feb 3 01:00:00 2009 -0700

    imported from back_2009_02_03
</pre>

<p>
  There you go — a nice, clean Git repository. It’s important to note that
  nothing is checked out — you don’t have any files in your working directory
  at first. To get them, you must reset your branch to where <code>master</code> is now:
</p>

<pre>
$ ls
$ git reset --hard master
HEAD is now at 10bfe7d imported from current
$ ls
file.rb  lib
</pre>

<p>
  You can do a lot more with the <code>fast-import</code> tool — handle different modes, binary data, multiple branches and merging,
  tags, progress indicators, and more. A number of examples of more complex
  scenarios are available in the <code>contrib/fast-import</code> directory of the Git source code; one of the better ones is the <code>git-p4</code> script I just covered.
</p>

<h2>Summary</h2>

<p>
  You should feel comfortable using Git with Subversion or importing nearly any
  existing repository into a new Git one without losing data. The next chapter
  will cover the raw internals of Git so you can craft every single byte, if
  need be.
</p>

<h1>Git Internals</h1>

<p>
  You may have skipped to this chapter from a previous chapter, or you may have
  gotten here after reading the rest of the book — in either case, this is
  where you’ll go over the inner workings and implementation of Git. I found
  that learning this information was fundamentally important to understanding
  how useful and powerful Git is, but others have argued to me that it can be
  confusing and unnecessarily complex for beginners. Thus, I’ve made this
  discussion the last chapter in the book so you could read it early or later
  in your learning process. I leave it up to you to decide.
</p>

<p>
  Now that you’re here, let’s get started. First, if it isn’t yet clear, Git is
  fundamentally a content-addressable filesystem with a VCS user interface
  written on top of it. You’ll learn more about what this means in a bit.
</p>

<p>
  In the early days of Git (mostly pre 1.5), the user interface was much more
  complex because it emphasized this filesystem rather than a polished VCS. In
  the last few years, the UI has been refined until it’s as clean and easy to
  use as any system out there; but often, the stereotype lingers about the
  early Git UI that was complex and difficult to learn.
</p>

<p>
  The content-addressable filesystem layer is amazingly cool, so I’ll cover
  that first in this chapter; then, you’ll learn about the transport mechanisms
  and the repository maintenance tasks that you may eventually have to deal
  with.
</p>

<h2>Plumbing and Porcelain</h2>

<p>
  This book covers how to use Git with 30 or so verbs such as <code>checkout</code>, <code>branch</code>, <code>remote</code>, and so on. But because Git was initially a toolkit for a VCS rather than a
  full user-friendly VCS, it has a bunch of verbs that do low-level work and
  were designed to be chained together UNIX style or called from scripts. These
  commands are generally referred to as "plumbing" commands, and the more
  user-friendly commands are called "porcelain" commands.
</p>

<p>
  The book’s first eight chapters deal almost exclusively with porcelain
  commands. But in this chapter, you’ll be dealing mostly with the lower-level
  plumbing commands, because they give you access to the inner workings of Git
  and help demonstrate how and why Git does what it does. These commands aren’t
  meant to be used manually on the command line, but rather to be used as
  building blocks for new tools and custom scripts.
</p>

<p>
  When you run <code>git init</code> in a new or existing directory, Git creates the <code>.git</code> directory, which is where almost everything that Git stores and manipulates
  is located. If you want to back up or clone your repository, copying this
  single directory elsewhere gives you nearly everything you need. This entire
  chapter basically deals with the stuff in this directory. Here’s what it
  looks like:
</p>

<pre>
$ ls
HEAD
branches/
config
description
hooks/
index
info/
objects/
refs/
</pre>

<p>
  You may see some other files in there, but this is a fresh <code>git init</code> repository — it’s what you see by default. The <code>branches</code> directory isn’t used by newer Git versions, and the <code>description</code> file is only used by the GitWeb program, so don’t worry about those. The <code>config</code> file contains your project-specific configuration options, and the <code>info</code> directory keeps a global exclude file for ignored patterns that you don’t
  want to track in a <code>.gitignore</code> file. The <code>hooks</code> directory contains your client- or server-side hook scripts, which are
  discussed in detail in Chapter 7.
</p>

<p>
  This leaves four important entries: the <code>HEAD</code> and <code>index</code> files and the <code>objects</code> and <code>refs</code> directories. These are the core parts of Git. The <code>objects</code> directory stores all the content for your database, the <code>refs</code> directory stores pointers into commit objects in that data (branches), the <code>HEAD</code> file points to the branch you currently have checked out, and the <code>index</code> file is where Git stores your staging area information. You’ll now look at
  each of these sections in detail to see how Git operates.
</p>

<h2>Git Objects</h2>

<p>
  Git is a content-addressable filesystem. Great. What does that mean? It means
  that at the core of Git is a simple key-value data store. You can insert any
  kind of content into it, and it will give you back a key that you can use to
  retrieve the content again at any time. To demonstrate, you can use the
  plumbing command <code>hash-object</code>, which takes some data, stores it in your <code>.git</code> directory, and gives you back the key the data is stored as. First, you
  initialize a new Git repository and verify that there is nothing in the <code>objects</code> directory:
</p>

<pre>
$ mkdir test
$ cd test
$ git init
Initialized empty Git repository in /tmp/test/.git/
$ find .git/objects
.git/objects
.git/objects/info
.git/objects/pack
$ find .git/objects -type f
$
</pre>

<p>
  Git has initialized the <code>objects</code> directory and created <code>pack</code> and <code>info</code> subdirectories in it, but there are no regular files. Now, store some text
  in your Git database:
</p>

<pre>
$ echo 'test content' | git hash-object -w --stdin
d670460b4b4aece5915caf5c68d12f560a9fe3e4
</pre>

<p>
  The <code>-w</code> tells <code>hash-object</code> to store the object; otherwise, the command simply tells you what the key
  would be. <code>--stdin</code> tells the command to read the content from stdin; if you don’t specify this,
  <code>hash-object</code> expects the path to a file. The output from the command is a 40-character
  checksum hash. This is the SHA-1 hash — a checksum of the content you’re
  storing plus a header, which you’ll learn about in a bit. Now you can see how
  Git has stored your data:
</p>

<pre>
$ find .git/objects -type f
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4
</pre>

<p>
  You can see a file in the <code>objects</code> directory. This is how Git stores the content initially — as a single file
  per piece of content, named with the SHA-1 checksum of the content and its
  header. The subdirectory is named with the first 2 characters of the SHA, and
  the filename is the remaining 38 characters.
</p>

<p>
  You can pull the content back out of Git with the <code>cat-file</code> command. This command is sort of a Swiss army knife for inspecting Git
  objects. Passing <code>-p</code> to it instructs the <code>cat-file</code> command to figure out the type of content and display it nicely for you:
</p>

<pre>
$ git cat-file -p d670460b4b4aece5915caf5c68d12f560a9fe3e4
test content
</pre>

<p>
  Now, you can add content to Git and pull it back out again. You can also do
  this with content in files. For example, you can do some simple version
  control on a file. First, create a new file and save its contents in your
  database:
</p>

<pre>
$ echo 'version 1' &gt; test.txt
$ git hash-object -w test.txt
83baae61804e65cc73a7201a7252750c76066a30
</pre>

<p>Then, write some new content to the file, and save it again:</p>

<pre>
$ echo 'version 2' &gt; test.txt
$ git hash-object -w test.txt
1f7a7a472abf3dd9643fd615f6da379c4acb3e3a
</pre>

<p>
  Your database contains the two new versions of the file as well as the first
  content you stored there:
</p>

<pre>
$ find .git/objects -type f
.git/objects/1f/7a7a472abf3dd9643fd615f6da379c4acb3e3a
.git/objects/83/baae61804e65cc73a7201a7252750c76066a30
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4
</pre>

<p>Now you can revert the file back to the first version</p>

<pre>
$ git cat-file -p 83baae61804e65cc73a7201a7252750c76066a30 &gt; test.txt
$ cat test.txt
version 1
</pre>

<p>or the second version:</p>

<pre>
$ git cat-file -p 1f7a7a472abf3dd9643fd615f6da379c4acb3e3a &gt; test.txt
$ cat test.txt
version 2
</pre>

<p>
  But remembering the SHA-1 key for each version of your file isn’t practical;
  plus, you aren’t storing the filename in your system — just the content. This
  object type is called a blob. You can have Git tell you the object type of
  any object in Git, given its SHA-1 key, with <code>cat-file -t</code>:
</p>

<pre>
$ git cat-file -t 1f7a7a472abf3dd9643fd615f6da379c4acb3e3a
blob
</pre>

<h3>Tree Objects</h3>

<p>
  The next type you’ll look at is the tree object, which solves the problem of
  storing the filename and also allows you to store a group of files together.
  Git stores content in a manner similar to a UNIX filesystem, but a bit
  simplified. All the content is stored as tree and blob objects, with trees
  corresponding to UNIX directory entries and blobs corresponding more or less
  to inodes or file contents. A single tree object contains one or more tree
  entries, each of which contains a SHA-1 pointer to a blob or subtree with its
  associated mode, type, and filename. For example, the most recent tree in the
  simplegit project may look something like this:
</p>

<pre>
$ git cat-file -p master^{tree}
100644 blob a906cb2a4a904a152e80877d4088654daad0c859      README
100644 blob 8f94139338f9404f26296befa88755fc2598c289      Rakefile
040000 tree 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0      lib
</pre>

<p>
  The <code>master^{tree}</code> syntax specifies the tree object that is pointed to by the last commit on
  your <code>master</code> branch. Notice that the <code>lib</code> subdirectory isn’t a blob but a pointer to another tree:
</p>

<pre>
$ git cat-file -p 99f1a6d12cb4b6f19c8655fca46c3ecf317074e0
100644 blob 47c6340d6459e05787f644c2447d2595f5d3a54b      simplegit.rb
</pre>

<p>Conceptually, the data that Git is storing is something like Figure 9-1.</p>

<p>
  Insert 18333fig0901.png Figure 9-1. Simple version of the Git data model.
</p>

<p>
  You can create your own tree. Git normally creates a tree by taking the state
  of your staging area or index and writing a tree object from it. So, to
  create a tree object, you first have to set up an index by staging some
  files. To create an index with a single entry — the first version of your
  test.txt file — you can use the plumbing command <code>update-index</code>. You use this command to artificially add the earlier version of the
  test.txt file to a new staging area. You must pass it the <code>--add</code> option because the file doesn’t yet exist in your staging area (you don’t
  even have a staging area set up yet) and <code>--cacheinfo</code> because the file you’re adding isn’t in your directory but is in your
  database. Then, you specify the mode, SHA-1, and filename:
</p>

<pre>
$ git update-index --add --cacheinfo 100644 \
  83baae61804e65cc73a7201a7252750c76066a30 test.txt
</pre>

<p>
  In this case, you’re specifying a mode of <code>100644</code>, which means it’s a normal file. Other options are <code>100755</code>, which means it’s an executable file; and <code>120000</code>, which specifies a symbolic link. The mode is taken from normal UNIX modes
  but is much less flexible — these three modes are the only ones that are
  valid for files (blobs) in Git (although other modes are used for directories
  and submodules).
</p>

<p>
  Now, you can use the <code>write-tree</code> command to write the staging area out to a tree object. No <code>-w</code> option is needed — calling <code>write-tree</code> automatically creates a tree object from the state of the index if that tree
  doesn’t yet exist:
</p>

<pre>
$ git write-tree
d8329fc1cc938780ffdd9f94e0d364e0ea74f579
$ git cat-file -p d8329fc1cc938780ffdd9f94e0d364e0ea74f579
100644 blob 83baae61804e65cc73a7201a7252750c76066a30      test.txt
</pre>

<p>You can also verify that this is a tree object:</p>

<pre>
$ git cat-file -t d8329fc1cc938780ffdd9f94e0d364e0ea74f579
tree
</pre>

<p>
  You’ll now create a new tree with the second version of test.txt and a new
  file as well:
</p>

<pre>
$ echo 'new file' &gt; new.txt
$ git update-index test.txt
$ git update-index --add new.txt
</pre>

<p>
  Your staging area now has the new version of test.txt as well as the new file
  new.txt. Write out that tree (recording the state of the staging area or
  index to a tree object) and see what it looks like:
</p>

<pre>
$ git write-tree
0155eb4229851634a0f03eb265b69f5a2d56f341
$ git cat-file -p 0155eb4229851634a0f03eb265b69f5a2d56f341
100644 blob fa49b077972391ad58037050f2a75f74e3671e92      new.txt
100644 blob 1f7a7a472abf3dd9643fd615f6da379c4acb3e3a      test.txt
</pre>

<p>
  Notice that this tree has both file entries and also that the test.txt SHA is
  the "version 2" SHA from earlier (<code>1f7a7a</code>). Just for fun, you’ll add the first tree as a subdirectory into this one.
  You can read trees into your staging area by calling <code>read-tree</code>. In this case, you can read an existing tree into your staging area as a
  subtree by using the <code>--prefix</code> option to <code>read-tree</code>:
</p>

<pre>
$ git read-tree --prefix=bak d8329fc1cc938780ffdd9f94e0d364e0ea74f579
$ git write-tree
3c4e9cd789d88d8d89c1073707c3585e41b0e614
$ git cat-file -p 3c4e9cd789d88d8d89c1073707c3585e41b0e614
040000 tree d8329fc1cc938780ffdd9f94e0d364e0ea74f579      bak
100644 blob fa49b077972391ad58037050f2a75f74e3671e92      new.txt
100644 blob 1f7a7a472abf3dd9643fd615f6da379c4acb3e3a      test.txt
</pre>

<p>
  If you created a working directory from the new tree you just wrote, you
  would get the two files in the top level of the working directory and a
  subdirectory named <code>bak</code> that contained the first version of the test.txt file. You can think of the
  data that Git contains for these structures as being like Figure 9-2.
</p>

<p>
  Insert 18333fig0902.png Figure 9-2. The content structure of your current Git
  data.
</p>

<h3>Commit Objects</h3>

<p>
  You have three trees that specify the different snapshots of your project
  that you want to track, but the earlier problem remains: you must remember
  all three SHA-1 values in order to recall the snapshots. You also don’t have
  any information about who saved the snapshots, when they were saved, or why
  they were saved. This is the basic information that the commit object stores
  for you.
</p>

<p>
  To create a commit object, you call <code>commit-tree</code> and specify a single tree SHA-1 and which commit objects, if any, directly
  preceded it. Start with the first tree you wrote:
</p>

<pre>
$ echo 'first commit' | git commit-tree d8329f
fdf4fc3344e67ab068f836878b6c4951e3b15f3d
</pre>

<p>Now you can look at your new commit object with <code>cat-file</code>:</p>

<pre>
$ git cat-file -p fdf4fc3
tree d8329fc1cc938780ffdd9f94e0d364e0ea74f579
author Scott Chacon &lt;schacon@gmail.com&gt; 1243040974 -0700
committer Scott Chacon &lt;schacon@gmail.com&gt; 1243040974 -0700

first commit
</pre>

<p>
  The format for a commit object is simple: it specifies the top-level tree for
  the snapshot of the project at that point; the author/committer information
  pulled from your <code>user.name</code> and <code>user.email</code> configuration settings, with the current timestamp; a blank line, and then
  the commit message.
</p>

<p>
  Next, you’ll write the other two commit objects, each referencing the commit
  that came directly before it:
</p>

<pre>
$ echo 'second commit' | git commit-tree 0155eb -p fdf4fc3
cac0cab538b970a37ea1e769cbbde608743bc96d
$ echo 'third commit'  | git commit-tree 3c4e9c -p cac0cab
1a410efbd13591db07496601ebc7a059dd55cfe9
</pre>

<p>
  Each of the three commit objects points to one of the three snapshot trees
  you created. Oddly enough, you have a real Git history now that you can view
  with the <code>git log</code> command, if you run it on the last commit SHA-1:
</p>

<pre>
$ git log --stat 1a410e
commit 1a410efbd13591db07496601ebc7a059dd55cfe9
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri May 22 18:15:24 2009 -0700

    third commit

 bak/test.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

commit cac0cab538b970a37ea1e769cbbde608743bc96d
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri May 22 18:14:29 2009 -0700

    second commit

 new.txt  |    1 +
 test.txt |    2 +-
 2 files changed, 2 insertions(+), 1 deletions(-)

commit fdf4fc3344e67ab068f836878b6c4951e3b15f3d
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri May 22 18:09:34 2009 -0700

    first commit

 test.txt |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
</pre>

<p>
  Amazing. You’ve just done the low-level operations to build up a Git history
  without using any of the front ends. This is essentially what Git does when
  you run the <code>git add</code> and <code>git commit</code> commands — it stores blobs for the files that have changed, updates the
  index, writes out trees, and writes commit objects that reference the
  top-level trees and the commits that came immediately before them. These
  three main Git objects — the blob, the tree, and the commit — are initially
  stored as separate files in your <code>.git/objects</code> directory. Here are all the objects in the example directory now, commented
  with what they store:
</p>

<pre>
$ find .git/objects -type f
.git/objects/01/55eb4229851634a0f03eb265b69f5a2d56f341 # tree 2
.git/objects/1a/410efbd13591db07496601ebc7a059dd55cfe9 # commit 3
.git/objects/1f/7a7a472abf3dd9643fd615f6da379c4acb3e3a # test.txt v2
.git/objects/3c/4e9cd789d88d8d89c1073707c3585e41b0e614 # tree 3
.git/objects/83/baae61804e65cc73a7201a7252750c76066a30 # test.txt v1
.git/objects/ca/c0cab538b970a37ea1e769cbbde608743bc96d # commit 2
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4 # 'test content'
.git/objects/d8/329fc1cc938780ffdd9f94e0d364e0ea74f579 # tree 1
.git/objects/fa/49b077972391ad58037050f2a75f74e3671e92 # new.txt
.git/objects/fd/f4fc3344e67ab068f836878b6c4951e3b15f3d # commit 1
</pre>

<p>
  If you follow all the internal pointers, you get an object graph something
  like Figure 9-3.
</p>

<p>
  Insert 18333fig0903.png Figure 9-3. All the objects in your Git directory.
</p>

<h3>Object Storage</h3>

<p>
  I mentioned earlier that a header is stored with the content. Let’s take a
  minute to look at how Git stores its objects. You’ll see how to store a blob
  object — in this case, the string "what is up, doc?" — interactively in the
  Ruby scripting language. You can start up interactive Ruby mode with the <code>irb</code> command:
</p>

<pre>
$ irb
&gt;&gt; content = "what is up, doc?"
=&gt; "what is up, doc?"
</pre>

<p>
  Git constructs a header that starts with the type of the object, in this case
  a blob. Then, it adds a space followed by the size of the content and finally
  a null byte:
</p>

<pre>
&gt;&gt; header = "blob #{content.length}\0"
=&gt; "blob 16\000"
</pre>

<p>
  Git concatenates the header and the original content and then calculates the
  SHA-1 checksum of that new content. You can calculate the SHA-1 value of a
  string in Ruby by including the SHA1 digest library with the <code>require</code> command and then calling <code>Digest::SHA1.hexdigest()</code> with the string:
</p>

<pre>
&gt;&gt; store = header + content
=&gt; "blob 16\000what is up, doc?"
&gt;&gt; require 'digest/sha1'
=&gt; true
&gt;&gt; sha1 = Digest::SHA1.hexdigest(store)
=&gt; "bd9dbf5aae1a3862dd1526723246b20206e5fc37"
</pre>

<p>
  Git compresses the new content with zlib, which you can do in Ruby with the
  zlib library. First, you need to require the library and then run <code>Zlib::Deflate.deflate()</code> on the content:
</p>

<pre>
&gt;&gt; require 'zlib'
=&gt; true
&gt;&gt; zlib_content = Zlib::Deflate.deflate(store)
=&gt; "x\234K\312\311OR04c(\317H,Q\310,V(-\320QH\311O\266\a\000_\034\a\235"
</pre>

<p>
  Finally, you’ll write your zlib-deflated content to an object on disk. You’ll
  determine the path of the object you want to write out (the first two
  characters of the SHA-1 value being the subdirectory name, and the last 38
  characters being the filename within that directory). In Ruby, you can use
  the <code>FileUtils.mkdir_p()</code> function to create the subdirectory if it doesn’t exist. Then, open the file
  with <code>File.open()</code> and write out the previously zlib-compressed content to the file with a <code>write()</code> call on the resulting file handle:
</p>

<pre>
&gt;&gt; path = '.git/objects/' + sha1[0,2] + '/' + sha1[2,38]
=&gt; ".git/objects/bd/9dbf5aae1a3862dd1526723246b20206e5fc37"
&gt;&gt; require 'fileutils'
=&gt; true
&gt;&gt; FileUtils.mkdir_p(File.dirname(path))
=&gt; ".git/objects/bd"
&gt;&gt; File.open(path, 'w') { |f| f.write zlib_content }
=&gt; 32
</pre>

<p>
  That’s it — you’ve created a valid Git blob object. All Git objects are
  stored the same way, just with different types — instead of the string blob,
  the header will begin with commit or tree. Also, although the blob content
  can be nearly anything, the commit and tree content are very specifically
  formatted.
</p>

<h2>Git References</h2>

<p>
  You can run something like <code>git log 1a410e</code> to look through your whole history, but you still have to remember that <code>1a410e</code> is the last commit in order to walk that history to find all those objects.
  You need a file in which you can store the SHA-1 value under a simple name so
  you can use that pointer rather than the raw SHA-1 value.
</p>

<p>
  In Git, these are called "references" or "refs"; you can find the files that
  contain the SHA-1 values in the <code>.git/refs</code> directory. In the current project, this directory contains no files, but it
  does contain a simple structure:
</p>

<pre>
$ find .git/refs
.git/refs
.git/refs/heads
.git/refs/tags
$ find .git/refs -type f
$
</pre>

<p>
  To create a new reference that will help you remember where your latest
  commit is, you can technically do something as simple as this:
</p>

<pre>
$ echo "1a410efbd13591db07496601ebc7a059dd55cfe9" &gt; .git/refs/heads/master
</pre>

<p>
  Now, you can use the head reference you just created instead of the SHA-1
  value in your Git commands:
</p>

<pre>
$ git log --pretty=oneline  master
1a410efbd13591db07496601ebc7a059dd55cfe9 third commit
cac0cab538b970a37ea1e769cbbde608743bc96d second commit
fdf4fc3344e67ab068f836878b6c4951e3b15f3d first commit
</pre>

<p>
  You aren’t encouraged to directly edit the reference files. Git provides a
  safer command to do this if you want to update a reference called <code>update-ref</code>:
</p>

<pre>
$ git update-ref refs/heads/master 1a410efbd13591db07496601ebc7a059dd55cfe9
</pre>

<p>
  That’s basically what a branch in Git is: a simple pointer or reference to
  the head of a line of work. To create a branch back at the second commit, you
  can do this:
</p>

<pre>
$ git update-ref refs/heads/test cac0ca
</pre>

<p>Your branch will contain only work from that commit down:</p>

<pre>
$ git log --pretty=oneline test
cac0cab538b970a37ea1e769cbbde608743bc96d second commit
fdf4fc3344e67ab068f836878b6c4951e3b15f3d first commit
</pre>

<p>Now, your Git database conceptually looks something like Figure 9-4.</p>

<p>
  Insert 18333fig0904.png Figure 9-4. Git directory objects with branch head
  references included.
</p>

<p>
  When you run commands like <code>git branch (branchname)</code>, Git basically runs that <code>update-ref</code> command to add the SHA-1 of the last commit of the branch you’re on into
  whatever new reference you want to create.
</p>

<p>
  If a reference is not found under the <code>refs</code> directory, Git then searches for it in the <code>.git/packed-refs</code> file, which contains space separated SHA-1 path pairs, for example:
</p>

<pre>
52d771167707552d8e2a50f602c669e2ad135722 refs/tags/v1.0.1
</pre>

<p>
  This file exists for performance reasons, as it is more efficient to have a
  single file rather than many small ones for references that don't change very
  often. You can tell Git to pack references into that file by using the <code>git pack-refs</code> command.
</p>

<h3>The HEAD</h3>

<p>
  The question now is, when you run <code>git branch (branchname)</code>, how does Git know the SHA-1 of the last commit? The answer is the HEAD
  file. The HEAD file is a symbolic reference to the branch you’re currently
  on. By symbolic reference, I mean that unlike a normal reference, it doesn’t
  generally contain a SHA-1 value but rather a pointer to another reference. If
  you look at the file, you’ll normally see something like this:
</p>

<pre>
$ cat .git/HEAD
ref: refs/heads/master
</pre>

<p>
  If you run <code>git checkout test</code>, Git updates the file to look like this:
</p>

<pre>
$ cat .git/HEAD
ref: refs/heads/test
</pre>

<p>
  When you run <code>git commit</code>, it creates the commit object, specifying the parent of that commit object
  to be whatever SHA-1 value the reference in HEAD points to.
</p>

<p>
  You can also manually edit this file, but again a safer command exists to do
  so: <code>symbolic-ref</code>. You can read the value of your HEAD via this command:
</p>

<pre>
$ git symbolic-ref HEAD
refs/heads/master
</pre>

<p>You can also set the value of HEAD:</p>

<pre>
$ git symbolic-ref HEAD refs/heads/test
$ cat .git/HEAD
ref: refs/heads/test
</pre>

<p>You can’t set a symbolic reference outside of the refs style:</p>

<pre>
$ git symbolic-ref HEAD test
fatal: Refusing to point HEAD outside of refs/
</pre>

<h3>Tags</h3>

<p>
  You’ve just gone over Git’s three main object types, but there is a fourth.
  The tag object is very much like a commit object — it contains a tagger, a
  date, a message, and a pointer. The main difference is that a tag object
  points to a commit rather than a tree. It’s like a branch reference, but it
  never moves — it always points to the same commit but gives it a friendlier
  name.
</p>

<p>
  As discussed in Chapter 2, there are two types of tags: annotated and
  lightweight. You can make a lightweight tag by running something like this:
</p>

<pre>
$ git update-ref refs/tags/v1.0 cac0cab538b970a37ea1e769cbbde608743bc96d
</pre>

<p>
  That is all a lightweight tag is — a branch that never moves. An annotated
  tag is more complex, however. If you create an annotated tag, Git creates a
  tag object and then writes a reference to point to it rather than directly to
  the commit. You can see this by creating an annotated tag (<code>-a</code> specifies that it’s an annotated tag):
</p>

<pre>
$ git tag -a v1.1 1a410efbd13591db07496601ebc7a059dd55cfe9 -m 'test tag'
</pre>

<p>Here’s the object SHA-1 value it created:</p>

<pre>
$ cat .git/refs/tags/v1.1
9585191f37f7b0fb9444f35a9bf50de191beadc2
</pre>

<p>Now, run the <code>cat-file</code> command on that SHA-1 value:</p>

<pre>
$ git cat-file -p 9585191f37f7b0fb9444f35a9bf50de191beadc2
object 1a410efbd13591db07496601ebc7a059dd55cfe9
type commit
tag v1.1
tagger Scott Chacon &lt;schacon@gmail.com&gt; Sat May 23 16:48:58 2009 -0700

test tag
</pre>

<p>
  Notice that the object entry points to the commit SHA-1 value that you
  tagged. Also notice that it doesn’t need to point to a commit; you can tag
  any Git object. In the Git source code, for example, the maintainer has added
  their GPG public key as a blob object and then tagged it. You can view the
  public key by running
</p>

<pre>
$ git cat-file blob junio-gpg-pub
</pre>

<p>
  in the Git source code repository. The Linux kernel repository also has a
  non-commit-pointing tag object — the first tag created points to the initial
  tree of the import of the source code.
</p>

<h3>Remotes</h3>

<p>
  The third type of reference that you’ll see is a remote reference. If you add
  a remote and push to it, Git stores the value you last pushed to that remote
  for each branch in the <code>refs/remotes</code> directory. For instance, you can add a remote called <code>origin</code> and push your <code>master</code> branch to it:
</p>

<pre>
$ git remote add origin git@github.com:schacon/simplegit-progit.git
$ git push origin master
Counting objects: 11, done.
Compressing objects: 100% (5/5), done.
Writing objects: 100% (7/7), 716 bytes, done.
Total 7 (delta 2), reused 4 (delta 1)
To git@github.com:schacon/simplegit-progit.git
   a11bef0..ca82a6d  master -&gt; master
</pre>

<p>
  Then, you can see what the <code>master</code> branch on the <code>origin</code> remote was the last time you communicated with the server, by checking the <code>refs/remotes/origin/master</code> file:
</p>

<pre>
$ cat .git/refs/remotes/origin/master
ca82a6dff817ec66f44342007202690a93763949
</pre>

<p>
  Remote references differ from branches (<code>refs/heads</code> references) mainly in that they can’t be checked out. Git moves them around
  as bookmarks to the last known state of where those branches were on those
  servers.
</p>

<h2>Packfiles</h2>

<p>
  Let’s go back to the objects database for your test Git repository. At this
  point, you have 11 objects — 4 blobs, 3 trees, 3 commits, and 1 tag:
</p>

<pre>
$ find .git/objects -type f
.git/objects/01/55eb4229851634a0f03eb265b69f5a2d56f341 # tree 2
.git/objects/1a/410efbd13591db07496601ebc7a059dd55cfe9 # commit 3
.git/objects/1f/7a7a472abf3dd9643fd615f6da379c4acb3e3a # test.txt v2
.git/objects/3c/4e9cd789d88d8d89c1073707c3585e41b0e614 # tree 3
.git/objects/83/baae61804e65cc73a7201a7252750c76066a30 # test.txt v1
.git/objects/95/85191f37f7b0fb9444f35a9bf50de191beadc2 # tag
.git/objects/ca/c0cab538b970a37ea1e769cbbde608743bc96d # commit 2
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4 # 'test content'
.git/objects/d8/329fc1cc938780ffdd9f94e0d364e0ea74f579 # tree 1
.git/objects/fa/49b077972391ad58037050f2a75f74e3671e92 # new.txt
.git/objects/fd/f4fc3344e67ab068f836878b6c4951e3b15f3d # commit 1
</pre>

<p>
  Git compresses the contents of these files with zlib, and you’re not storing
  much, so all these files collectively take up only 925 bytes. You’ll add some
  larger content to the repository to demonstrate an interesting feature of
  Git. Add the repo.rb file from the Grit library you worked with earlier —
  this is about a 12K source code file:
</p>

<pre>
$ curl -L https://raw.github.com/mojombo/grit/master/lib/grit/repo.rb &gt; repo.rb
$ git add repo.rb
$ git commit -m 'added repo.rb'
[master 484a592] added repo.rb
 3 files changed, 459 insertions(+), 2 deletions(-)
 delete mode 100644 bak/test.txt
 create mode 100644 repo.rb
 rewrite test.txt (100%)
</pre>

<p>
  If you look at the resulting tree, you can see the SHA-1 value your repo.rb
  file got for the blob object:
</p>

<pre>
$ git cat-file -p master^{tree}
100644 blob fa49b077972391ad58037050f2a75f74e3671e92      new.txt
100644 blob 9bc1dc421dcd51b4ac296e3e5b6e2a99cf44391e      repo.rb
100644 blob e3f094f522629ae358806b17daf78246c27c007b      test.txt
</pre>

<p>You can then check how big is that object on your disk:</p>

<pre>
$ du -b .git/objects/9b/c1dc421dcd51b4ac296e3e5b6e2a99cf44391e
4102	.git/objects/9b/c1dc421dcd51b4ac296e3e5b6e2a99cf44391e
</pre>

<p>Now, modify that file a little, and see what happens:</p>

<pre>
$ echo '# testing' &gt;&gt; repo.rb
$ git commit -am 'modified repo a bit'
[master ab1afef] modified repo a bit
 1 files changed, 1 insertions(+), 0 deletions(-)
</pre>

<p>
  Check the tree created by that commit, and you see something interesting:
</p>

<pre>
$ git cat-file -p master^{tree}
100644 blob fa49b077972391ad58037050f2a75f74e3671e92      new.txt
100644 blob 05408d195263d853f09dca71d55116663690c27c      repo.rb
100644 blob e3f094f522629ae358806b17daf78246c27c007b      test.txt
</pre>

<p>
  The blob is now a different blob, which means that although you added only a
  single line to the end of a 400-line file, Git stored that new content as a
  completely new object:
</p>

<pre>
$ du -b .git/objects/05/408d195263d853f09dca71d55116663690c27c
4109	.git/objects/05/408d195263d853f09dca71d55116663690c27c
</pre>

<p>
  You have two nearly identical 4K objects on your disk. Wouldn’t it be nice if
  Git could store one of them in full but then the second object only as the
  delta between it and the first?
</p>

<p>
  It turns out that it can. The initial format in which Git saves objects on
  disk is called a loose object format. However, occasionally Git packs up
  several of these objects into a single binary file called a packfile in order
  to save space and be more efficient. Git does this if you have too many loose
  objects around, if you run the <code>git gc</code> command manually, or if you push to a remote server. To see what happens,
  you can manually ask Git to pack up the objects by calling the <code>git gc</code> command:
</p>

<pre>
$ git gc
Counting objects: 17, done.
Delta compression using 2 threads.
Compressing objects: 100% (13/13), done.
Writing objects: 100% (17/17), done.
Total 17 (delta 1), reused 10 (delta 0)
</pre>

<p>
  If you look in your objects directory, you’ll find that most of your objects
  are gone, and a new pair of files has appeared:
</p>

<pre>
$ find .git/objects -type f
.git/objects/71/08f7ecb345ee9d0084193f147cdad4d2998293
.git/objects/d6/70460b4b4aece5915caf5c68d12f560a9fe3e4
.git/objects/info/packs
.git/objects/pack/pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.idx
.git/objects/pack/pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.pack
</pre>

<p>
  The objects that remain are the blobs that aren’t pointed to by any commit —
  in this case, the "what is up, doc?" example and the "test content" example
  blobs you created earlier. Because you never added them to any commits,
  they’re considered dangling and aren’t packed up in your new packfile.
</p>

<p>
  The other files are your new packfile and an index. The packfile is a single
  file containing the contents of all the objects that were removed from your
  filesystem. The index is a file that contains offsets into that packfile so
  you can quickly seek to a specific object. What is cool is that although the
  objects on disk before you ran the <code>gc</code> were collectively about 8K in size, the new packfile is only 4K. You’ve
  halved your disk usage by packing your objects.
</p>

<p>
  How does Git do this? When Git packs objects, it looks for files that are
  named and sized similarly, and stores just the deltas from one version of the
  file to the next. You can look into the packfile and see what Git did to save
  space. The <code>git verify-pack</code> plumbing command allows you to see what was packed up:
</p>

<pre>
$ git verify-pack -v \
  .git/objects/pack/pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.idx
0155eb4229851634a0f03eb265b69f5a2d56f341 tree   71 76 5400
05408d195263d853f09dca71d55116663690c27c blob   12908 3478 874
09f01cea547666f58d6a8d809583841a7c6f0130 tree   106 107 5086
1a410efbd13591db07496601ebc7a059dd55cfe9 commit 225 151 322
1f7a7a472abf3dd9643fd615f6da379c4acb3e3a blob   10 19 5381
3c4e9cd789d88d8d89c1073707c3585e41b0e614 tree   101 105 5211
484a59275031909e19aadb7c92262719cfcdf19a commit 226 153 169
83baae61804e65cc73a7201a7252750c76066a30 blob   10 19 5362
9585191f37f7b0fb9444f35a9bf50de191beadc2 tag    136 127 5476
9bc1dc421dcd51b4ac296e3e5b6e2a99cf44391e blob   7 18 5193 1 \
  05408d195263d853f09dca71d55116663690c27c
ab1afef80fac8e34258ff41fc1b867c702daa24b commit 232 157 12
cac0cab538b970a37ea1e769cbbde608743bc96d commit 226 154 473
d8329fc1cc938780ffdd9f94e0d364e0ea74f579 tree   36 46 5316
e3f094f522629ae358806b17daf78246c27c007b blob   1486 734 4352
f8f51d7d8a1760462eca26eebafde32087499533 tree   106 107 749
fa49b077972391ad58037050f2a75f74e3671e92 blob   9 18 856
fdf4fc3344e67ab068f836878b6c4951e3b15f3d commit 177 122 627
chain length = 1: 1 object
pack-7a16e4488ae40c7d2bc56ea2bd43e25212a66c45.pack: ok
</pre>

<p>
  Here, the <code>9bc1d</code> blob, which if you remember was the first version of your repo.rb file, is
  referencing the <code>05408</code> blob, which was the second version of the file. The third column in the
  output is the size of the object’s content, so you can see that the content
  of <code>05408</code> takes up 12K, but that of <code>9bc1d</code> only takes up 7 bytes. What is also interesting is that the second version
  of the file is the one that is stored intact, whereas the original version is
  stored as a delta — this is because you’re most likely to need faster access
  to the most recent version of the file.
</p>

<p>
  The really nice thing about this is that it can be repacked at any time. Git
  will occasionally repack your database automatically, always trying to save
  more space. You can also manually repack at any time by running <code>git gc</code> by hand.
</p>

<h2>The Refspec</h2>

<p>
  Throughout this book, you’ve used simple mappings from remote branches to
  local references; but they can be more complex. Suppose you add a remote like
  this:
</p>

<pre>
$ git remote add origin git@github.com:schacon/simplegit-progit.git
</pre>

<p>
  It adds a section to your <code>.git/config</code> file, specifying the name of the remote (<code>origin</code>), the URL of the remote repository, and the refspec for fetching:
</p>

<pre>
[remote "origin"]
       url = git@github.com:schacon/simplegit-progit.git
       fetch = +refs/heads/*:refs/remotes/origin/*
</pre>

<p>
  The format of the refspec is an optional <code>+</code>, followed by <code>&lt;src>:&lt;dst></code>, where <code>&lt;src></code> is the pattern for references on the remote side and <code>&lt;dst></code> is where those references will be written locally. The <code>+</code> tells Git to update the reference even if it isn’t a fast-forward.
</p>

<p>
  In the default case that is automatically written by a <code>git remote add</code> command, Git fetches all the references under <code>refs/heads/</code> on the server and writes them to <code>refs/remotes/origin/</code> locally. So, if there is a <code>master</code> branch on the server, you can access the log of that branch locally via
</p>

<pre>
$ git log origin/master
$ git log remotes/origin/master
$ git log refs/remotes/origin/master
</pre>

<p>
  They’re all equivalent, because Git expands each of them to <code>refs/remotes/origin/master</code>.
</p>

<p>
  If you want Git instead to pull down only the <code>master</code> branch each time, and not every other branch on the remote server, you can
  change the fetch line to
</p>

<pre>
fetch = +refs/heads/master:refs/remotes/origin/master
</pre>

<p>
  This is just the default refspec for <code>git fetch</code> for that remote. If you want to do something one time, you can specify the
  refspec on the command line, too. To pull the <code>master</code> branch on the remote down to <code>origin/mymaster</code> locally, you can run
</p>

<pre>
$ git fetch origin master:refs/remotes/origin/mymaster
</pre>

<p>
  You can also specify multiple refspecs. On the command line, you can pull
  down several branches like so:
</p>

<pre>
$ git fetch origin master:refs/remotes/origin/mymaster \
   topic:refs/remotes/origin/topic
From git@github.com:schacon/simplegit
 ! [rejected]        master     -&gt; origin/mymaster  (non fast forward)
 * [new branch]      topic      -&gt; origin/topic
</pre>

<p>
  In this case, the&emsp14;&emsp14;master branch pull was rejected because it
  wasn’t a fast-forward reference. You can override that by specifying the <code>+</code> in front of the refspec.
</p>

<p>
  You can also specify multiple refspecs for fetching in your configuration
  file. If you want to always fetch the master and experiment branches, add two
  lines:
</p>

<pre>
[remote "origin"]
       url = git@github.com:schacon/simplegit-progit.git
       fetch = +refs/heads/master:refs/remotes/origin/master
       fetch = +refs/heads/experiment:refs/remotes/origin/experiment
</pre>

<p>You can’t use partial globs in the pattern, so this would be invalid:</p>

<pre>
fetch = +refs/heads/qa*:refs/remotes/origin/qa*
</pre>

<p>
  However, you can use namespacing to accomplish something like that. If you
  have a QA team that pushes a series of branches, and you want to get the
  master branch and any of the QA team’s branches but nothing else, you can use
  a config section like this:
</p>

<pre>
[remote "origin"]
       url = git@github.com:schacon/simplegit-progit.git
       fetch = +refs/heads/master:refs/remotes/origin/master
       fetch = +refs/heads/qa/*:refs/remotes/origin/qa/*
</pre>

<p>
  If you have a complex workflow process that has a QA team pushing branches,
  developers pushing branches, and integration teams pushing and collaborating
  on remote branches, you can namespace them easily this way.
</p>

<h3>Pushing Refspecs</h3>

<p>
  It’s nice that you can fetch namespaced references that way, but how does the
  QA team get their branches into a <code>qa/</code> namespace in the first place? You accomplish that by using refspecs to push.
</p>

<p>
  If the QA team wants to push their <code>master</code> branch to <code>qa/master</code> on the remote server, they can run
</p>

<pre>
$ git push origin master:refs/heads/qa/master
</pre>

<p>
  If they want Git to do that automatically each time they run <code>git push origin</code>, they can add a <code>push</code> value to their config file:
</p>

<pre>
[remote "origin"]
       url = git@github.com:schacon/simplegit-progit.git
       fetch = +refs/heads/*:refs/remotes/origin/*
       push = refs/heads/master:refs/heads/qa/master
</pre>

<p>
  Again, this will cause a <code>git push origin</code> to push the local <code>master</code> branch to the remote <code>qa/master</code> branch by default.
</p>

<h3>Deleting References</h3>

<p>
  You can also use the refspec to delete references from the remote server by
  running something like this:
</p>

<pre>
$ git push origin :topic
</pre>

<p>
  Because the refspec is <code>&lt;src>:&lt;dst></code>, by leaving off the <code>&lt;src></code> part, this basically says to make the topic branch on the remote nothing,
  which deletes it.
</p>

<h2>Transfer Protocols</h2>

<p>
  Git can transfer data between two repositories in two major ways: over HTTP
  and via the so-called smart protocols used in the <code>file://</code>, <code>ssh://</code>, and <code>git://</code> transports. This section will quickly cover how these two main protocols
  operate.
</p>

<h3>The Dumb Protocol</h3>

<p>
  Git transport over HTTP is often referred to as the dumb protocol because it
  requires no Git-specific code on the server side during the transport
  process. The fetch process is a series of GET requests, where the client can
  assume the layout of the Git repository on the server. Let’s follow the <code>http-fetch</code> process for the simplegit library:
</p>

<pre>
$ git clone http://github.com/schacon/simplegit-progit.git
</pre>

<p>
  The first thing this command does is pull down the <code>info/refs</code> file. This file is written by the <code>update-server-info</code> command, which is why you need to enable that as a <code>post-receive</code> hook in order for the HTTP transport to work properly:
</p>

<pre>
=&gt; GET info/refs
ca82a6dff817ec66f44342007202690a93763949     refs/heads/master
</pre>

<p>
  Now you have a list of the remote references and SHAs. Next, you look for
  what the HEAD reference is so you know what to check out when you’re
  finished:
</p>

<pre>
=&gt; GET HEAD
ref: refs/heads/master
</pre>

<p>
  You need to check out the <code>master</code> branch when you’ve completed the process. At this point, you’re ready to
  start the walking process. Because your starting point is the <code>ca82a6</code> commit object you saw in the <code>info/refs</code> file, you start by fetching that:
</p>

<pre>
=&gt; GET objects/ca/82a6dff817ec66f44342007202690a93763949
(179 bytes of binary data)
</pre>

<p>
  You get an object back — that object is in loose format on the server, and
  you fetched it over a static HTTP GET request. You can zlib-uncompress it,
  strip off the header, and look at the commit content:
</p>

<pre>
$ git cat-file -p ca82a6dff817ec66f44342007202690a93763949
tree cfda3bf379e4f8dba8717dee55aab78aef7f4daf
parent 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
author Scott Chacon &lt;schacon@gmail.com&gt; 1205815931 -0700
committer Scott Chacon &lt;schacon@gmail.com&gt; 1240030591 -0700

changed the version number
</pre>

<p>
  Next, you have two more objects to retrieve — <code>cfda3b</code>, which is the tree of content that the commit we just retrieved points to;
  and <code>085bb3</code>, which is the parent commit:
</p>

<pre>
=&gt; GET objects/08/5bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
(179 bytes of data)
</pre>

<p>That gives you your next commit object. Grab the tree object:</p>

<pre>
=&gt; GET objects/cf/da3bf379e4f8dba8717dee55aab78aef7f4daf
(404 - Not Found)
</pre>

<p>
  Oops — it looks like that tree object isn’t in loose format on the server, so
  you get a 404 response back. There are a couple of reasons for this — the
  object could be in an alternate repository, or it could be in a packfile in
  this repository. Git checks for any listed alternates first:
</p>

<pre>
=&gt; GET objects/info/http-alternates
(empty file)
</pre>

<p>
  If this comes back with a list of alternate URLs, Git checks for loose files
  and packfiles there — this is a nice mechanism for projects that are forks of
  one another to share objects on disk. However, because no alternates are
  listed in this case, your object must be in a packfile. To see what packfiles
  are available on this server, you need to get the <code>objects/info/packs</code> file, which contains a listing of them (also generated by <code>update-server-info</code>):
</p>

<pre>
=&gt; GET objects/info/packs
P pack-816a9b2334da9953e530f27bcac22082a9f5b835.pack
</pre>

<p>
  There is only one packfile on the server, so your object is obviously in
  there, but you’ll check the index file to make sure. This is also useful if
  you have multiple packfiles on the server, so you can see which packfile
  contains the object you need:
</p>

<pre>
=&gt; GET objects/pack/pack-816a9b2334da9953e530f27bcac22082a9f5b835.idx
(4k of binary data)
</pre>

<p>
  Now that you have the packfile index, you can see if your object is in it —
  because the index lists the SHAs of the objects contained in the packfile and
  the offsets to those objects. Your object is there, so go ahead and get the
  whole packfile:
</p>

<pre>
=&gt; GET objects/pack/pack-816a9b2334da9953e530f27bcac22082a9f5b835.pack
(13k of binary data)
</pre>

<p>
  You have your tree object, so you continue walking your commits. They’re all
  also within the packfile you just downloaded, so you don’t have to do any
  more requests to your server. Git checks out a working copy of the <code>master</code> branch that was pointed to by the HEAD reference you downloaded at the
  beginning.
</p>

<p>The entire output of this process looks like this:</p>

<pre>
$ git clone http://github.com/schacon/simplegit-progit.git
Initialized empty Git repository in /private/tmp/simplegit-progit/.git/
got ca82a6dff817ec66f44342007202690a93763949
walk ca82a6dff817ec66f44342007202690a93763949
got 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
Getting alternates list for http://github.com/schacon/simplegit-progit.git
Getting pack list for http://github.com/schacon/simplegit-progit.git
Getting index for pack 816a9b2334da9953e530f27bcac22082a9f5b835
Getting pack 816a9b2334da9953e530f27bcac22082a9f5b835
 which contains cfda3bf379e4f8dba8717dee55aab78aef7f4daf
walk 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
walk a11bef06a3f659402fe7563abf99ad00de2209e6
</pre>

<h3>The Smart Protocol</h3>

<p>
  The HTTP method is simple but a bit inefficient. Using smart protocols is a
  more common method of transferring data. These protocols have a process on
  the remote end that is intelligent about Git — it can read local data and
  figure out what the client has or needs and generate custom data for it.
  There are two sets of processes for transferring data: a pair for uploading
  data and a pair for downloading data.
</p>

<p>Uploading Data</p>

<p>
  To upload data to a remote process, Git uses the <code>send-pack</code> and <code>receive-pack</code> processes. The <code>send-pack</code> process runs on the client and connects to a <code>receive-pack</code> process on the remote side.
</p>

<p>
  For example, say you run <code>git push origin master</code> in your project, and <code>origin</code> is defined as a URL that uses the SSH protocol. Git fires up the <code>send-pack</code> process, which initiates a connection over SSH to your server. It tries to
  run a command on the remote server via an SSH call that looks something like
  this:
</p>

<pre>
$ ssh -x git@github.com "git-receive-pack 'schacon/simplegit-progit.git'"
005bca82a6dff817ec66f4437202690a93763949 refs/heads/master report-status delete-refs
003e085bb3bcb608e1e84b2432f8ecbe6306e7e7 refs/heads/topic
0000
</pre>

<p>
  The <code>git-receive-pack</code> command immediately responds with one line for each reference it currently
  has — in this case, just the <code>master</code> branch and its SHA. The first line also has a list of the server’s
  capabilities (here, <code>report-status</code> and <code>delete-refs</code>).
</p>

<p>
  Each line starts with a 4-byte hex value specifying how long the rest of the
  line is. Your first line starts with 005b, which is 91 in decimal, meaning
  that 91 bytes remain on that line. The next line starts with 003e, which is
  62, so you read the remaining 62 bytes. The next line is 0000, meaning the
  server is done with its references listing.
</p>

<p>
  Now that it knows the server’s state, your <code>send-pack</code> process determines what commits it has that the server doesn’t. For each
  reference that this push will update, the <code>send-pack</code> process tells the <code>receive-pack</code> process that information. For instance, if you’re updating the <code>master</code> branch and adding an <code>experiment</code> branch, the <code>send-pack</code> response may look something like this:
</p>

<pre>
0085ca82a6dff817ec66f44342007202690a93763949  15027957951b64cf874c3557a0f3547bd83b3ff6 refs/heads/master report-status
00670000000000000000000000000000000000000000 cdfdb42577e2506715f8cfeacdbabc092bf63e8d refs/heads/experiment
0000
</pre>

<p>
  The SHA-1 value of all '0's means that nothing was there before — because
  you’re adding the experiment reference. If you were deleting a reference, you
  would see the opposite: all '0's on the right side.
</p>

<p>
  Git sends a line for each reference you’re updating with the old SHA, the new
  SHA, and the reference that is being updated. The first line also has the
  client’s capabilities. Next, the client uploads a packfile of all the objects
  the server doesn’t have yet. Finally, the server responds with a success (or
  failure) indication:
</p>

<pre>
000Aunpack ok
</pre>

<p>Downloading Data</p>

<p>
  When you download data, the <code>fetch-pack</code> and <code>upload-pack</code> processes are involved. The client initiates a <code>fetch-pack</code> process that connects to an <code>upload-pack</code> process on the remote side to negotiate what data will be transferred down.
</p>

<p>
  There are different ways to initiate the <code>upload-pack</code> process on the remote repository. You can run via SSH in the same manner as
  the <code>receive-pack</code> process. You can also initiate the process via the Git daemon, which listens
  on a server on port 9418 by default. The <code>fetch-pack</code> process sends data that looks like this to the daemon after connecting:
</p>

<pre>
003fgit-upload-pack schacon/simplegit-progit.git\0host=myserver.com\0
</pre>

<p>
  It starts with the 4 bytes specifying how much data is following, then the
  command to run followed by a null byte, and then the server’s hostname
  followed by a final null byte. The Git daemon checks that the command can be
  run and that the repository exists and has public permissions. If everything
  is cool, it fires up the <code>upload-pack</code> process and hands off the request to it.
</p>

<p>
  If you’re doing the fetch over SSH, <code>fetch-pack</code> instead runs something like this:
</p>

<pre>
$ ssh -x git@github.com "git-upload-pack 'schacon/simplegit-progit.git'"
</pre>

<p>
  In either case, after <code>fetch-pack</code> connects, <code>upload-pack</code> sends back something like this:
</p>

<pre>
0088ca82a6dff817ec66f44342007202690a93763949 HEAD\0multi_ack thin-pack \
  side-band side-band-64k ofs-delta shallow no-progress include-tag
003fca82a6dff817ec66f44342007202690a93763949 refs/heads/master
003e085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7 refs/heads/topic
0000
</pre>

<p>
  This is very similar to what <code>receive-pack</code> responds with, but the capabilities are different. In addition, it sends
  back the HEAD reference so the client knows what to check out if this is a
  clone.
</p>

<p>
  At this point, the <code>fetch-pack</code> process looks at what objects it has and responds with the objects that it
  needs by sending "want" and then the SHA it wants. It sends all the objects
  it already has with "have" and then the SHA. At the end of this list, it
  writes "done" to initiate the <code>upload-pack</code> process to begin sending the packfile of the data it needs:
</p>

<pre>
0054want ca82a6dff817ec66f44342007202690a93763949 ofs-delta
0032have 085bb3bcb608e1e8451d4b2432f8ecbe6306e7e7
0000
0009done
</pre>

<p>
  That is a very basic case of the transfer protocols. In more complex cases,
  the client supports <code>multi_ack</code> or <code>side-band</code> capabilities; but this example shows you the basic back and forth used by
  the smart protocol processes.
</p>

<h2>Maintenance and Data Recovery</h2>

<p>
  Occasionally, you may have to do some cleanup — make a repository more
  compact, clean up an imported repository, or recover lost work. This section
  will cover some of these scenarios.
</p>

<h3>Maintenance</h3>

<p>
  Occasionally, Git automatically runs a command called "auto gc". Most of the
  time, this command does nothing. However, if there are too many loose objects
  (objects not in a packfile) or too many packfiles, Git launches a
  full-fledged <code>git gc</code> command. The <code>gc</code> stands for garbage collect, and the command does a number of things: it
  gathers up all the loose objects and places them in packfiles, it
  consolidates packfiles into one big packfile, and it removes objects that
  aren’t reachable from any commit and are a few months old.
</p>

<p>You can run auto gc manually as follows:</p>

<pre>
$ git gc --auto
</pre>

<p>
  Again, this generally does nothing. You must have around 7,000 loose objects
  or more than 50 packfiles for Git to fire up a real gc command. You can
  modify these limits with the <code>gc.auto</code> and <code>gc.autopacklimit</code> config settings, respectively.
</p>

<p>
  The other thing <code>gc</code> will do is pack up your references into a single file. Suppose your
  repository contains the following branches and tags:
</p>

<pre>
$ find .git/refs -type f
.git/refs/heads/experiment
.git/refs/heads/master
.git/refs/tags/v1.0
.git/refs/tags/v1.1
</pre>

<p>
  If you run <code>git gc</code>, you’ll no longer have these files in the <code>refs</code> directory. Git will move them for the sake of efficiency into a file named <code>.git/packed-refs</code> that looks like this:
</p>

<pre>
$ cat .git/packed-refs
# pack-refs with: peeled
cac0cab538b970a37ea1e769cbbde608743bc96d refs/heads/experiment
ab1afef80fac8e34258ff41fc1b867c702daa24b refs/heads/master
cac0cab538b970a37ea1e769cbbde608743bc96d refs/tags/v1.0
9585191f37f7b0fb9444f35a9bf50de191beadc2 refs/tags/v1.1
^1a410efbd13591db07496601ebc7a059dd55cfe9
</pre>

<p>
  If you update a reference, Git doesn’t edit this file but instead writes a
  new file to <code>refs/heads</code>. To get the appropriate SHA for a given reference, Git checks for that
  reference in the <code>refs</code> directory and then checks the <code>packed-refs</code> file as a fallback. However, if you can’t find a reference in the <code>refs</code> directory, it’s probably in your <code>packed-refs</code> file.
</p>

<p>
  Notice the last line of the file, which begins with a <code>^</code>. This means the tag directly above is an annotated tag and that line is the
  commit that the annotated tag points to.
</p>

<h3>Data Recovery</h3>

<p>
  At some point in your Git journey, you may accidentally lose a commit.
  Generally, this happens because you force-delete a branch that had work on
  it, and it turns out you wanted the branch after all; or you hard-reset a
  branch, thus abandoning commits that you wanted something from. Assuming this
  happens, how can you get your commits back?
</p>

<p>
  Here’s an example that hard-resets the master branch in your test repository
  to an older commit and then recovers the lost commits. First, let’s review
  where your repository is at this point:
</p>

<pre>
$ git log --pretty=oneline
ab1afef80fac8e34258ff41fc1b867c702daa24b modified repo a bit
484a59275031909e19aadb7c92262719cfcdf19a added repo.rb
1a410efbd13591db07496601ebc7a059dd55cfe9 third commit
cac0cab538b970a37ea1e769cbbde608743bc96d second commit
fdf4fc3344e67ab068f836878b6c4951e3b15f3d first commit
</pre>

<p>Now, move the <code>master</code> branch back to the middle commit:</p>

<pre>
$ git reset --hard 1a410efbd13591db07496601ebc7a059dd55cfe9
HEAD is now at 1a410ef third commit
$ git log --pretty=oneline
1a410efbd13591db07496601ebc7a059dd55cfe9 third commit
cac0cab538b970a37ea1e769cbbde608743bc96d second commit
fdf4fc3344e67ab068f836878b6c4951e3b15f3d first commit
</pre>

<p>
  You’ve effectively lost the top two commits — you have no branch from which
  those commits are reachable. You need to find the latest commit SHA and then
  add a branch that points to it. The trick is finding that latest commit SHA —
  it’s not like you’ve memorized it, right?
</p>

<p>
  Often, the quickest way is to use a tool called <code>git reflog</code>. As you’re working, Git silently records what your HEAD is every time you
  change it. Each time you commit or change branches, the reflog is updated.
  The reflog is also updated by the <code>git update-ref</code> command, which is another reason to use it instead of just writing the SHA
  value to your ref files, as we covered in the "Git References" section of
  this chapter earlier.&emsp14;&emsp14;You can see where you’ve been at any
  time by running <code>git reflog</code>:
</p>

<pre>
$ git reflog
1a410ef HEAD@{0}: 1a410efbd13591db07496601ebc7a059dd55cfe9: updating HEAD
ab1afef HEAD@{1}: ab1afef80fac8e34258ff41fc1b867c702daa24b: updating HEAD
</pre>

<p>
  Here we can see the two commits that we have had checked out, however there
  is not much information here.&emsp14;&emsp14;To see the same information in a
  much more useful way, we can run <code>git log -g</code>, which will give you a normal log output for your reflog.
</p>

<pre>
$ git log -g
commit 1a410efbd13591db07496601ebc7a059dd55cfe9
Reflog: HEAD@{0} (Scott Chacon &lt;schacon@gmail.com&gt;)
Reflog message: updating HEAD
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri May 22 18:22:37 2009 -0700

    third commit

commit ab1afef80fac8e34258ff41fc1b867c702daa24b
Reflog: HEAD@{1} (Scott Chacon &lt;schacon@gmail.com&gt;)
Reflog message: updating HEAD
Author: Scott Chacon &lt;schacon@gmail.com&gt;
Date:   Fri May 22 18:15:24 2009 -0700

     modified repo a bit
</pre>

<p>
  It looks like the bottom commit is the one you lost, so you can recover it by
  creating a new branch at that commit. For example, you can start a branch
  named <code>recover-branch</code> at that commit (ab1afef):
</p>

<pre>
$ git branch recover-branch ab1afef
$ git log --pretty=oneline recover-branch
ab1afef80fac8e34258ff41fc1b867c702daa24b modified repo a bit
484a59275031909e19aadb7c92262719cfcdf19a added repo.rb
1a410efbd13591db07496601ebc7a059dd55cfe9 third commit
cac0cab538b970a37ea1e769cbbde608743bc96d second commit
fdf4fc3344e67ab068f836878b6c4951e3b15f3d first commit
</pre>

<p>
  Cool — now you have a branch named <code>recover-branch</code> that is where your <code>master</code> branch used to be, making the first two commits reachable again. Next,
  suppose your loss was for some reason not in the reflog — you can simulate
  that by removing <code>recover-branch</code> and deleting the reflog. Now the first two commits aren’t reachable by
  anything:
</p>

<pre>
$ git branch -D recover-branch
$ rm -Rf .git/logs/
</pre>

<p>
  Because the reflog data is kept in the <code>.git/logs/</code> directory, you effectively have no reflog. How can you recover that commit
  at this point? One way is to use the <code>git fsck</code> utility, which checks your database for integrity. If you run it with the <code>--full</code> option, it shows you all objects that aren’t pointed to by another object:
</p>

<pre>
$ git fsck --full
dangling blob d670460b4b4aece5915caf5c68d12f560a9fe3e4
dangling commit ab1afef80fac8e34258ff41fc1b867c702daa24b
dangling tree aea790b9a58f6cf6f2804eeac9f0abbe9631e4c9
dangling blob 7108f7ecb345ee9d0084193f147cdad4d2998293
</pre>

<p>
  In this case, you can see your missing commit after the dangling commit. You
  can recover it the same way, by adding a branch that points to that SHA.
</p>

<h3>Removing Objects</h3>

<p>
  There are a lot of great things about Git, but one feature that can cause
  issues is the fact that a <code>git clone</code> downloads the entire history of the project, including every version of
  every file. This is fine if the whole thing is source code, because Git is
  highly optimized to compress that data efficiently. However, if someone at
  any point in the history of your project added a single huge file, every
  clone for all time will be forced to download that large file, even if it was
  removed from the project in the very next commit. Because it’s reachable from
  the history, it will always be there.
</p>

<p>
  This can be a huge problem when you’re converting Subversion or Perforce
  repositories into Git. Because you don’t download the whole history in those
  systems, this type of addition carries few consequences. If you did an import
  from another system or otherwise find that your repository is much larger
  than it should be, here is how you can find and remove large objects.
</p>

<p>
  Be warned: this technique is destructive to your commit history. It rewrites
  every commit object downstream from the earliest tree you have to modify to
  remove a large file reference. If you do this immediately after an import,
  before anyone has started to base work on the commit, you’re fine —
  otherwise, you have to notify all contributors that they must rebase their
  work onto your new commits.
</p>

<p>
  To demonstrate, you’ll add a large file into your test repository, remove it
  in the next commit, find it, and remove it permanently from the repository.
  First, add a large object to your history:
</p>

<pre>
$ curl http://kernel.org/pub/software/scm/git/git-1.6.3.1.tar.bz2 &gt; git.tbz2
$ git add git.tbz2
$ git commit -am 'added git tarball'
[master 6df7640] added git tarball
 1 files changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 git.tbz2
</pre>

<p>
  Oops — you didn’t want to add a huge tarball to your project. Better get rid
  of it:
</p>

<pre>
$ git rm git.tbz2
rm 'git.tbz2'
$ git commit -m 'oops - removed large tarball'
[master da3f30d] oops - removed large tarball
 1 files changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 git.tbz2
</pre>

<p>Now, <code>gc</code> your database and see how much space you’re using:</p>

<pre>
$ git gc
Counting objects: 21, done.
Delta compression using 2 threads.
Compressing objects: 100% (16/16), done.
Writing objects: 100% (21/21), done.
Total 21 (delta 3), reused 15 (delta 1)
</pre>

<p>
  You can run the <code>count-objects</code> command to quickly see how much space you’re using:
</p>

<pre>
$ git count-objects -v
count: 4
size: 16
in-pack: 21
packs: 1
size-pack: 2016
prune-packable: 0
garbage: 0
</pre>

<p>
  The <code>size-pack</code> entry is the size of your packfiles in kilobytes, so you’re using 2MB.
  Before the last commit, you were using closer to 2K — clearly, removing the
  file from the previous commit didn’t remove it from your history. Every time
  anyone clones this repository, they will have to clone all 2MB just to get
  this tiny project, because you accidentally added a big file. Let’s get rid
  of it.
</p>

<p>
  First you have to find it. In this case, you already know what file it is.
  But suppose you didn’t; how would you identify what file or files were taking
  up so much space? If you run <code>git gc</code>, all the objects are in a packfile; you can identify the big objects by
  running another plumbing command called <code>git verify-pack</code> and sorting on the third field in the output, which is file size. You can
  also pipe it through the <code>tail</code> command because you’re only interested in the last few largest files:
</p>

<pre>
$ git verify-pack -v .git/objects/pack/pack-3f8c0...bb.idx | sort -k 3 -n | tail -3
e3f094f522629ae358806b17daf78246c27c007b blob   1486 734 4667
05408d195263d853f09dca71d55116663690c27c blob   12908 3478 1189
7a9eb2fba2b1811321254ac360970fc169ba2330 blob   2056716 2056872 5401
</pre>

<p>
  The big object is at the bottom: 2MB. To find out what file it is, you’ll use
  the <code>rev-list</code> command, which you used briefly in Chapter 7. If you pass <code>--objects</code> to <code>rev-list</code>, it lists all the commit SHAs and also the blob SHAs with the file paths
  associated with them. You can use this to find your blob’s name:
</p>

<pre>
$ git rev-list --objects --all | grep 7a9eb2fb
7a9eb2fba2b1811321254ac360970fc169ba2330 git.tbz2
</pre>

<p>
  Now, you need to remove this file from all trees in your past. You can easily
  see what commits modified this file:
</p>

<pre>
$ git log --pretty=oneline --branches -- git.tbz2
da3f30d019005479c99eb4c3406225613985a1db oops - removed large tarball
6df764092f3e7c8f5f94cbe08ee5cf42e92a0289 added git tarball
</pre>

<p>
  You must rewrite all the commits downstream from <code>6df76</code> to fully remove this file from your Git history. To do so, you use <code>filter-branch</code>, which you used in Chapter 6:
</p>

<pre>
$ git filter-branch --index-filter \
   'git rm --cached --ignore-unmatch git.tbz2' -- 6df7640^..
Rewrite 6df764092f3e7c8f5f94cbe08ee5cf42e92a0289 (1/2)rm 'git.tbz2'
Rewrite da3f30d019005479c99eb4c3406225613985a1db (2/2)
Ref 'refs/heads/master' was rewritten
</pre>

<p>
  The <code>--index-filter</code> option is similar to the <code>--tree-filter</code> option used in Chapter 6, except that instead of passing a command that
  modifies files checked out on disk, you’re modifying your staging area or
  index each time. Rather than remove a specific file with something like <code>rm file</code>, you have to remove it with <code>git rm --cached</code> — you must remove it from the index, not from disk. The reason to do it this
  way is speed — because Git doesn’t have to check out each revision to disk
  before running your filter, the process can be much, much faster. You can
  accomplish the same task with <code>--tree-filter</code> if you want. The <code>--ignore-unmatch</code> option to <code>git rm</code> tells it not to error out if the pattern you’re trying to remove isn’t
  there. Finally, you ask <code>filter-branch</code> to rewrite your history only from the <code>6df7640</code> commit up, because you know that is where this problem started. Otherwise,
  it will start from the beginning and will unnecessarily take longer.
</p>

<p>
  Your history no longer contains a reference to that file. However, your
  reflog and a new set of refs that Git added when you did the <code>filter-branch</code> under <code>.git/refs/original</code> still do, so you have to remove them and then repack the database. You need
  to get rid of anything that has a pointer to those old commits before you
  repack:
</p>

<pre>
$ rm -Rf .git/refs/original
$ rm -Rf .git/logs/
$ git gc
Counting objects: 19, done.
Delta compression using 2 threads.
Compressing objects: 100% (14/14), done.
Writing objects: 100% (19/19), done.
Total 19 (delta 3), reused 16 (delta 1)
</pre>

<p>Let’s see how much space you saved.</p>

<pre>
$ git count-objects -v
count: 8
size: 2040
in-pack: 19
packs: 1
size-pack: 7
prune-packable: 0
garbage: 0
</pre>

<p>
  The packed repository size is down to 7K, which is much better than 2MB. You
  can see from the size value that the big object is still in your loose
  objects, so it’s not gone; but it won’t be transferred on a push or
  subsequent clone, which is what is important. If you really wanted to, you
  could remove the object completely by running <code>git prune --expire</code>.
</p>

<h2>Summary</h2>

<p>
  You should have a pretty good understanding of what Git does in the
  background and, to some degree, how it’s implemented. This chapter has
  covered a number of plumbing commands — commands that are lower level and
  simpler than the porcelain commands you’ve learned about in the rest of the
  book. Understanding how Git works at a lower level should make it easier to
  understand why it’s doing what it’s doing and also to write your own tools
  and helping scripts to make your specific workflow work for you.
</p>

<p>
  Git as a content-addressable filesystem is a very powerful tool that you can
  easily use as more than just a VCS. I hope you can use your newfound
  knowledge of Git internals to implement your own cool application of this
  technology and feel more comfortable using Git in more advanced ways.
</p>
