<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE article PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">

<article lang="en">
<articleinfo>
    <title>Git User&#8217;s Manual (for version 1.5.3 or newer)</title>
</articleinfo>
<simpara>Git is a fast distributed revision control system.</simpara>
<simpara>This manual is designed to be readable by someone with basic UNIX
command-line skills, but no previous knowledge of git.</simpara>
<simpara><xref linkend="repositories-and-branches"/> and <xref linkend="exploring-git-history"/> explain how
to fetch and study a project using git&#8212;read these chapters to learn how
to build and test a particular version of a software project, search for
regressions, and so on.</simpara>
<simpara>People needing to do actual development will also want to read
<xref linkend="Developing-With-git"/> and <xref linkend="sharing-development"/>.</simpara>
<simpara>Further chapters cover more specialized topics.</simpara>
<simpara>Comprehensive reference documentation is available through the man
pages, or <ulink url="git-help.html">git-help(1)</ulink> command.  For example, for the command
"git clone &lt;repo&gt;", you can either use:</simpara>
<literallayout>$ man git-clone</literallayout>
<simpara>or:</simpara>
<literallayout>$ git help clone</literallayout>
<simpara>With the latter, you can use the manual viewer of your choice; see
<ulink url="git-help.html">git-help(1)</ulink> for more information.</simpara>
<simpara>See also <xref linkend="git-quick-start"/> for a brief overview of git commands,
without any explanation.</simpara>
<simpara>Finally, see <xref linkend="todo"/> for ways that you can help make this manual more
complete.</simpara>
<section id="repositories-and-branches">
<title>Repositories and Branches</title>
<section id="how-to-get-a-git-repository">
<title>How to get a git repository</title>
<simpara>It will be useful to have a git repository to experiment with as you
read this manual.</simpara>
<simpara>The best way to get one is by using the <ulink url="git-clone.html">git-clone(1)</ulink> command to
download a copy of an existing repository.  If you don&#8217;t already have a
project in mind, here are some interesting examples:</simpara>
<literallayout>        # git itself (approx. 10MB download):
$ git clone git://git.kernel.org/pub/scm/git/git.git
        # the Linux kernel (approx. 150MB download):
$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git</literallayout>
<simpara>The initial clone may be time-consuming for a large project, but you
will only need to clone once.</simpara>
<simpara>The clone command creates a new directory named after the project ("git"
or "linux-2.6" in the examples above).  After you cd into this
directory, you will see that it contains a copy of the project files,
called the <link linkend="def_working_tree">working tree</link>, together with a special
top-level directory named ".git", which contains all the information
about the history of the project.</simpara>
</section>
<section id="how-to-check-out">
<title>How to check out a different version of a project</title>
<simpara>Git is best thought of as a tool for storing the history of a collection
of files.  It stores the history as a compressed collection of
interrelated snapshots of the project&#8217;s contents.  In git each such
version is called a <link linkend="def_commit">commit</link>.</simpara>
<simpara>Those snapshots aren&#8217;t necessarily all arranged in a single line from
oldest to newest; instead, work may simultaneously proceed along
parallel lines of development, called <link linkend="def_branch">branches</link>, which may
merge and diverge.</simpara>
<simpara>A single git repository can track development on multiple branches.  It
does this by keeping a list of <link linkend="def_head">heads</link> which reference the
latest commit on each branch; the <ulink url="git-branch.html">git-branch(1)</ulink> command shows
you the list of branch heads:</simpara>
<literallayout>$ git branch
* master</literallayout>
<simpara>A freshly cloned repository contains a single branch head, by default
named "master", with the working directory initialized to the state of
the project referred to by that branch head.</simpara>
<simpara>Most projects also use <link linkend="def_tag">tags</link>.  Tags, like heads, are
references into the project&#8217;s history, and can be listed using the
<ulink url="git-tag.html">git-tag(1)</ulink> command:</simpara>
<literallayout>$ git tag -l
v2.6.11
v2.6.11-tree
v2.6.12
v2.6.12-rc2
v2.6.12-rc3
v2.6.12-rc4
v2.6.12-rc5
v2.6.12-rc6
v2.6.13
...</literallayout>
<simpara>Tags are expected to always point at the same version of a project,
while heads are expected to advance as development progresses.</simpara>
<simpara>Create a new branch head pointing to one of these versions and check it
out using <ulink url="git-checkout.html">git-checkout(1)</ulink>:</simpara>
<literallayout>$ git checkout -b new v2.6.13</literallayout>
<simpara>The working directory then reflects the contents that the project had
when it was tagged v2.6.13, and <ulink url="git-branch.html">git-branch(1)</ulink> shows two
branches, with an asterisk marking the currently checked-out branch:</simpara>
<literallayout>$ git branch
  master
* new</literallayout>
<simpara>If you decide that you&#8217;d rather see version 2.6.17, you can modify
the current branch to point at v2.6.17 instead, with</simpara>
<literallayout>$ git reset --hard v2.6.17</literallayout>
<simpara>Note that if the current branch head was your only reference to a
particular point in history, then resetting that branch may leave you
with no way to find the history it used to point to; so use this command
carefully.</simpara>
</section>
<section id="understanding-commits">
<title>Understanding History: Commits</title>
<simpara>Every change in the history of a project is represented by a commit.
The <ulink url="git-show.html">git-show(1)</ulink> command shows the most recent commit on the
current branch:</simpara>
<literallayout>$ git show
commit 17cf781661e6d38f737f15f53ab552f1e95960d7
Author: Linus Torvalds &lt;torvalds@ppc970.osdl.org.(none)&gt;
Date:   Tue Apr 19 14:11:06 2005 -0700

    Remove duplicate getenv(DB_ENVIRONMENT) call

    Noted by Tony Luck.

diff --git a/init-db.c b/init-db.c
index 65898fa..b002dc6 100644
--- a/init-db.c
+++ b/init-db.c
@@ -7,7 +7,7 @@

 int main(int argc, char **argv)
 {
-       char *sha1_dir = getenv(DB_ENVIRONMENT), *path;
+       char *sha1_dir, *path;
        int len, i;

        if (mkdir(".git", 0755) &lt; 0) {</literallayout>
<simpara>As you can see, a commit shows who made the latest change, what they
did, and why.</simpara>
<simpara>Every commit has a 40-hexdigit id, sometimes called the "object name" or the
"SHA1 id", shown on the first line of the "git-show" output.  You can usually
refer to a commit by a shorter name, such as a tag or a branch name, but this
longer name can also be useful.  Most importantly, it is a globally unique
name for this commit: so if you tell somebody else the object name (for
example in email), then you are guaranteed that name will refer to the same
commit in their repository that it does in yours (assuming their repository
has that commit at all).  Since the object name is computed as a hash over the
contents of the commit, you are guaranteed that the commit can never change
without its name also changing.</simpara>
<simpara>In fact, in <xref linkend="git-concepts"/> we shall see that everything stored in git
history, including file data and directory contents, is stored in an object
with a name that is a hash of its contents.</simpara>
<section id="understanding-reachability">
<title>Understanding history: commits, parents, and reachability</title>
<simpara>Every commit (except the very first commit in a project) also has a
parent commit which shows what happened before this commit.
Following the chain of parents will eventually take you back to the
beginning of the project.</simpara>
<simpara>However, the commits do not form a simple list; git allows lines of
development to diverge and then reconverge, and the point where two
lines of development reconverge is called a "merge".  The commit
representing a merge can therefore have more than one parent, with
each parent representing the most recent commit on one of the lines
of development leading to that point.</simpara>
<simpara>The best way to see how this works is using the <ulink url="gitk.html">gitk(1)</ulink>
command; running gitk now on a git repository and looking for merge
commits will help understand how the git organizes history.</simpara>
<simpara>In the following, we say that commit X is "reachable" from commit Y
if commit X is an ancestor of commit Y.  Equivalently, you could say
that Y is a descendant of X, or that there is a chain of parents
leading from commit Y to commit X.</simpara>
</section>
<section id="history-diagrams">
<title>Understanding history: History diagrams</title>
<simpara>We will sometimes represent git history using diagrams like the one
below.  Commits are shown as "o", and the links between them with
lines drawn with - / and \.  Time goes left to right:</simpara>
<literallayout class="monospaced">         o--o--o &lt;-- Branch A
        /
 o--o--o &lt;-- master
        \
         o--o--o &lt;-- Branch B</literallayout>
<simpara>If we need to talk about a particular commit, the character "o" may
be replaced with another letter or number.</simpara>
</section>
<section id="what-is-a-branch">
<title>Understanding history: What is a branch?</title>
<simpara>When we need to be precise, we will use the word "branch" to mean a line
of development, and "branch head" (or just "head") to mean a reference
to the most recent commit on a branch.  In the example above, the branch
head named "A" is a pointer to one particular commit, but we refer to
the line of three commits leading up to that point as all being part of
"branch A".</simpara>
<simpara>However, when no confusion will result, we often just use the term
"branch" both for branches and for branch heads.</simpara>
</section>
</section>
<section id="manipulating-branches">
<title>Manipulating branches</title>
<simpara>Creating, deleting, and modifying branches is quick and easy; here&#8217;s
a summary of the commands:</simpara>
<variablelist>
<varlistentry>
<term>
git branch
</term>
<listitem>
<simpara>
        list all branches
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
git branch &lt;branch&gt;
</term>
<listitem>
<simpara>
        create a new branch named &lt;branch&gt;, referencing the same
        point in history as the current branch
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
git branch &lt;branch&gt; &lt;start-point&gt;
</term>
<listitem>
<simpara>
        create a new branch named &lt;branch&gt;, referencing
        &lt;start-point&gt;, which may be specified any way you like,
        including using a branch name or a tag name
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
git branch -d &lt;branch&gt;
</term>
<listitem>
<simpara>
        delete the branch &lt;branch&gt;; if the branch you are deleting
        points to a commit which is not reachable from the current
        branch, this command will fail with a warning.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
git branch -D &lt;branch&gt;
</term>
<listitem>
<simpara>
        even if the branch points to a commit not reachable
        from the current branch, you may know that that commit
        is still reachable from some other branch or tag.  In that
        case it is safe to use this command to force git to delete
        the branch.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
git checkout &lt;branch&gt;
</term>
<listitem>
<simpara>
        make the current branch &lt;branch&gt;, updating the working
        directory to reflect the version referenced by &lt;branch&gt;
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
git checkout -b &lt;new&gt; &lt;start-point&gt;
</term>
<listitem>
<simpara>
        create a new branch &lt;new&gt; referencing &lt;start-point&gt;, and
        check it out.
</simpara>
</listitem>
</varlistentry>
</variablelist>
<simpara>The special symbol "HEAD" can always be used to refer to the current
branch.  In fact, git uses a file named "HEAD" in the .git directory to
remember which branch is current:</simpara>
<literallayout>$ cat .git/HEAD
ref: refs/heads/master</literallayout>
</section>
<section id="detached-head">
<title>Examining an old version without creating a new branch</title>
<simpara>The git-checkout command normally expects a branch head, but will also
accept an arbitrary commit; for example, you can check out the commit
referenced by a tag:</simpara>
<literallayout>$ git checkout v2.6.17
Note: moving to "v2.6.17" which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b &lt;new_branch_name&gt;
HEAD is now at 427abfa... Linux v2.6.17</literallayout>
<simpara>The HEAD then refers to the SHA1 of the commit instead of to a branch,
and git branch shows that you are no longer on a branch:</simpara>
<literallayout>$ cat .git/HEAD
427abfa28afedffadfca9dd8b067eb6d36bac53f
$ git branch
* (no branch)
  master</literallayout>
<simpara>In this case we say that the HEAD is "detached".</simpara>
<simpara>This is an easy way to check out a particular version without having to
make up a name for the new branch.   You can still create a new branch
(or tag) for this version later if you decide to.</simpara>
</section>
<section id="examining-remote-branches">
<title>Examining branches from a remote repository</title>
<simpara>The "master" branch that was created at the time you cloned is a copy
of the HEAD in the repository that you cloned from.  That repository
may also have had other branches, though, and your local repository
keeps branches which track each of those remote branches, which you
can view using the "-r" option to <ulink url="git-branch.html">git-branch(1)</ulink>:</simpara>
<literallayout>$ git branch -r
  origin/HEAD
  origin/html
  origin/maint
  origin/man
  origin/master
  origin/next
  origin/pu
  origin/todo</literallayout>
<simpara>You cannot check out these remote-tracking branches, but you can
examine them on a branch of your own, just as you would a tag:</simpara>
<literallayout>$ git checkout -b my-todo-copy origin/todo</literallayout>
<simpara>Note that the name "origin" is just the name that git uses by default
to refer to the repository that you cloned from.</simpara>
</section>
<section id="how-git-stores-references">
<title>Naming branches, tags, and other references</title>
<simpara>Branches, remote-tracking branches, and tags are all references to
commits.  All references are named with a slash-separated path name
starting with "refs"; the names we&#8217;ve been using so far are actually
shorthand:</simpara>
<itemizedlist>
<listitem>
<simpara>
The branch "test" is short for "refs/heads/test".
</simpara>
</listitem>
<listitem>
<simpara>
The tag "v2.6.18" is short for "refs/tags/v2.6.18".
</simpara>
</listitem>
<listitem>
<simpara>
"origin/master" is short for "refs/remotes/origin/master".
</simpara>
</listitem>
</itemizedlist>
<simpara>The full name is occasionally useful if, for example, there ever
exists a tag and a branch with the same name.</simpara>
<simpara>(Newly created refs are actually stored in the .git/refs directory,
under the path given by their name.  However, for efficiency reasons
they may also be packed together in a single file; see
<ulink url="git-pack-refs.html">git-pack-refs(1)</ulink>).</simpara>
<simpara>As another useful shortcut, the "HEAD" of a repository can be referred
to just using the name of that repository.  So, for example, "origin"
is usually a shortcut for the HEAD branch in the repository "origin".</simpara>
<simpara>For the complete list of paths which git checks for references, and
the order it uses to decide which to choose when there are multiple
references with the same shorthand name, see the "SPECIFYING
REVISIONS" section of <ulink url="git-rev-parse.html">git-rev-parse(1)</ulink>.</simpara>
</section>
<section id="Updating-a-repository-With-git-fetch">
<title>Updating a repository with git-fetch</title>
<simpara>Eventually the developer cloned from will do additional work in her
repository, creating new commits and advancing the branches to point
at the new commits.</simpara>
<simpara>The command "git fetch", with no arguments, will update all of the
remote-tracking branches to the latest version found in her
repository.  It will not touch any of your own branches&#8212;not even the
"master" branch that was created for you on clone.</simpara>
</section>
<section id="fetching-branches">
<title>Fetching branches from other repositories</title>
<simpara>You can also track branches from repositories other than the one you
cloned from, using <ulink url="git-remote.html">git-remote(1)</ulink>:</simpara>
<literallayout>$ git remote add linux-nfs git://linux-nfs.org/pub/nfs-2.6.git
$ git fetch linux-nfs
* refs/remotes/linux-nfs/master: storing branch 'master' ...
  commit: bf81b46</literallayout>
<simpara>New remote-tracking branches will be stored under the shorthand name
that you gave "git-remote add", in this case linux-nfs:</simpara>
<literallayout>$ git branch -r
linux-nfs/master
origin/master</literallayout>
<simpara>If you run "git fetch &lt;remote&gt;" later, the tracking branches for the
named &lt;remote&gt; will be updated.</simpara>
<simpara>If you examine the file .git/config, you will see that git has added
a new stanza:</simpara>
<literallayout>$ cat .git/config
...
[remote "linux-nfs"]
        url = git://linux-nfs.org/pub/nfs-2.6.git
        fetch = +refs/heads/*:refs/remotes/linux-nfs/*
...</literallayout>
<simpara>This is what causes git to track the remote&#8217;s branches; you may modify
or delete these configuration options by editing .git/config with a
text editor.  (See the "CONFIGURATION FILE" section of
<ulink url="git-config.html">git-config(1)</ulink> for details.)</simpara>
</section>
</section>
<section id="exploring-git-history">
<title>Exploring git history</title>
<simpara>Git is best thought of as a tool for storing the history of a
collection of files.  It does this by storing compressed snapshots of
the contents of a file hierarchy, together with "commits" which show
the relationships between these snapshots.</simpara>
<simpara>Git provides extremely flexible and fast tools for exploring the
history of a project.</simpara>
<simpara>We start with one specialized tool that is useful for finding the
commit that introduced a bug into a project.</simpara>
<section id="using-bisect">
<title>How to use bisect to find a regression</title>
<simpara>Suppose version 2.6.18 of your project worked, but the version at
"master" crashes.  Sometimes the best way to find the cause of such a
regression is to perform a brute-force search through the project&#8217;s
history to find the particular commit that caused the problem.  The
<ulink url="git-bisect.html">git-bisect(1)</ulink> command can help you do this:</simpara>
<literallayout>$ git bisect start
$ git bisect good v2.6.18
$ git bisect bad master
Bisecting: 3537 revisions left to test after this
[65934a9a028b88e83e2b0f8b36618fe503349f8e] BLOCK: Make USB storage depend on SCSI rather than selecting it [try #6]</literallayout>
<simpara>If you run "git branch" at this point, you&#8217;ll see that git has
temporarily moved you in "(no branch)". HEAD is now detached from any
branch and points directly to a commit (with commit id 65934&#8230;) that
is reachable from "master" but not from v2.6.18. Compile and test it,
and see whether it crashes. Assume it does crash. Then:</simpara>
<literallayout>$ git bisect bad
Bisecting: 1769 revisions left to test after this
[7eff82c8b1511017ae605f0c99ac275a7e21b867] i2c-core: Drop useless bitmaskings</literallayout>
<simpara>checks out an older version.  Continue like this, telling git at each
stage whether the version it gives you is good or bad, and notice
that the number of revisions left to test is cut approximately in
half each time.</simpara>
<simpara>After about 13 tests (in this case), it will output the commit id of
the guilty commit.  You can then examine the commit with
<ulink url="git-show.html">git-show(1)</ulink>, find out who wrote it, and mail them your bug
report with the commit id.  Finally, run</simpara>
<literallayout>$ git bisect reset</literallayout>
<simpara>to return you to the branch you were on before.</simpara>
<simpara>Note that the version which git-bisect checks out for you at each
point is just a suggestion, and you&#8217;re free to try a different
version if you think it would be a good idea.  For example,
occasionally you may land on a commit that broke something unrelated;
run</simpara>
<literallayout>$ git bisect visualize</literallayout>
<simpara>which will run gitk and label the commit it chose with a marker that
says "bisect".  Choose a safe-looking commit nearby, note its commit
id, and check it out with:</simpara>
<literallayout>$ git reset --hard fb47ddb2db...</literallayout>
<simpara>then test, run "bisect good" or "bisect bad" as appropriate, and
continue.</simpara>
<simpara>Instead of "git bisect visualize" and then "git reset --hard
fb47ddb2db&#8230;", you might just want to tell git that you want to skip
the current commit:</simpara>
<literallayout>$ git bisect skip</literallayout>
<simpara>In this case, though, git may not eventually be able to tell the first
bad one between some first skipped commits and a later bad commit.</simpara>
<simpara>There are also ways to automate the bisecting process if you have a
test script that can tell a good from a bad commit. See
<ulink url="git-bisect.html">git-bisect(1)</ulink> for more information about this and other "git
bisect" features.</simpara>
</section>
<section id="naming-commits">
<title>Naming commits</title>
<simpara>We have seen several ways of naming commits already:</simpara>
<itemizedlist>
<listitem>
<simpara>
40-hexdigit object name
</simpara>
</listitem>
<listitem>
<simpara>
branch name: refers to the commit at the head of the given
          branch
</simpara>
</listitem>
<listitem>
<simpara>
tag name: refers to the commit pointed to by the given tag
          (we&#8217;ve seen branches and tags are special cases of
          <link linkend="how-git-stores-references">references</link>).
</simpara>
</listitem>
<listitem>
<simpara>
HEAD: refers to the head of the current branch
</simpara>
</listitem>
</itemizedlist>
<simpara>There are many more; see the "SPECIFYING REVISIONS" section of the
<ulink url="git-rev-parse.html">git-rev-parse(1)</ulink> man page for the complete list of ways to
name revisions.  Some examples:</simpara>
<literallayout>$ git show fb47ddb2 # the first few characters of the object name
                    # are usually enough to specify it uniquely
$ git show HEAD^    # the parent of the HEAD commit
$ git show HEAD^^   # the grandparent
$ git show HEAD~4   # the great-great-grandparent</literallayout>
<simpara>Recall that merge commits may have more than one parent; by default,
^ and ~ follow the first parent listed in the commit, but you can
also choose:</simpara>
<literallayout>$ git show HEAD^1   # show the first parent of HEAD
$ git show HEAD^2   # show the second parent of HEAD</literallayout>
<simpara>In addition to HEAD, there are several other special names for
commits:</simpara>
<simpara>Merges (to be discussed later), as well as operations such as
git-reset, which change the currently checked-out commit, generally
set ORIG_HEAD to the value HEAD had before the current operation.</simpara>
<simpara>The git-fetch operation always stores the head of the last fetched
branch in FETCH_HEAD.  For example, if you run git fetch without
specifying a local branch as the target of the operation</simpara>
<literallayout>$ git fetch git://example.com/proj.git theirbranch</literallayout>
<simpara>the fetched commits will still be available from FETCH_HEAD.</simpara>
<simpara>When we discuss merges we&#8217;ll also see the special name MERGE_HEAD,
which refers to the other branch that we&#8217;re merging in to the current
branch.</simpara>
<simpara>The <ulink url="git-rev-parse.html">git-rev-parse(1)</ulink> command is a low-level command that is
occasionally useful for translating some name for a commit to the object
name for that commit:</simpara>
<literallayout>$ git rev-parse origin
e05db0fd4f31dde7005f075a84f96b360d05984b</literallayout>
</section>
<section id="creating-tags">
<title>Creating tags</title>
<simpara>We can also create a tag to refer to a particular commit; after
running</simpara>
<literallayout>$ git tag stable-1 1b2e1d63ff</literallayout>
<simpara>You can use stable-1 to refer to the commit 1b2e1d63ff.</simpara>
<simpara>This creates a "lightweight" tag.  If you would also like to include a
comment with the tag, and possibly sign it cryptographically, then you
should create a tag object instead; see the <ulink url="git-tag.html">git-tag(1)</ulink> man page
for details.</simpara>
</section>
<section id="browsing-revisions">
<title>Browsing revisions</title>
<simpara>The <ulink url="git-log.html">git-log(1)</ulink> command can show lists of commits.  On its
own, it shows all commits reachable from the parent commit; but you
can also make more specific requests:</simpara>
<literallayout>$ git log v2.5..        # commits since (not reachable from) v2.5
$ git log test..master  # commits reachable from master but not test
$ git log master..test  # ...reachable from test but not master
$ git log master...test # ...reachable from either test or master,
                        #    but not both
$ git log --since="2 weeks ago" # commits from the last 2 weeks
$ git log Makefile      # commits which modify Makefile
$ git log fs/           # ... which modify any file under fs/
$ git log -S'foo()'     # commits which add or remove any file data
                        # matching the string 'foo()'</literallayout>
<simpara>And of course you can combine all of these; the following finds
commits since v2.5 which touch the Makefile or any file under fs:</simpara>
<literallayout>$ git log v2.5.. Makefile fs/</literallayout>
<simpara>You can also ask git log to show patches:</simpara>
<literallayout>$ git log -p</literallayout>
<simpara>See the "--pretty" option in the <ulink url="git-log.html">git-log(1)</ulink> man page for more
display options.</simpara>
<simpara>Note that git log starts with the most recent commit and works
backwards through the parents; however, since git history can contain
multiple independent lines of development, the particular order that
commits are listed in may be somewhat arbitrary.</simpara>
</section>
<section id="generating-diffs">
<title>Generating diffs</title>
<simpara>You can generate diffs between any two versions using
<ulink url="git-diff.html">git-diff(1)</ulink>:</simpara>
<literallayout>$ git diff master..test</literallayout>
<simpara>That will produce the diff between the tips of the two branches.  If
you&#8217;d prefer to find the diff from their common ancestor to test, you
can use three dots instead of two:</simpara>
<literallayout>$ git diff master...test</literallayout>
<simpara>Sometimes what you want instead is a set of patches; for this you can
use <ulink url="git-format-patch.html">git-format-patch(1)</ulink>:</simpara>
<literallayout>$ git format-patch master..test</literallayout>
<simpara>will generate a file with a patch for each commit reachable from test
but not from master.</simpara>
</section>
<section id="viewing-old-file-versions">
<title>Viewing old file versions</title>
<simpara>You can always view an old version of a file by just checking out the
correct revision first.  But sometimes it is more convenient to be
able to view an old version of a single file without checking
anything out; this command does that:</simpara>
<literallayout>$ git show v2.5:fs/locks.c</literallayout>
<simpara>Before the colon may be anything that names a commit, and after it
may be any path to a file tracked by git.</simpara>
</section>
<section id="history-examples">
<title>Examples</title>
<section id="counting-commits-on-a-branch">
<title>Counting the number of commits on a branch</title>
<simpara>Suppose you want to know how many commits you&#8217;ve made on "mybranch"
since it diverged from "origin":</simpara>
<literallayout>$ git log --pretty=oneline origin..mybranch | wc -l</literallayout>
<simpara>Alternatively, you may often see this sort of thing done with the
lower-level command <ulink url="git-rev-list.html">git-rev-list(1)</ulink>, which just lists the SHA1&#8217;s
of all the given commits:</simpara>
<literallayout>$ git rev-list origin..mybranch | wc -l</literallayout>
</section>
<section id="checking-for-equal-branches">
<title>Check whether two branches point at the same history</title>
<simpara>Suppose you want to check whether two branches point at the same point
in history.</simpara>
<literallayout>$ git diff origin..master</literallayout>
<simpara>will tell you whether the contents of the project are the same at the
two branches; in theory, however, it&#8217;s possible that the same project
contents could have been arrived at by two different historical
routes.  You could compare the object names:</simpara>
<literallayout>$ git rev-list origin
e05db0fd4f31dde7005f075a84f96b360d05984b
$ git rev-list master
e05db0fd4f31dde7005f075a84f96b360d05984b</literallayout>
<simpara>Or you could recall that the &#8230; operator selects all commits
contained reachable from either one reference or the other but not
both: so</simpara>
<literallayout>$ git log origin...master</literallayout>
<simpara>will return no commits when the two branches are equal.</simpara>
</section>
<section id="finding-tagged-descendants">
<title>Find first tagged version including a given fix</title>
<simpara>Suppose you know that the commit e05db0fd fixed a certain problem.
You&#8217;d like to find the earliest tagged release that contains that
fix.</simpara>
<simpara>Of course, there may be more than one answer&#8212;if the history branched
after commit e05db0fd, then there could be multiple "earliest" tagged
releases.</simpara>
<simpara>You could just visually inspect the commits since e05db0fd:</simpara>
<literallayout>$ gitk e05db0fd..</literallayout>
<simpara>Or you can use <ulink url="git-name-rev.html">git-name-rev(1)</ulink>, which will give the commit a
name based on any tag it finds pointing to one of the commit&#8217;s
descendants:</simpara>
<literallayout>$ git name-rev --tags e05db0fd
e05db0fd tags/v1.5.0-rc1^0~23</literallayout>
<simpara>The <ulink url="git-describe.html">git-describe(1)</ulink> command does the opposite, naming the
revision using a tag on which the given commit is based:</simpara>
<literallayout>$ git describe e05db0fd
v1.5.0-rc0-260-ge05db0f</literallayout>
<simpara>but that may sometimes help you guess which tags might come after the
given commit.</simpara>
<simpara>If you just want to verify whether a given tagged version contains a
given commit, you could use <ulink url="git-merge-base.html">git-merge-base(1)</ulink>:</simpara>
<literallayout>$ git merge-base e05db0fd v1.5.0-rc1
e05db0fd4f31dde7005f075a84f96b360d05984b</literallayout>
<simpara>The merge-base command finds a common ancestor of the given commits,
and always returns one or the other in the case where one is a
descendant of the other; so the above output shows that e05db0fd
actually is an ancestor of v1.5.0-rc1.</simpara>
<simpara>Alternatively, note that</simpara>
<literallayout>$ git log v1.5.0-rc1..e05db0fd</literallayout>
<simpara>will produce empty output if and only if v1.5.0-rc1 includes e05db0fd,
because it outputs only commits that are not reachable from v1.5.0-rc1.</simpara>
<simpara>As yet another alternative, the <ulink url="git-show-branch.html">git-show-branch(1)</ulink> command lists
the commits reachable from its arguments with a display on the left-hand
side that indicates which arguments that commit is reachable from.  So,
you can run something like</simpara>
<literallayout>$ git show-branch e05db0fd v1.5.0-rc0 v1.5.0-rc1 v1.5.0-rc2
! [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
available
 ! [v1.5.0-rc0] GIT v1.5.0 preview
  ! [v1.5.0-rc1] GIT v1.5.0-rc1
   ! [v1.5.0-rc2] GIT v1.5.0-rc2
...</literallayout>
<simpara>then search for a line that looks like</simpara>
<literallayout>+ ++ [e05db0fd] Fix warnings in sha1_file.c - use C99 printf format if
available</literallayout>
<simpara>Which shows that e05db0fd is reachable from itself, from v1.5.0-rc1, and
from v1.5.0-rc2, but not from v1.5.0-rc0.</simpara>
</section>
<section id="showing-commits-unique-to-a-branch">
<title>Showing commits unique to a given branch</title>
<simpara>Suppose you would like to see all the commits reachable from the branch
head named "master" but not from any other head in your repository.</simpara>
<simpara>We can list all the heads in this repository with
<ulink url="git-show-ref.html">git-show-ref(1)</ulink>:</simpara>
<literallayout>$ git show-ref --heads
bf62196b5e363d73353a9dcf094c59595f3153b7 refs/heads/core-tutorial
db768d5504c1bb46f63ee9d6e1772bd047e05bf9 refs/heads/maint
a07157ac624b2524a059a3414e99f6f44bebc1e7 refs/heads/master
24dbc180ea14dc1aebe09f14c8ecf32010690627 refs/heads/tutorial-2
1e87486ae06626c2f31eaa63d26fc0fd646c8af2 refs/heads/tutorial-fixes</literallayout>
<simpara>We can get just the branch-head names, and remove "master", with
the help of the standard utilities cut and grep:</simpara>
<literallayout>$ git show-ref --heads | cut -d' ' -f2 | grep -v '^refs/heads/master'
refs/heads/core-tutorial
refs/heads/maint
refs/heads/tutorial-2
refs/heads/tutorial-fixes</literallayout>
<simpara>And then we can ask to see all the commits reachable from master
but not from these other heads:</simpara>
<literallayout>$ gitk master --not $( git show-ref --heads | cut -d' ' -f2 |
                                grep -v '^refs/heads/master' )</literallayout>
<simpara>Obviously, endless variations are possible; for example, to see all
commits reachable from some head but not from any tag in the repository:</simpara>
<literallayout>$ gitk $( git show-ref --heads ) --not  $( git show-ref --tags )</literallayout>
<simpara>(See <ulink url="git-rev-parse.html">git-rev-parse(1)</ulink> for explanations of commit-selecting
syntax such as <literal>--not</literal>.)</simpara>
</section>
<section id="making-a-release">
<title>Creating a changelog and tarball for a software release</title>
<simpara>The <ulink url="git-archive.html">git-archive(1)</ulink> command can create a tar or zip archive from
any version of a project; for example:</simpara>
<literallayout>$ git archive --format=tar --prefix=project/ HEAD | gzip &gt;latest.tar.gz</literallayout>
<simpara>will use HEAD to produce a tar archive in which each filename is
preceded by "project/".</simpara>
<simpara>If you&#8217;re releasing a new version of a software project, you may want
to simultaneously make a changelog to include in the release
announcement.</simpara>
<simpara>Linus Torvalds, for example, makes new kernel releases by tagging them,
then running:</simpara>
<literallayout>$ release-script 2.6.12 2.6.13-rc6 2.6.13-rc7</literallayout>
<simpara>where release-script is a shell script that looks like:</simpara>
<literallayout>#!/bin/sh
stable="$1"
last="$2"
new="$3"
echo "# git tag v$new"
echo "git archive --prefix=linux-$new/ v$new | gzip -9 &gt; ../linux-$new.tar.gz"
echo "git diff v$stable v$new | gzip -9 &gt; ../patch-$new.gz"
echo "git log --no-merges v$new ^v$last &gt; ../ChangeLog-$new"
echo "git shortlog --no-merges v$new ^v$last &gt; ../ShortLog"
echo "git diff --stat --summary -M v$last v$new &gt; ../diffstat-$new"</literallayout>
<simpara>and then he just cut-and-pastes the output commands after verifying that
they look OK.</simpara>
</section>
<section id="Finding-comments-With-given-Content">
<title>Finding commits referencing a file with given content</title>
<simpara>Somebody hands you a copy of a file, and asks which commits modified a
file such that it contained the given content either before or after the
commit.  You can find out with this:</simpara>
<literallayout>$  git log --raw --abbrev=40 --pretty=oneline |
        grep -B 1 `git hash-object filename`</literallayout>
<simpara>Figuring out why this works is left as an exercise to the (advanced)
student.  The <ulink url="git-log.html">git-log(1)</ulink>, <ulink url="git-diff-tree.html">git-diff-tree(1)</ulink>, and
<ulink url="git-hash-object.html">git-hash-object(1)</ulink> man pages may prove helpful.</simpara>
</section>
</section>
</section>
<section id="Developing-With-git">
<title>Developing with git</title>
<section id="telling-git-your-name">
<title>Telling git your name</title>
<simpara>Before creating any commits, you should introduce yourself to git.  The
easiest way to do so is to make sure the following lines appear in a
file named .gitconfig in your home directory:</simpara>
<literallayout>[user]
        name = Your Name Comes Here
        email = you@yourdomain.example.com</literallayout>
<simpara>(See the "CONFIGURATION FILE" section of <ulink url="git-config.html">git-config(1)</ulink> for
details on the configuration file.)</simpara>
</section>
<section id="creating-a-new-repository">
<title>Creating a new repository</title>
<simpara>Creating a new repository from scratch is very easy:</simpara>
<literallayout>$ mkdir project
$ cd project
$ git init</literallayout>
<simpara>If you have some initial content (say, a tarball):</simpara>
<literallayout>$ tar xzvf project.tar.gz
$ cd project
$ git init
$ git add . # include everything below ./ in the first commit:
$ git commit</literallayout>
</section>
<section id="how-to-make-a-commit">
<title>How to make a commit</title>
<simpara>Creating a new commit takes three steps:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>
Making some changes to the working directory using your
           favorite editor.
</simpara>
</listitem>
<listitem>
<simpara>
Telling git about your changes.
</simpara>
</listitem>
<listitem>
<simpara>
Creating the commit using the content you told git about
           in step 2.
</simpara>
</listitem>
</orderedlist>
<simpara>In practice, you can interleave and repeat steps 1 and 2 as many
times as you want: in order to keep track of what you want committed
at step 3, git maintains a snapshot of the tree&#8217;s contents in a
special staging area called "the index."</simpara>
<simpara>At the beginning, the content of the index will be identical to
that of the HEAD.  The command "git diff --cached", which shows
the difference between the HEAD and the index, should therefore
produce no output at that point.</simpara>
<simpara>Modifying the index is easy:</simpara>
<simpara>To update the index with the new contents of a modified file, use</simpara>
<literallayout>$ git add path/to/file</literallayout>
<simpara>To add the contents of a new file to the index, use</simpara>
<literallayout>$ git add path/to/file</literallayout>
<simpara>To remove a file from the index and from the working tree,</simpara>
<literallayout>$ git rm path/to/file</literallayout>
<simpara>After each step you can verify that</simpara>
<literallayout>$ git diff --cached</literallayout>
<simpara>always shows the difference between the HEAD and the index file&#8212;this
is what you&#8217;d commit if you created the commit now&#8212;and that</simpara>
<literallayout>$ git diff</literallayout>
<simpara>shows the difference between the working tree and the index file.</simpara>
<simpara>Note that "git-add" always adds just the current contents of a file
to the index; further changes to the same file will be ignored unless
you run git-add on the file again.</simpara>
<simpara>When you&#8217;re ready, just run</simpara>
<literallayout>$ git commit</literallayout>
<simpara>and git will prompt you for a commit message and then create the new
commit.  Check to make sure it looks like what you expected with</simpara>
<literallayout>$ git show</literallayout>
<simpara>As a special shortcut,</simpara>
<literallayout>$ git commit -a</literallayout>
<simpara>will update the index with any files that you&#8217;ve modified or removed
and create a commit, all in one step.</simpara>
<simpara>A number of commands are useful for keeping track of what you&#8217;re
about to commit:</simpara>
<literallayout>$ git diff --cached # difference between HEAD and the index; what
                    # would be committed if you ran "commit" now.
$ git diff          # difference between the index file and your
                    # working directory; changes that would not
                    # be included if you ran "commit" now.
$ git diff HEAD     # difference between HEAD and working tree; what
                    # would be committed if you ran "commit -a" now.
$ git status        # a brief per-file summary of the above.</literallayout>
<simpara>You can also use <ulink url="git-gui.html">git-gui(1)</ulink> to create commits, view changes in
the index and the working tree files, and individually select diff hunks
for inclusion in the index (by right-clicking on the diff hunk and
choosing "Stage Hunk For Commit").</simpara>
</section>
<section id="creating-good-commit-messages">
<title>Creating good commit messages</title>
<simpara>Though not required, it&#8217;s a good idea to begin the commit message
with a single short (less than 50 character) line summarizing the
change, followed by a blank line and then a more thorough
description.  Tools that turn commits into email, for example, use
the first line on the Subject line and the rest of the commit in the
body.</simpara>
</section>
<section id="ignoring-files">
<title>Ignoring files</title>
<simpara>A project will often generate files that you do <emphasis>not</emphasis> want to track with git.
This typically includes files generated by a build process or temporary
backup files made by your editor. Of course, <emphasis>not</emphasis> tracking files with git
is just a matter of <emphasis>not</emphasis> calling "<literal>git-add</literal>" on them. But it quickly becomes
annoying to have these untracked files lying around; e.g. they make
"<literal>git add .</literal>" practically useless, and they keep showing up in the output of
"<literal>git status</literal>".</simpara>
<simpara>You can tell git to ignore certain files by creating a file called .gitignore
in the top level of your working directory, with contents such as:</simpara>
<literallayout># Lines starting with '#' are considered comments.
# Ignore any file named foo.txt.
foo.txt
# Ignore (generated) html files,
*.html
# except foo.html which is maintained by hand.
!foo.html
# Ignore objects and archives.
*.[oa]</literallayout>
<simpara>See <ulink url="gitignore.html">gitignore(5)</ulink> for a detailed explanation of the syntax.  You can
also place .gitignore files in other directories in your working tree, and they
will apply to those directories and their subdirectories.  The <literal>.gitignore</literal>
files can be added to your repository like any other files (just run <literal>git add
.gitignore</literal> and <literal>git commit</literal>, as usual), which is convenient when the exclude
patterns (such as patterns matching build output files) would also make sense
for other users who clone your repository.</simpara>
<simpara>If you wish the exclude patterns to affect only certain repositories
(instead of every repository for a given project), you may instead put
them in a file in your repository named .git/info/exclude, or in any file
specified by the <literal>core.excludesfile</literal> configuration variable.  Some git
commands can also take exclude patterns directly on the command line.
See <ulink url="gitignore.html">gitignore(5)</ulink> for the details.</simpara>
</section>
<section id="how-to-merge">
<title>How to merge</title>
<simpara>You can rejoin two diverging branches of development using
<ulink url="git-merge.html">git-merge(1)</ulink>:</simpara>
<literallayout>$ git merge branchname</literallayout>
<simpara>merges the development in the branch "branchname" into the current
branch.  If there are conflicts&#8212;for example, if the same file is
modified in two different ways in the remote branch and the local
branch&#8212;then you are warned; the output may look something like this:</simpara>
<literallayout>$ git merge next
 100% (4/4) done
Auto-merged file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.</literallayout>
<simpara>Conflict markers are left in the problematic files, and after
you resolve the conflicts manually, you can update the index
with the contents and run git commit, as you normally would when
creating a new file.</simpara>
<simpara>If you examine the resulting commit using gitk, you will see that it
has two parents, one pointing to the top of the current branch, and
one to the top of the other branch.</simpara>
</section>
<section id="resolving-a-merge">
<title>Resolving a merge</title>
<simpara>When a merge isn&#8217;t resolved automatically, git leaves the index and
the working tree in a special state that gives you all the
information you need to help resolve the merge.</simpara>
<simpara>Files with conflicts are marked specially in the index, so until you
resolve the problem and update the index, <ulink url="git-commit.html">git-commit(1)</ulink> will
fail:</simpara>
<literallayout>$ git commit
file.txt: needs merge</literallayout>
<simpara>Also, <ulink url="git-status.html">git-status(1)</ulink> will list those files as "unmerged", and the
files with conflicts will have conflict markers added, like this:</simpara>
<literallayout>&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD:file.txt
Hello world
=======
Goodbye
&gt;&gt;&gt;&gt;&gt;&gt;&gt; 77976da35a11db4580b80ae27e8d65caf5208086:file.txt</literallayout>
<simpara>All you need to do is edit the files to resolve the conflicts, and then</simpara>
<literallayout>$ git add file.txt
$ git commit</literallayout>
<simpara>Note that the commit message will already be filled in for you with
some information about the merge.  Normally you can just use this
default message unchanged, but you may add additional commentary of
your own if desired.</simpara>
<simpara>The above is all you need to know to resolve a simple merge.  But git
also provides more information to help resolve conflicts:</simpara>
<section id="conflict-resolution">
<title>Getting conflict-resolution help during a merge</title>
<simpara>All of the changes that git was able to merge automatically are
already added to the index file, so <ulink url="git-diff.html">git-diff(1)</ulink> shows only
the conflicts.  It uses an unusual syntax:</simpara>
<literallayout>$ git diff
diff --cc file.txt
index 802992c,2b60207..0000000
--- a/file.txt
+++ b/file.txt
@@@ -1,1 -1,1 +1,5 @@@
++&lt;&lt;&lt;&lt;&lt;&lt;&lt; HEAD:file.txt
 +Hello world
++=======
+ Goodbye
++&gt;&gt;&gt;&gt;&gt;&gt;&gt; 77976da35a11db4580b80ae27e8d65caf5208086:file.txt</literallayout>
<simpara>Recall that the commit which will be committed after we resolve this
conflict will have two parents instead of the usual one: one parent
will be HEAD, the tip of the current branch; the other will be the
tip of the other branch, which is stored temporarily in MERGE_HEAD.</simpara>
<simpara>During the merge, the index holds three versions of each file.  Each of
these three "file stages" represents a different version of the file:</simpara>
<literallayout>$ git show :1:file.txt  # the file in a common ancestor of both branches
$ git show :2:file.txt  # the version from HEAD.
$ git show :3:file.txt  # the version from MERGE_HEAD.</literallayout>
<simpara>When you ask <ulink url="git-diff.html">git-diff(1)</ulink> to show the conflicts, it runs a
three-way diff between the conflicted merge results in the work tree with
stages 2 and 3 to show only hunks whose contents come from both sides,
mixed (in other words, when a hunk&#8217;s merge results come only from stage 2,
that part is not conflicting and is not shown.  Same for stage 3).</simpara>
<simpara>The diff above shows the differences between the working-tree version of
file.txt and the stage 2 and stage 3 versions.  So instead of preceding
each line by a single "+" or "-", it now uses two columns: the first
column is used for differences between the first parent and the working
directory copy, and the second for differences between the second parent
and the working directory copy.  (See the "COMBINED DIFF FORMAT" section
of <ulink url="git-diff-files.html">git-diff-files(1)</ulink> for a details of the format.)</simpara>
<simpara>After resolving the conflict in the obvious way (but before updating the
index), the diff will look like:</simpara>
<literallayout>$ git diff
diff --cc file.txt
index 802992c,2b60207..0000000
--- a/file.txt
+++ b/file.txt
@@@ -1,1 -1,1 +1,1 @@@
- Hello world
 -Goodbye
++Goodbye world</literallayout>
<simpara>This shows that our resolved version deleted "Hello world" from the
first parent, deleted "Goodbye" from the second parent, and added
"Goodbye world", which was previously absent from both.</simpara>
<simpara>Some special diff options allow diffing the working directory against
any of these stages:</simpara>
<literallayout>$ git diff -1 file.txt          # diff against stage 1
$ git diff --base file.txt      # same as the above
$ git diff -2 file.txt          # diff against stage 2
$ git diff --ours file.txt      # same as the above
$ git diff -3 file.txt          # diff against stage 3
$ git diff --theirs file.txt    # same as the above.</literallayout>
<simpara>The <ulink url="git-log.html">git-log(1)</ulink> and <ulink url="gitk.html">gitk(1)</ulink> commands also provide special help
for merges:</simpara>
<literallayout>$ git log --merge
$ gitk --merge</literallayout>
<simpara>These will display all commits which exist only on HEAD or on
MERGE_HEAD, and which touch an unmerged file.</simpara>
<simpara>You may also use <ulink url="git-mergetool.html">git-mergetool(1)</ulink>, which lets you merge the
unmerged files using external tools such as Emacs or kdiff3.</simpara>
<simpara>Each time you resolve the conflicts in a file and update the index:</simpara>
<literallayout>$ git add file.txt</literallayout>
<simpara>the different stages of that file will be "collapsed", after which
git-diff will (by default) no longer show diffs for that file.</simpara>
</section>
</section>
<section id="undoing-a-merge">
<title>Undoing a merge</title>
<simpara>If you get stuck and decide to just give up and throw the whole mess
away, you can always return to the pre-merge state with</simpara>
<literallayout>$ git reset --hard HEAD</literallayout>
<simpara>Or, if you&#8217;ve already committed the merge that you want to throw away,</simpara>
<literallayout>$ git reset --hard ORIG_HEAD</literallayout>
<simpara>However, this last command can be dangerous in some cases&#8212;never
throw away a commit you have already committed if that commit may
itself have been merged into another branch, as doing so may confuse
further merges.</simpara>
</section>
<section id="fast-forwards">
<title>Fast-forward merges</title>
<simpara>There is one special case not mentioned above, which is treated
differently.  Normally, a merge results in a merge commit, with two
parents, one pointing at each of the two lines of development that
were merged.</simpara>
<simpara>However, if the current branch is a descendant of the other&#8212;so every
commit present in the one is already contained in the other&#8212;then git
just performs a "fast forward"; the head of the current branch is moved
forward to point at the head of the merged-in branch, without any new
commits being created.</simpara>
</section>
<section id="fixing-mistakes">
<title>Fixing mistakes</title>
<simpara>If you&#8217;ve messed up the working tree, but haven&#8217;t yet committed your
mistake, you can return the entire working tree to the last committed
state with</simpara>
<literallayout>$ git reset --hard HEAD</literallayout>
<simpara>If you make a commit that you later wish you hadn&#8217;t, there are two
fundamentally different ways to fix the problem:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>
You can create a new commit that undoes whatever was done
        by the old commit.  This is the correct thing if your
        mistake has already been made public.
</simpara>
</listitem>
<listitem>
<simpara>
You can go back and modify the old commit.  You should
        never do this if you have already made the history public;
        git does not normally expect the "history" of a project to
        change, and cannot correctly perform repeated merges from
        a branch that has had its history changed.
</simpara>
</listitem>
</orderedlist>
<section id="reverting-a-commit">
<title>Fixing a mistake with a new commit</title>
<simpara>Creating a new commit that reverts an earlier change is very easy;
just pass the <ulink url="git-revert.html">git-revert(1)</ulink> command a reference to the bad
commit; for example, to revert the most recent commit:</simpara>
<literallayout>$ git revert HEAD</literallayout>
<simpara>This will create a new commit which undoes the change in HEAD.  You
will be given a chance to edit the commit message for the new commit.</simpara>
<simpara>You can also revert an earlier change, for example, the next-to-last:</simpara>
<literallayout>$ git revert HEAD^</literallayout>
<simpara>In this case git will attempt to undo the old change while leaving
intact any changes made since then.  If more recent changes overlap
with the changes to be reverted, then you will be asked to fix
conflicts manually, just as in the case of <link linkend="resolving-a-merge">resolving a merge</link>.</simpara>
</section>
<section id="fixing-a-mistake-by-rewriting-history">
<title>Fixing a mistake by rewriting history</title>
<simpara>If the problematic commit is the most recent commit, and you have not
yet made that commit public, then you may just
<link linkend="undoing-a-merge">destroy it using git-reset</link>.</simpara>
<simpara>Alternatively, you
can edit the working directory and update the index to fix your
mistake, just as if you were going to <link linkend="how-to-make-a-commit">create a new commit</link>, then run</simpara>
<literallayout>$ git commit --amend</literallayout>
<simpara>which will replace the old commit by a new commit incorporating your
changes, giving you a chance to edit the old commit message first.</simpara>
<simpara>Again, you should never do this to a commit that may already have
been merged into another branch; use <ulink url="git-revert.html">git-revert(1)</ulink> instead in
that case.</simpara>
<simpara>It is also possible to replace commits further back in the history, but
this is an advanced topic to be left for
<link linkend="cleaning-up-history">another chapter</link>.</simpara>
</section>
<section id="checkout-of-path">
<title>Checking out an old version of a file</title>
<simpara>In the process of undoing a previous bad change, you may find it
useful to check out an older version of a particular file using
<ulink url="git-checkout.html">git-checkout(1)</ulink>.  We&#8217;ve used git-checkout before to switch
branches, but it has quite different behavior if it is given a path
name: the command</simpara>
<literallayout>$ git checkout HEAD^ path/to/file</literallayout>
<simpara>replaces path/to/file by the contents it had in the commit HEAD^, and
also updates the index to match.  It does not change branches.</simpara>
<simpara>If you just want to look at an old version of the file, without
modifying the working directory, you can do that with
<ulink url="git-show.html">git-show(1)</ulink>:</simpara>
<literallayout>$ git show HEAD^:path/to/file</literallayout>
<simpara>which will display the given version of the file.</simpara>
</section>
<section id="interrupted-work">
<title>Temporarily setting aside work in progress</title>
<simpara>While you are in the middle of working on something complicated, you
find an unrelated but obvious and trivial bug.  You would like to fix it
before continuing.  You can use <ulink url="git-stash.html">git-stash(1)</ulink> to save the current
state of your work, and after fixing the bug (or, optionally after doing
so on a different branch and then coming back), unstash the
work-in-progress changes.</simpara>
<literallayout>$ git stash save "work in progress for foo feature"</literallayout>
<simpara>This command will save your changes away to the <literal>stash</literal>, and
reset your working tree and the index to match the tip of your
current branch.  Then you can make your fix as usual.</simpara>
<literallayout>... edit and test ...
$ git commit -a -m "blorpl: typofix"</literallayout>
<simpara>After that, you can go back to what you were working on with
<literal>git stash apply</literal>:</simpara>
<literallayout>$ git stash apply</literallayout>
</section>
</section>
<section id="ensuring-good-performance">
<title>Ensuring good performance</title>
<simpara>On large repositories, git depends on compression to keep the history
information from taking up too much space on disk or in memory.</simpara>
<simpara>This compression is not performed automatically.  Therefore you
should occasionally run <ulink url="git-gc.html">git-gc(1)</ulink>:</simpara>
<literallayout>$ git gc</literallayout>
<simpara>to recompress the archive.  This can be very time-consuming, so
you may prefer to run git-gc when you are not doing other work.</simpara>
</section>
<section id="ensuring-reliability">
<title>Ensuring reliability</title>
<section id="checking-for-corruption">
<title>Checking the repository for corruption</title>
<simpara>The <ulink url="git-fsck.html">git-fsck(1)</ulink> command runs a number of self-consistency checks
on the repository, and reports on any problems.  This may take some
time.  The most common warning by far is about "dangling" objects:</simpara>
<literallayout>$ git fsck
dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
dangling blob 218761f9d90712d37a9c5e36f406f92202db07eb
dangling commit bf093535a34a4d35731aa2bd90fe6b176302f14f
dangling commit 8e4bec7f2ddaa268bef999853c25755452100f8e
dangling tree d50bb86186bf27b681d25af89d3b5b68382e4085
dangling tree b24c2473f1fd3d91352a624795be026d64c8841f
...</literallayout>
<simpara>Dangling objects are not a problem.  At worst they may take up a little
extra disk space.  They can sometimes provide a last-resort method for
recovering lost work&#8212;see <xref linkend="dangling-objects"/> for details.</simpara>
</section>
<section id="recovering-lost-changes">
<title>Recovering lost changes</title>
<section id="reflogs">
<title>Reflogs</title>
<simpara>Say you modify a branch with <literal><ulink url="git-reset.html">git-reset(1)</ulink> --hard</literal>, and then
realize that the branch was the only reference you had to that point in
history.</simpara>
<simpara>Fortunately, git also keeps a log, called a "reflog", of all the
previous values of each branch.  So in this case you can still find the
old history using, for example,</simpara>
<literallayout>$ git log master@{1}</literallayout>
<simpara>This lists the commits reachable from the previous version of the
"master" branch head.  This syntax can be used with any git command
that accepts a commit, not just with git log.  Some other examples:</simpara>
<literallayout>$ git show master@{2}           # See where the branch pointed 2,
$ git show master@{3}           # 3, ... changes ago.
$ gitk master@{yesterday}       # See where it pointed yesterday,
$ gitk master@{"1 week ago"}    # ... or last week
$ git log --walk-reflogs master # show reflog entries for master</literallayout>
<simpara>A separate reflog is kept for the HEAD, so</simpara>
<literallayout>$ git show HEAD@{"1 week ago"}</literallayout>
<simpara>will show what HEAD pointed to one week ago, not what the current branch
pointed to one week ago.  This allows you to see the history of what
you&#8217;ve checked out.</simpara>
<simpara>The reflogs are kept by default for 30 days, after which they may be
pruned.  See <ulink url="git-reflog.html">git-reflog(1)</ulink> and <ulink url="git-gc.html">git-gc(1)</ulink> to learn
how to control this pruning, and see the "SPECIFYING REVISIONS"
section of <ulink url="git-rev-parse.html">git-rev-parse(1)</ulink> for details.</simpara>
<simpara>Note that the reflog history is very different from normal git history.
While normal history is shared by every repository that works on the
same project, the reflog history is not shared: it tells you only about
how the branches in your local repository have changed over time.</simpara>
</section>
<section id="dangling-object-recovery">
<title>Examining dangling objects</title>
<simpara>In some situations the reflog may not be able to save you.  For example,
suppose you delete a branch, then realize you need the history it
contained.  The reflog is also deleted; however, if you have not yet
pruned the repository, then you may still be able to find the lost
commits in the dangling objects that git-fsck reports.  See
<xref linkend="dangling-objects"/> for the details.</simpara>
<literallayout>$ git fsck
dangling commit 7281251ddd2a61e38657c827739c57015671a6b3
dangling commit 2706a059f258c6b245f298dc4ff2ccd30ec21a63
dangling commit 13472b7c4b80851a1bc551779171dcb03655e9b5
...</literallayout>
<simpara>You can examine
one of those dangling commits with, for example,</simpara>
<literallayout>$ gitk 7281251ddd --not --all</literallayout>
<simpara>which does what it sounds like: it says that you want to see the commit
history that is described by the dangling commit(s), but not the
history that is described by all your existing branches and tags.  Thus
you get exactly the history reachable from that commit that is lost.
(And notice that it might not be just one commit: we only report the
"tip of the line" as being dangling, but there might be a whole deep
and complex commit history that was dropped.)</simpara>
<simpara>If you decide you want the history back, you can always create a new
reference pointing to it, for example, a new branch:</simpara>
<literallayout>$ git branch recovered-branch 7281251ddd</literallayout>
<simpara>Other types of dangling objects (blobs and trees) are also possible, and
dangling objects can arise in other situations.</simpara>
</section>
</section>
</section>
</section>
<section id="sharing-development">
<title>Sharing development with others</title>
<section id="getting-updates-With-git-pull">
<title>Getting updates with git-pull</title>
<simpara>After you clone a repository and make a few changes of your own, you
may wish to check the original repository for updates and merge them
into your own work.</simpara>
<simpara>We have already seen <link linkend="Updating-a-repository-With-git-fetch">how to keep remote tracking branches up to date</link> with <ulink url="git-fetch.html">git-fetch(1)</ulink>,
and how to merge two branches.  So you can merge in changes from the
original repository&#8217;s master branch with:</simpara>
<literallayout>$ git fetch
$ git merge origin/master</literallayout>
<simpara>However, the <ulink url="git-pull.html">git-pull(1)</ulink> command provides a way to do this in
one step:</simpara>
<literallayout>$ git pull origin master</literallayout>
<simpara>In fact, if you have "master" checked out, then by default "git pull"
merges from the HEAD branch of the origin repository.  So often you can
accomplish the above with just a simple</simpara>
<literallayout>$ git pull</literallayout>
<simpara>More generally, a branch that is created from a remote branch will pull
by default from that branch.  See the descriptions of the
branch.&lt;name&gt;.remote and branch.&lt;name&gt;.merge options in
<ulink url="git-config.html">git-config(1)</ulink>, and the discussion of the <literal>--track</literal> option in
<ulink url="git-checkout.html">git-checkout(1)</ulink>, to learn how to control these defaults.</simpara>
<simpara>In addition to saving you keystrokes, "git pull" also helps you by
producing a default commit message documenting the branch and
repository that you pulled from.</simpara>
<simpara>(But note that no such commit will be created in the case of a
<link linkend="fast-forwards">fast forward</link>; instead, your branch will just be
updated to point to the latest commit from the upstream branch.)</simpara>
<simpara>The git-pull command can also be given "." as the "remote" repository,
in which case it just merges in a branch from the current repository; so
the commands</simpara>
<literallayout>$ git pull . branch
$ git merge branch</literallayout>
<simpara>are roughly equivalent.  The former is actually very commonly used.</simpara>
</section>
<section id="submitting-patches">
<title>Submitting patches to a project</title>
<simpara>If you just have a few changes, the simplest way to submit them may
just be to send them as patches in email:</simpara>
<simpara>First, use <ulink url="git-format-patch.html">git-format-patch(1)</ulink>; for example:</simpara>
<literallayout>$ git format-patch origin</literallayout>
<simpara>will produce a numbered series of files in the current directory, one
for each patch in the current branch but not in origin/HEAD.</simpara>
<simpara>You can then import these into your mail client and send them by
hand.  However, if you have a lot to send at once, you may prefer to
use the <ulink url="git-send-email.html">git-send-email(1)</ulink> script to automate the process.
Consult the mailing list for your project first to determine how they
prefer such patches be handled.</simpara>
</section>
<section id="importing-patches">
<title>Importing patches to a project</title>
<simpara>Git also provides a tool called <ulink url="git-am.html">git-am(1)</ulink> (am stands for
"apply mailbox"), for importing such an emailed series of patches.
Just save all of the patch-containing messages, in order, into a
single mailbox file, say "patches.mbox", then run</simpara>
<literallayout>$ git am -3 patches.mbox</literallayout>
<simpara>Git will apply each patch in order; if any conflicts are found, it
will stop, and you can fix the conflicts as described in
"<link linkend="resolving-a-merge">Resolving a merge</link>".  (The "-3" option tells
git to perform a merge; if you would prefer it just to abort and
leave your tree and index untouched, you may omit that option.)</simpara>
<simpara>Once the index is updated with the results of the conflict
resolution, instead of creating a new commit, just run</simpara>
<literallayout>$ git am --resolved</literallayout>
<simpara>and git will create the commit for you and continue applying the
remaining patches from the mailbox.</simpara>
<simpara>The final result will be a series of commits, one for each patch in
the original mailbox, with authorship and commit log message each
taken from the message containing each patch.</simpara>
</section>
<section id="public-repositories">
<title>Public git repositories</title>
<simpara>Another way to submit changes to a project is to tell the maintainer
of that project to pull the changes from your repository using
<ulink url="git-pull.html">git-pull(1)</ulink>.  In the section "<link linkend="getting-updates-With-git-pull">Getting updates with git-pull</link>" we described this as a way to get
updates from the "main" repository, but it works just as well in the
other direction.</simpara>
<simpara>If you and the maintainer both have accounts on the same machine, then
you can just pull changes from each other&#8217;s repositories directly;
commands that accept repository URLs as arguments will also accept a
local directory name:</simpara>
<literallayout>$ git clone /path/to/repository
$ git pull /path/to/other/repository</literallayout>
<simpara>or an ssh URL:</simpara>
<literallayout>$ git clone ssh://yourhost/~you/repository</literallayout>
<simpara>For projects with few developers, or for synchronizing a few private
repositories, this may be all you need.</simpara>
<simpara>However, the more common way to do this is to maintain a separate public
repository (usually on a different host) for others to pull changes
from.  This is usually more convenient, and allows you to cleanly
separate private work in progress from publicly visible work.</simpara>
<simpara>You will continue to do your day-to-day work in your personal
repository, but periodically "push" changes from your personal
repository into your public repository, allowing other developers to
pull from that repository.  So the flow of changes, in a situation
where there is one other developer with a public repository, looks
like this:</simpara>
<literallayout class="monospaced">                      you push
your personal repo ------------------&gt; your public repo
      ^                                     |
      |                                     |
      | you pull                            | they pull
      |                                     |
      |                                     |
      |               they push             V
their public repo &lt;------------------- their repo</literallayout>
<simpara>We explain how to do this in the following sections.</simpara>
<section id="setting-up-a-public-repository">
<title>Setting up a public repository</title>
<simpara>Assume your personal repository is in the directory ~/proj.  We
first create a new clone of the repository and tell git-daemon that it
is meant to be public:</simpara>
<literallayout>$ git clone --bare ~/proj proj.git
$ touch proj.git/git-daemon-export-ok</literallayout>
<simpara>The resulting directory proj.git contains a "bare" git repository&#8212;it is
just the contents of the ".git" directory, without any files checked out
around it.</simpara>
<simpara>Next, copy proj.git to the server where you plan to host the
public repository.  You can use scp, rsync, or whatever is most
convenient.</simpara>
</section>
<section id="exporting-via-git">
<title>Exporting a git repository via the git protocol</title>
<simpara>This is the preferred method.</simpara>
<simpara>If someone else administers the server, they should tell you what
directory to put the repository in, and what git:// URL it will appear
at.  You can then skip to the section
"<link linkend="pushing-changes-to-a-public-repository">Pushing changes to a public repository</link>", below.</simpara>
<simpara>Otherwise, all you need to do is start <ulink url="git-daemon.html">git-daemon(1)</ulink>; it will
listen on port 9418.  By default, it will allow access to any directory
that looks like a git directory and contains the magic file
git-daemon-export-ok.  Passing some directory paths as git-daemon
arguments will further restrict the exports to those paths.</simpara>
<simpara>You can also run git-daemon as an inetd service; see the
<ulink url="git-daemon.html">git-daemon(1)</ulink> man page for details.  (See especially the
examples section.)</simpara>
</section>
<section id="exporting-via-http">
<title>Exporting a git repository via http</title>
<simpara>The git protocol gives better performance and reliability, but on a
host with a web server set up, http exports may be simpler to set up.</simpara>
<simpara>All you need to do is place the newly created bare git repository in
a directory that is exported by the web server, and make some
adjustments to give web clients some extra information they need:</simpara>
<literallayout>$ mv proj.git /home/you/public_html/proj.git
$ cd proj.git
$ git --bare update-server-info
$ mv hooks/post-update.sample hooks/post-update</literallayout>
<simpara>(For an explanation of the last two lines, see
<ulink url="git-update-server-info.html">git-update-server-info(1)</ulink> and <ulink url="githooks.html">githooks(5)</ulink>.)</simpara>
<simpara>Advertise the URL of proj.git.  Anybody else should then be able to
clone or pull from that URL, for example with a command line like:</simpara>
<literallayout>$ git clone http://yourserver.com/~you/proj.git</literallayout>
<simpara>(See also
<ulink url="howto/setup-git-server-over-http.txt">setup-git-server-over-http</ulink>
for a slightly more sophisticated setup using WebDAV which also
allows pushing over http.)</simpara>
</section>
<section id="pushing-changes-to-a-public-repository">
<title>Pushing changes to a public repository</title>
<simpara>Note that the two techniques outlined above (exporting via
<link linkend="exporting-via-http">http</link> or <link linkend="exporting-via-git">git</link>) allow other
maintainers to fetch your latest changes, but they do not allow write
access, which you will need to update the public repository with the
latest changes created in your private repository.</simpara>
<simpara>The simplest way to do this is using <ulink url="git-push.html">git-push(1)</ulink> and ssh; to
update the remote branch named "master" with the latest state of your
branch named "master", run</simpara>
<literallayout>$ git push ssh://yourserver.com/~you/proj.git master:master</literallayout>
<simpara>or just</simpara>
<literallayout>$ git push ssh://yourserver.com/~you/proj.git master</literallayout>
<simpara>As with git-fetch, git-push will complain if this does not result in a
<link linkend="fast-forwards">fast forward</link>; see the following section for details on
handling this case.</simpara>
<simpara>Note that the target of a "push" is normally a
<link linkend="def_bare_repository">bare</link> repository.  You can also push to a
repository that has a checked-out working tree, but the working tree
will not be updated by the push.  This may lead to unexpected results if
the branch you push to is the currently checked-out branch!</simpara>
<simpara>As with git-fetch, you may also set up configuration options to
save typing; so, for example, after</simpara>
<literallayout>$ cat &gt;&gt;.git/config &lt;&lt;EOF
[remote "public-repo"]
        url = ssh://yourserver.com/~you/proj.git
EOF</literallayout>
<simpara>you should be able to perform the above push with just</simpara>
<literallayout>$ git push public-repo master</literallayout>
<simpara>See the explanations of the remote.&lt;name&gt;.url, branch.&lt;name&gt;.remote,
and remote.&lt;name&gt;.push options in <ulink url="git-config.html">git-config(1)</ulink> for
details.</simpara>
</section>
<section id="forcing-push">
<title>What to do when a push fails</title>
<simpara>If a push would not result in a <link linkend="fast-forwards">fast forward</link> of the
remote branch, then it will fail with an error like:</simpara>
<literallayout>error: remote 'refs/heads/master' is not an ancestor of
 local  'refs/heads/master'.
 Maybe you are not up-to-date and need to pull first?
error: failed to push to 'ssh://yourserver.com/~you/proj.git'</literallayout>
<simpara>This can happen, for example, if you:</simpara>
<itemizedlist>
<listitem>
<simpara>
use <literal>git-reset --hard</literal> to remove already-published commits, or
</simpara>
</listitem>
<listitem>
<simpara>
use <literal>git-commit --amend</literal> to replace already-published commits
          (as in <xref linkend="fixing-a-mistake-by-rewriting-history"/>), or
</simpara>
</listitem>
<listitem>
<simpara>
use <literal>git-rebase</literal> to rebase any already-published commits (as
          in <xref linkend="using-git-rebase"/>).
</simpara>
</listitem>
</itemizedlist>
<simpara>You may force git-push to perform the update anyway by preceding the
branch name with a plus sign:</simpara>
<literallayout>$ git push ssh://yourserver.com/~you/proj.git +master</literallayout>
<simpara>Normally whenever a branch head in a public repository is modified, it
is modified to point to a descendant of the commit that it pointed to
before.  By forcing a push in this situation, you break that convention.
(See <xref linkend="problems-With-rewriting-history"/>.)</simpara>
<simpara>Nevertheless, this is a common practice for people that need a simple
way to publish a work-in-progress patch series, and it is an acceptable
compromise as long as you warn other developers that this is how you
intend to manage the branch.</simpara>
<simpara>It&#8217;s also possible for a push to fail in this way when other people have
the right to push to the same repository.  In that case, the correct
solution is to retry the push after first updating your work: either by a
pull, or by a fetch followed by a rebase; see the
<link linkend="setting-up-a-shared-repository">next section</link> and
<ulink url="gitcvs-migration.html">gitcvs-migration(7)</ulink> for more.</simpara>
</section>
<section id="setting-up-a-shared-repository">
<title>Setting up a shared repository</title>
<simpara>Another way to collaborate is by using a model similar to that
commonly used in CVS, where several developers with special rights
all push to and pull from a single shared repository.  See
<ulink url="gitcvs-migration.html">gitcvs-migration(7)</ulink> for instructions on how to
set this up.</simpara>
<simpara>However, while there is nothing wrong with git&#8217;s support for shared
repositories, this mode of operation is not generally recommended,
simply because the mode of collaboration that git supports&#8212;by
exchanging patches and pulling from public repositories&#8212;has so many
advantages over the central shared repository:</simpara>
<itemizedlist>
<listitem>
<simpara>
Git&#8217;s ability to quickly import and merge patches allows a
          single maintainer to process incoming changes even at very
          high rates.  And when that becomes too much, git-pull provides
          an easy way for that maintainer to delegate this job to other
          maintainers while still allowing optional review of incoming
          changes.
</simpara>
</listitem>
<listitem>
<simpara>
Since every developer&#8217;s repository has the same complete copy
          of the project history, no repository is special, and it is
          trivial for another developer to take over maintenance of a
          project, either by mutual agreement, or because a maintainer
          becomes unresponsive or difficult to work with.
</simpara>
</listitem>
<listitem>
<simpara>
The lack of a central group of "committers" means there is
          less need for formal decisions about who is "in" and who is
          "out".
</simpara>
</listitem>
</itemizedlist>
</section>
<section id="setting-up-gitweb">
<title>Allowing web browsing of a repository</title>
<simpara>The gitweb cgi script provides users an easy way to browse your
project&#8217;s files and history without having to install git; see the file
gitweb/INSTALL in the git source tree for instructions on setting it up.</simpara>
</section>
</section>
<section id="sharing-development-examples">
<title>Examples</title>
<section id="maintaining-topic-branches">
<title>Maintaining topic branches for a Linux subsystem maintainer</title>
<simpara>This describes how Tony Luck uses git in his role as maintainer of the
IA64 architecture for the Linux kernel.</simpara>
<simpara>He uses two public branches:</simpara>
<itemizedlist>
<listitem>
<simpara>
A "test" tree into which patches are initially placed so that they
   can get some exposure when integrated with other ongoing development.
   This tree is available to Andrew for pulling into -mm whenever he
   wants.
</simpara>
</listitem>
<listitem>
<simpara>
A "release" tree into which tested patches are moved for final sanity
   checking, and as a vehicle to send them upstream to Linus (by sending
   him a "please pull" request.)
</simpara>
</listitem>
</itemizedlist>
<simpara>He also uses a set of temporary branches ("topic branches"), each
containing a logical grouping of patches.</simpara>
<simpara>To set this up, first create your work tree by cloning Linus&#8217;s public
tree:</simpara>
<literallayout>$ git clone git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git work
$ cd work</literallayout>
<simpara>Linus&#8217;s tree will be stored in the remote branch named origin/master,
and can be updated using <ulink url="git-fetch.html">git-fetch(1)</ulink>; you can track other
public trees using <ulink url="git-remote.html">git-remote(1)</ulink> to set up a "remote" and
<ulink url="git-fetch.html">git-fetch(1)</ulink> to keep them up-to-date; see
<xref linkend="repositories-and-branches"/>.</simpara>
<simpara>Now create the branches in which you are going to work; these start out
at the current tip of origin/master branch, and should be set up (using
the --track option to <ulink url="git-branch.html">git-branch(1)</ulink>) to merge changes in from
Linus by default.</simpara>
<literallayout>$ git branch --track test origin/master
$ git branch --track release origin/master</literallayout>
<simpara>These can be easily kept up to date using <ulink url="git-pull.html">git-pull(1)</ulink>.</simpara>
<literallayout>$ git checkout test &amp;&amp; git pull
$ git checkout release &amp;&amp; git pull</literallayout>
<simpara>Important note!  If you have any local changes in these branches, then
this merge will create a commit object in the history (with no local
changes git will simply do a "Fast forward" merge).  Many people dislike
the "noise" that this creates in the Linux history, so you should avoid
doing this capriciously in the "release" branch, as these noisy commits
will become part of the permanent history when you ask Linus to pull
from the release branch.</simpara>
<simpara>A few configuration variables (see <ulink url="git-config.html">git-config(1)</ulink>) can
make it easy to push both branches to your public tree.  (See
<xref linkend="setting-up-a-public-repository"/>.)</simpara>
<literallayout>$ cat &gt;&gt; .git/config &lt;&lt;EOF
[remote "mytree"]
        url =  master.kernel.org:/pub/scm/linux/kernel/git/aegl/linux-2.6.git
        push = release
        push = test
EOF</literallayout>
<simpara>Then you can push both the test and release trees using
<ulink url="git-push.html">git-push(1)</ulink>:</simpara>
<literallayout>$ git push mytree</literallayout>
<simpara>or push just one of the test and release branches using:</simpara>
<literallayout>$ git push mytree test</literallayout>
<simpara>or</simpara>
<literallayout>$ git push mytree release</literallayout>
<simpara>Now to apply some patches from the community.  Think of a short
snappy name for a branch to hold this patch (or related group of
patches), and create a new branch from the current tip of Linus&#8217;s
branch:</simpara>
<literallayout>$ git checkout -b speed-up-spinlocks origin</literallayout>
<simpara>Now you apply the patch(es), run some tests, and commit the change(s).  If
the patch is a multi-part series, then you should apply each as a separate
commit to this branch.</simpara>
<literallayout>$ ... patch ... test  ... commit [ ... patch ... test ... commit ]*</literallayout>
<simpara>When you are happy with the state of this change, you can pull it into the
"test" branch in preparation to make it public:</simpara>
<literallayout>$ git checkout test &amp;&amp; git pull . speed-up-spinlocks</literallayout>
<simpara>It is unlikely that you would have any conflicts here &#8230; but you might if you
spent a while on this step and had also pulled new versions from upstream.</simpara>
<simpara>Some time later when enough time has passed and testing done, you can pull the
same branch into the "release" tree ready to go upstream.  This is where you
see the value of keeping each patch (or patch series) in its own branch.  It
means that the patches can be moved into the "release" tree in any order.</simpara>
<literallayout>$ git checkout release &amp;&amp; git pull . speed-up-spinlocks</literallayout>
<simpara>After a while, you will have a number of branches, and despite the
well chosen names you picked for each of them, you may forget what
they are for, or what status they are in.  To get a reminder of what
changes are in a specific branch, use:</simpara>
<literallayout>$ git log linux..branchname | git shortlog</literallayout>
<simpara>To see whether it has already been merged into the test or release branches,
use:</simpara>
<literallayout>$ git log test..branchname</literallayout>
<simpara>or</simpara>
<literallayout>$ git log release..branchname</literallayout>
<simpara>(If this branch has not yet been merged, you will see some log entries.
If it has been merged, then there will be no output.)</simpara>
<simpara>Once a patch completes the great cycle (moving from test to release,
then pulled by Linus, and finally coming back into your local
"origin/master" branch), the branch for this change is no longer needed.
You detect this when the output from:</simpara>
<literallayout>$ git log origin..branchname</literallayout>
<simpara>is empty.  At this point the branch can be deleted:</simpara>
<literallayout>$ git branch -d branchname</literallayout>
<simpara>Some changes are so trivial that it is not necessary to create a separate
branch and then merge into each of the test and release branches.  For
these changes, just apply directly to the "release" branch, and then
merge that into the "test" branch.</simpara>
<simpara>To create diffstat and shortlog summaries of changes to include in a "please
pull" request to Linus you can use:</simpara>
<literallayout>$ git diff --stat origin..release</literallayout>
<simpara>and</simpara>
<literallayout>$ git log -p origin..release | git shortlog</literallayout>
<simpara>Here are some of the scripts that simplify all this even further.</simpara>
<literallayout>==== update script ====
# Update a branch in my GIT tree.  If the branch to be updated
# is origin, then pull from kernel.org.  Otherwise merge
# origin/master branch into test|release branch

case "$1" in
test|release)
        git checkout $1 &amp;&amp; git pull . origin
        ;;
origin)
        before=$(git rev-parse refs/remotes/origin/master)
        git fetch origin
        after=$(git rev-parse refs/remotes/origin/master)
        if [ $before != $after ]
        then
                git log $before..$after | git shortlog
        fi
        ;;
*)
        echo "Usage: $0 origin|test|release" 1&gt;&amp;2
        exit 1
        ;;
esac</literallayout>
<literallayout>==== merge script ====
# Merge a branch into either the test or release branch

pname=$0

usage()
{
        echo "Usage: $pname branch test|release" 1&gt;&amp;2
        exit 1
}

git show-ref -q --verify -- refs/heads/"$1" || {
        echo "Can't see branch &lt;$1&gt;" 1&gt;&amp;2
        usage
}

case "$2" in
test|release)
        if [ $(git log $2..$1 | wc -c) -eq 0 ]
        then
                echo $1 already merged into $2 1&gt;&amp;2
                exit 1
        fi
        git checkout $2 &amp;&amp; git pull . $1
        ;;
*)
        usage
        ;;
esac</literallayout>
<literallayout>==== status script ====
# report on status of my ia64 GIT tree

gb=$(tput setab 2)
rb=$(tput setab 1)
restore=$(tput setab 9)

if [ `git rev-list test..release | wc -c` -gt 0 ]
then
        echo $rb Warning: commits in release that are not in test $restore
        git log test..release
fi

for branch in `git show-ref --heads | sed 's|^.*/||'`
do
        if [ $branch = test -o $branch = release ]
        then
                continue
        fi

        echo -n $gb ======= $branch ====== $restore " "
        status=
        for ref in test release origin/master
        do
                if [ `git rev-list $ref..$branch | wc -c` -gt 0 ]
                then
                        status=$status${ref:0:1}
                fi
        done
        case $status in
        trl)
                echo $rb Need to pull into test $restore
                ;;
        rl)
                echo "In test"
                ;;
        l)
                echo "Waiting for linus"
                ;;
        "")
                echo $rb All done $restore
                ;;
        *)
                echo $rb "&lt;$status&gt;" $restore
                ;;
        esac
        git log origin/master..$branch | git shortlog
done</literallayout>
</section>
</section>
</section>
<section id="cleaning-up-history">
<title>Rewriting history and maintaining patch series</title>
<simpara>Normally commits are only added to a project, never taken away or
replaced.  Git is designed with this assumption, and violating it will
cause git&#8217;s merge machinery (for example) to do the wrong thing.</simpara>
<simpara>However, there is a situation in which it can be useful to violate this
assumption.</simpara>
<section id="patch-series">
<title>Creating the perfect patch series</title>
<simpara>Suppose you are a contributor to a large project, and you want to add a
complicated feature, and to present it to the other developers in a way
that makes it easy for them to read your changes, verify that they are
correct, and understand why you made each change.</simpara>
<simpara>If you present all of your changes as a single patch (or commit), they
may find that it is too much to digest all at once.</simpara>
<simpara>If you present them with the entire history of your work, complete with
mistakes, corrections, and dead ends, they may be overwhelmed.</simpara>
<simpara>So the ideal is usually to produce a series of patches such that:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>
Each patch can be applied in order.
</simpara>
</listitem>
<listitem>
<simpara>
Each patch includes a single logical change, together with a
           message explaining the change.
</simpara>
</listitem>
<listitem>
<simpara>
No patch introduces a regression: after applying any initial
           part of the series, the resulting project still compiles and
           works, and has no bugs that it didn&#8217;t have before.
</simpara>
</listitem>
<listitem>
<simpara>
The complete series produces the same end result as your own
           (probably much messier!) development process did.
</simpara>
</listitem>
</orderedlist>
<simpara>We will introduce some tools that can help you do this, explain how to
use them, and then explain some of the problems that can arise because
you are rewriting history.</simpara>
</section>
<section id="using-git-rebase">
<title>Keeping a patch series up to date using git-rebase</title>
<simpara>Suppose that you create a branch "mywork" on a remote-tracking branch
"origin", and create some commits on top of it:</simpara>
<literallayout>$ git checkout -b mywork origin
$ vi file.txt
$ git commit
$ vi otherfile.txt
$ git commit
...</literallayout>
<simpara>You have performed no merges into mywork, so it is just a simple linear
sequence of patches on top of "origin":</simpara>
<literallayout class="monospaced"> o--o--o &lt;-- origin
        \
         o--o--o &lt;-- mywork</literallayout>
<simpara>Some more interesting work has been done in the upstream project, and
"origin" has advanced:</simpara>
<literallayout class="monospaced"> o--o--O--o--o--o &lt;-- origin
        \
         a--b--c &lt;-- mywork</literallayout>
<simpara>At this point, you could use "pull" to merge your changes back in;
the result would create a new merge commit, like this:</simpara>
<literallayout class="monospaced"> o--o--O--o--o--o &lt;-- origin
        \        \
         a--b--c--m &lt;-- mywork</literallayout>
<simpara>However, if you prefer to keep the history in mywork a simple series of
commits without any merges, you may instead choose to use
<ulink url="git-rebase.html">git-rebase(1)</ulink>:</simpara>
<literallayout>$ git checkout mywork
$ git rebase origin</literallayout>
<simpara>This will remove each of your commits from mywork, temporarily saving
them as patches (in a directory named ".git/rebase-apply"), update mywork to
point at the latest version of origin, then apply each of the saved
patches to the new mywork.  The result will look like:</simpara>
<literallayout class="monospaced"> o--o--O--o--o--o &lt;-- origin
                 \
                  a'--b'--c' &lt;-- mywork</literallayout>
<simpara>In the process, it may discover conflicts.  In that case it will stop
and allow you to fix the conflicts; after fixing conflicts, use "git-add"
to update the index with those contents, and then, instead of
running git-commit, just run</simpara>
<literallayout>$ git rebase --continue</literallayout>
<simpara>and git will continue applying the rest of the patches.</simpara>
<simpara>At any point you may use the <literal>--abort</literal> option to abort this process and
return mywork to the state it had before you started the rebase:</simpara>
<literallayout>$ git rebase --abort</literallayout>
</section>
<section id="rewriting-one-commit">
<title>Rewriting a single commit</title>
<simpara>We saw in <xref linkend="fixing-a-mistake-by-rewriting-history"/> that you can replace the
most recent commit using</simpara>
<literallayout>$ git commit --amend</literallayout>
<simpara>which will replace the old commit by a new commit incorporating your
changes, giving you a chance to edit the old commit message first.</simpara>
<simpara>You can also use a combination of this and <ulink url="git-rebase.html">git-rebase(1)</ulink> to
replace a commit further back in your history and recreate the
intervening changes on top of it.  First, tag the problematic commit
with</simpara>
<literallayout>$ git tag bad mywork~5</literallayout>
<simpara>(Either gitk or git-log may be useful for finding the commit.)</simpara>
<simpara>Then check out that commit, edit it, and rebase the rest of the series
on top of it (note that we could check out the commit on a temporary
branch, but instead we&#8217;re using a <link linkend="detached-head">detached head</link>):</simpara>
<literallayout>$ git checkout bad
$ # make changes here and update the index
$ git commit --amend
$ git rebase --onto HEAD bad mywork</literallayout>
<simpara>When you&#8217;re done, you&#8217;ll be left with mywork checked out, with the top
patches on mywork reapplied on top of your modified commit.  You can
then clean up with</simpara>
<literallayout>$ git tag -d bad</literallayout>
<simpara>Note that the immutable nature of git history means that you haven&#8217;t really
"modified" existing commits; instead, you have replaced the old commits with
new commits having new object names.</simpara>
</section>
<section id="reordering-patch-series">
<title>Reordering or selecting from a patch series</title>
<simpara>Given one existing commit, the <ulink url="git-cherry-pick.html">git-cherry-pick(1)</ulink> command
allows you to apply the change introduced by that commit and create a
new commit that records it.  So, for example, if "mywork" points to a
series of patches on top of "origin", you might do something like:</simpara>
<literallayout>$ git checkout -b mywork-new origin
$ gitk origin..mywork &amp;</literallayout>
<simpara>and browse through the list of patches in the mywork branch using gitk,
applying them (possibly in a different order) to mywork-new using
cherry-pick, and possibly modifying them as you go using <literal>commit --amend</literal>.
The <ulink url="git-gui.html">git-gui(1)</ulink> command may also help as it allows you to
individually select diff hunks for inclusion in the index (by
right-clicking on the diff hunk and choosing "Stage Hunk for Commit").</simpara>
<simpara>Another technique is to use git-format-patch to create a series of
patches, then reset the state to before the patches:</simpara>
<literallayout>$ git format-patch origin
$ git reset --hard origin</literallayout>
<simpara>Then modify, reorder, or eliminate patches as preferred before applying
them again with <ulink url="git-am.html">git-am(1)</ulink>.</simpara>
</section>
<section id="patch-series-tools">
<title>Other tools</title>
<simpara>There are numerous other tools, such as StGIT, which exist for the
purpose of maintaining a patch series.  These are outside of the scope of
this manual.</simpara>
</section>
<section id="problems-With-rewriting-history">
<title>Problems with rewriting history</title>
<simpara>The primary problem with rewriting the history of a branch has to do
with merging.  Suppose somebody fetches your branch and merges it into
their branch, with a result something like this:</simpara>
<literallayout class="monospaced"> o--o--O--o--o--o &lt;-- origin
        \        \
         t--t--t--m &lt;-- their branch:</literallayout>
<simpara>Then suppose you modify the last three commits:</simpara>
<literallayout class="monospaced">         o--o--o &lt;-- new head of origin
        /
 o--o--O--o--o--o &lt;-- old head of origin</literallayout>
<simpara>If we examined all this history together in one repository, it will
look like:</simpara>
<literallayout class="monospaced">         o--o--o &lt;-- new head of origin
        /
 o--o--O--o--o--o &lt;-- old head of origin
        \        \
         t--t--t--m &lt;-- their branch:</literallayout>
<simpara>Git has no way of knowing that the new head is an updated version of
the old head; it treats this situation exactly the same as it would if
two developers had independently done the work on the old and new heads
in parallel.  At this point, if someone attempts to merge the new head
in to their branch, git will attempt to merge together the two (old and
new) lines of development, instead of trying to replace the old by the
new.  The results are likely to be unexpected.</simpara>
<simpara>You may still choose to publish branches whose history is rewritten,
and it may be useful for others to be able to fetch those branches in
order to examine or test them, but they should not attempt to pull such
branches into their own work.</simpara>
<simpara>For true distributed development that supports proper merging,
published branches should never be rewritten.</simpara>
</section>
<section id="bisect-merges">
<title>Why bisecting merge commits can be harder than bisecting linear history</title>
<simpara>The <ulink url="git-bisect.html">git-bisect(1)</ulink> command correctly handles history that
includes merge commits.  However, when the commit that it finds is a
merge commit, the user may need to work harder than usual to figure out
why that commit introduced a problem.</simpara>
<simpara>Imagine this history:</simpara>
<literallayout class="monospaced">      ---Z---o---X---...---o---A---C---D
          \                       /
           o---o---Y---...---o---B</literallayout>
<simpara>Suppose that on the upper line of development, the meaning of one
of the functions that exists at Z is changed at commit X.  The
commits from Z leading to A change both the function&#8217;s
implementation and all calling sites that exist at Z, as well
as new calling sites they add, to be consistent.  There is no
bug at A.</simpara>
<simpara>Suppose that in the meantime on the lower line of development somebody
adds a new calling site for that function at commit Y.  The
commits from Z leading to B all assume the old semantics of that
function and the callers and the callee are consistent with each
other.  There is no bug at B, either.</simpara>
<simpara>Suppose further that the two development lines merge cleanly at C,
so no conflict resolution is required.</simpara>
<simpara>Nevertheless, the code at C is broken, because the callers added
on the lower line of development have not been converted to the new
semantics introduced on the upper line of development.  So if all
you know is that D is bad, that Z is good, and that
<ulink url="git-bisect.html">git-bisect(1)</ulink> identifies C as the culprit, how will you
figure out that the problem is due to this change in semantics?</simpara>
<simpara>When the result of a git-bisect is a non-merge commit, you should
normally be able to discover the problem by examining just that commit.
Developers can make this easy by breaking their changes into small
self-contained commits.  That won&#8217;t help in the case above, however,
because the problem isn&#8217;t obvious from examination of any single
commit; instead, a global view of the development is required.  To
make matters worse, the change in semantics in the problematic
function may be just one small part of the changes in the upper
line of development.</simpara>
<simpara>On the other hand, if instead of merging at C you had rebased the
history between Z to B on top of A, you would have gotten this
linear history:</simpara>
<literallayout class="monospaced">    ---Z---o---X--...---o---A---o---o---Y*--...---o---B*--D*</literallayout>
<simpara>Bisecting between Z and D* would hit a single culprit commit Y*,
and understanding why Y* was broken would probably be easier.</simpara>
<simpara>Partly for this reason, many experienced git users, even when
working on an otherwise merge-heavy project, keep the history
linear by rebasing against the latest upstream version before
publishing.</simpara>
</section>
</section>
<section id="advanced-branch-management">
<title>Advanced branch management</title>
<section id="fetching-individual-branches">
<title>Fetching individual branches</title>
<simpara>Instead of using <ulink url="git-remote.html">git-remote(1)</ulink>, you can also choose just
to update one branch at a time, and to store it locally under an
arbitrary name:</simpara>
<literallayout>$ git fetch origin todo:my-todo-work</literallayout>
<simpara>The first argument, "origin", just tells git to fetch from the
repository you originally cloned from.  The second argument tells git
to fetch the branch named "todo" from the remote repository, and to
store it locally under the name refs/heads/my-todo-work.</simpara>
<simpara>You can also fetch branches from other repositories; so</simpara>
<literallayout>$ git fetch git://example.com/proj.git master:example-master</literallayout>
<simpara>will create a new branch named "example-master" and store in it the
branch named "master" from the repository at the given URL.  If you
already have a branch named example-master, it will attempt to
<link linkend="fast-forwards">fast-forward</link> to the commit given by example.com&#8217;s
master branch.  In more detail:</simpara>
</section>
<section id="fetch-fast-forwards">
<title>git fetch and fast-forwards</title>
<simpara>In the previous example, when updating an existing branch, "git-fetch"
checks to make sure that the most recent commit on the remote
branch is a descendant of the most recent commit on your copy of the
branch before updating your copy of the branch to point at the new
commit.  Git calls this process a <link linkend="fast-forwards">fast forward</link>.</simpara>
<simpara>A fast forward looks something like this:</simpara>
<literallayout class="monospaced"> o--o--o--o &lt;-- old head of the branch
           \
            o--o--o &lt;-- new head of the branch</literallayout>
<simpara>In some cases it is possible that the new head will <emphasis role="strong">not</emphasis> actually be
a descendant of the old head.  For example, the developer may have
realized she made a serious mistake, and decided to backtrack,
resulting in a situation like:</simpara>
<literallayout class="monospaced"> o--o--o--o--a--b &lt;-- old head of the branch
           \
            o--o--o &lt;-- new head of the branch</literallayout>
<simpara>In this case, "git-fetch" will fail, and print out a warning.</simpara>
<simpara>In that case, you can still force git to update to the new head, as
described in the following section.  However, note that in the
situation above this may mean losing the commits labeled "a" and "b",
unless you&#8217;ve already created a reference of your own pointing to
them.</simpara>
</section>
<section id="forcing-fetch">
<title>Forcing git-fetch to do non-fast-forward updates</title>
<simpara>If git fetch fails because the new head of a branch is not a
descendant of the old head, you may force the update with:</simpara>
<literallayout>$ git fetch git://example.com/proj.git +master:refs/remotes/example/master</literallayout>
<simpara>Note the addition of the "+" sign.  Alternatively, you can use the "-f"
flag to force updates of all the fetched branches, as in:</simpara>
<literallayout>$ git fetch -f origin</literallayout>
<simpara>Be aware that commits that the old version of example/master pointed at
may be lost, as we saw in the previous section.</simpara>
</section>
<section id="remote-branch-configuration">
<title>Configuring remote branches</title>
<simpara>We saw above that "origin" is just a shortcut to refer to the
repository that you originally cloned from.  This information is
stored in git configuration variables, which you can see using
<ulink url="git-config.html">git-config(1)</ulink>:</simpara>
<literallayout>$ git config -l
core.repositoryformatversion=0
core.filemode=true
core.logallrefupdates=true
remote.origin.url=git://git.kernel.org/pub/scm/git/git.git
remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*
branch.master.remote=origin
branch.master.merge=refs/heads/master</literallayout>
<simpara>If there are other repositories that you also use frequently, you can
create similar configuration options to save typing; for example,
after</simpara>
<literallayout>$ git config remote.example.url git://example.com/proj.git</literallayout>
<simpara>then the following two commands will do the same thing:</simpara>
<literallayout>$ git fetch git://example.com/proj.git master:refs/remotes/example/master
$ git fetch example master:refs/remotes/example/master</literallayout>
<simpara>Even better, if you add one more option:</simpara>
<literallayout>$ git config remote.example.fetch master:refs/remotes/example/master</literallayout>
<simpara>then the following commands will all do the same thing:</simpara>
<literallayout>$ git fetch git://example.com/proj.git master:refs/remotes/example/master
$ git fetch example master:refs/remotes/example/master
$ git fetch example</literallayout>
<simpara>You can also add a "+" to force the update each time:</simpara>
<literallayout>$ git config remote.example.fetch +master:ref/remotes/example/master</literallayout>
<simpara>Don&#8217;t do this unless you&#8217;re sure you won&#8217;t mind "git fetch" possibly
throwing away commits on <emphasis>example/master</emphasis>.</simpara>
<simpara>Also note that all of the above configuration can be performed by
directly editing the file .git/config instead of using
<ulink url="git-config.html">git-config(1)</ulink>.</simpara>
<simpara>See <ulink url="git-config.html">git-config(1)</ulink> for more details on the configuration
options mentioned above.</simpara>
</section>
</section>
<section id="git-concepts">
<title>Git concepts</title>
<simpara>Git is built on a small number of simple but powerful ideas.  While it
is possible to get things done without understanding them, you will find
git much more intuitive if you do.</simpara>
<simpara>We start with the most important, the  <link linkend="def_object_database">object database</link> and the <link linkend="def_index">index</link>.</simpara>
<section id="the-object-database">
<title>The Object Database</title>
<simpara>We already saw in <xref linkend="understanding-commits"/> that all commits are stored
under a 40-digit "object name".  In fact, all the information needed to
represent the history of a project is stored in objects with such names.
In each case the name is calculated by taking the SHA1 hash of the
contents of the object.  The SHA1 hash is a cryptographic hash function.
What that means to us is that it is impossible to find two different
objects with the same name.  This has a number of advantages; among
others:</simpara>
<itemizedlist>
<listitem>
<simpara>
Git can quickly determine whether two objects are identical or not,
  just by comparing names.
</simpara>
</listitem>
<listitem>
<simpara>
Since object names are computed the same way in every repository, the
  same content stored in two repositories will always be stored under
  the same name.
</simpara>
</listitem>
<listitem>
<simpara>
Git can detect errors when it reads an object, by checking that the
  object&#8217;s name is still the SHA1 hash of its contents.
</simpara>
</listitem>
</itemizedlist>
<simpara>(See <xref linkend="object-details"/> for the details of the object formatting and
SHA1 calculation.)</simpara>
<simpara>There are four different types of objects: "blob", "tree", "commit", and
"tag".</simpara>
<itemizedlist>
<listitem>
<simpara>
A <link linkend="def_blob_object">"blob" object</link> is used to store file data.
</simpara>
</listitem>
<listitem>
<simpara>
A <link linkend="def_tree_object">"tree" object</link> ties one or more
  "blob" objects into a directory structure. In addition, a tree object
  can refer to other tree objects, thus creating a directory hierarchy.
</simpara>
</listitem>
<listitem>
<simpara>
A <link linkend="def_commit_object">"commit" object</link> ties such directory hierarchies
  together into a <link linkend="def_DAG">directed acyclic graph</link> of revisions&#8212;each
  commit contains the object name of exactly one tree designating the
  directory hierarchy at the time of the commit. In addition, a commit
  refers to "parent" commit objects that describe the history of how we
  arrived at that directory hierarchy.
</simpara>
</listitem>
<listitem>
<simpara>
A <link linkend="def_tag_object">"tag" object</link> symbolically identifies and can be
  used to sign other objects. It contains the object name and type of
  another object, a symbolic name (of course!) and, optionally, a
  signature.
</simpara>
</listitem>
</itemizedlist>
<simpara>The object types in some more detail:</simpara>
<section id="commit-object">
<title>Commit Object</title>
<simpara>The "commit" object links a physical state of a tree with a description
of how we got there and why.  Use the --pretty=raw option to
<ulink url="git-show.html">git-show(1)</ulink> or <ulink url="git-log.html">git-log(1)</ulink> to examine your favorite
commit:</simpara>
<literallayout>$ git show -s --pretty=raw 2be7fcb476
commit 2be7fcb4764f2dbcee52635b91fedb1b3dcf7ab4
tree fb3a8bdd0ceddd019615af4d57a53f43d8cee2bf
parent 257a84d9d02e90447b149af58b271c19405edb6a
author Dave Watson &lt;dwatson@mimvista.com&gt; 1187576872 -0400
committer Junio C Hamano &lt;gitster@pobox.com&gt; 1187591163 -0700

    Fix misspelling of 'suppress' in docs

    Signed-off-by: Junio C Hamano &lt;gitster@pobox.com&gt;</literallayout>
<simpara>As you can see, a commit is defined by:</simpara>
<itemizedlist>
<listitem>
<simpara>
a tree: The SHA1 name of a tree object (as defined below), representing
  the contents of a directory at a certain point in time.
</simpara>
</listitem>
<listitem>
<simpara>
parent(s): The SHA1 name of some number of commits which represent the
  immediately previous step(s) in the history of the project.  The
  example above has one parent; merge commits may have more than
  one.  A commit with no parents is called a "root" commit, and
  represents the initial revision of a project.  Each project must have
  at least one root.  A project can also have multiple roots, though
  that isn&#8217;t common (or necessarily a good idea).
</simpara>
</listitem>
<listitem>
<simpara>
an author: The name of the person responsible for this change, together
  with its date.
</simpara>
</listitem>
<listitem>
<simpara>
a committer: The name of the person who actually created the commit,
  with the date it was done.  This may be different from the author, for
  example, if the author was someone who wrote a patch and emailed it
  to the person who used it to create the commit.
</simpara>
</listitem>
<listitem>
<simpara>
a comment describing this commit.
</simpara>
</listitem>
</itemizedlist>
<simpara>Note that a commit does not itself contain any information about what
actually changed; all changes are calculated by comparing the contents
of the tree referred to by this commit with the trees associated with
its parents.  In particular, git does not attempt to record file renames
explicitly, though it can identify cases where the existence of the same
file data at changing paths suggests a rename.  (See, for example, the
-M option to <ulink url="git-diff.html">git-diff(1)</ulink>).</simpara>
<simpara>A commit is usually created by <ulink url="git-commit.html">git-commit(1)</ulink>, which creates a
commit whose parent is normally the current HEAD, and whose tree is
taken from the content currently stored in the index.</simpara>
</section>
<section id="tree-object">
<title>Tree Object</title>
<simpara>The ever-versatile <ulink url="git-show.html">git-show(1)</ulink> command can also be used to
examine tree objects, but <ulink url="git-ls-tree.html">git-ls-tree(1)</ulink> will give you more
details:</simpara>
<literallayout>$ git ls-tree fb3a8bdd0ce
100644 blob 63c918c667fa005ff12ad89437f2fdc80926e21c    .gitignore
100644 blob 5529b198e8d14decbe4ad99db3f7fb632de0439d    .mailmap
100644 blob 6ff87c4664981e4397625791c8ea3bbb5f2279a3    COPYING
040000 tree 2fb783e477100ce076f6bf57e4a6f026013dc745    Documentation
100755 blob 3c0032cec592a765692234f1cba47dfdcc3a9200    GIT-VERSION-GEN
100644 blob 289b046a443c0647624607d471289b2c7dcd470b    INSTALL
100644 blob 4eb463797adc693dc168b926b6932ff53f17d0b1    Makefile
100644 blob 548142c327a6790ff8821d67c2ee1eff7a656b52    README
...</literallayout>
<simpara>As you can see, a tree object contains a list of entries, each with a
mode, object type, SHA1 name, and name, sorted by name.  It represents
the contents of a single directory tree.</simpara>
<simpara>The object type may be a blob, representing the contents of a file, or
another tree, representing the contents of a subdirectory.  Since trees
and blobs, like all other objects, are named by the SHA1 hash of their
contents, two trees have the same SHA1 name if and only if their
contents (including, recursively, the contents of all subdirectories)
are identical.  This allows git to quickly determine the differences
between two related tree objects, since it can ignore any entries with
identical object names.</simpara>
<simpara>(Note: in the presence of submodules, trees may also have commits as
entries.  See <xref linkend="submodules"/> for documentation.)</simpara>
<simpara>Note that the files all have mode 644 or 755: git actually only pays
attention to the executable bit.</simpara>
</section>
<section id="blob-object">
<title>Blob Object</title>
<simpara>You can use <ulink url="git-show.html">git-show(1)</ulink> to examine the contents of a blob; take,
for example, the blob in the entry for "COPYING" from the tree above:</simpara>
<literallayout>$ git show 6ff87c4664

 Note that the only valid version of the GPL as far as this project
 is concerned is _this_ particular version of the license (ie v2, not
 v2.2 or v3.x or whatever), unless explicitly otherwise stated.
...</literallayout>
<simpara>A "blob" object is nothing but a binary blob of data.  It doesn&#8217;t refer
to anything else or have attributes of any kind.</simpara>
<simpara>Since the blob is entirely defined by its data, if two files in a
directory tree (or in multiple different versions of the repository)
have the same contents, they will share the same blob object. The object
is totally independent of its location in the directory tree, and
renaming a file does not change the object that file is associated with.</simpara>
<simpara>Note that any tree or blob object can be examined using
<ulink url="git-show.html">git-show(1)</ulink> with the &lt;revision&gt;:&lt;path&gt; syntax.  This can
sometimes be useful for browsing the contents of a tree that is not
currently checked out.</simpara>
</section>
<section id="trust">
<title>Trust</title>
<simpara>If you receive the SHA1 name of a blob from one source, and its contents
from another (possibly untrusted) source, you can still trust that those
contents are correct as long as the SHA1 name agrees.  This is because
the SHA1 is designed so that it is infeasible to find different contents
that produce the same hash.</simpara>
<simpara>Similarly, you need only trust the SHA1 name of a top-level tree object
to trust the contents of the entire directory that it refers to, and if
you receive the SHA1 name of a commit from a trusted source, then you
can easily verify the entire history of commits reachable through
parents of that commit, and all of those contents of the trees referred
to by those commits.</simpara>
<simpara>So to introduce some real trust in the system, the only thing you need
to do is to digitally sign just <emphasis>one</emphasis> special note, which includes the
name of a top-level commit.  Your digital signature shows others
that you trust that commit, and the immutability of the history of
commits tells others that they can trust the whole history.</simpara>
<simpara>In other words, you can easily validate a whole archive by just
sending out a single email that tells the people the name (SHA1 hash)
of the top commit, and digitally sign that email using something
like GPG/PGP.</simpara>
<simpara>To assist in this, git also provides the tag object&#8230;</simpara>
</section>
<section id="tag-object">
<title>Tag Object</title>
<simpara>A tag object contains an object, object type, tag name, the name of the
person ("tagger") who created the tag, and a message, which may contain
a signature, as can be seen using <ulink url="git-cat-file.html">git-cat-file(1)</ulink>:</simpara>
<literallayout>$ git cat-file tag v1.5.0
object 437b1b20df4b356c9342dac8d38849f24ef44f27
type commit
tag v1.5.0
tagger Junio C Hamano &lt;junkio@cox.net&gt; 1171411200 +0000

GIT 1.5.0
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.6 (GNU/Linux)

iD8DBQBF0lGqwMbZpPMRm5oRAuRiAJ9ohBLd7s2kqjkKlq1qqC57SbnmzQCdG4ui
nLE/L9aUXdWeTFPron96DLA=
=2E+0
-----END PGP SIGNATURE-----</literallayout>
<simpara>See the <ulink url="git-tag.html">git-tag(1)</ulink> command to learn how to create and verify tag
objects.  (Note that <ulink url="git-tag.html">git-tag(1)</ulink> can also be used to create
"lightweight tags", which are not tag objects at all, but just simple
references whose names begin with "refs/tags/").</simpara>
</section>
<section id="pack-files">
<title>How git stores objects efficiently: pack files</title>
<simpara>Newly created objects are initially created in a file named after the
object&#8217;s SHA1 hash (stored in .git/objects).</simpara>
<simpara>Unfortunately this system becomes inefficient once a project has a
lot of objects.  Try this on an old project:</simpara>
<literallayout>$ git count-objects
6930 objects, 47620 kilobytes</literallayout>
<simpara>The first number is the number of objects which are kept in
individual files.  The second is the amount of space taken up by
those "loose" objects.</simpara>
<simpara>You can save space and make git faster by moving these loose objects in
to a "pack file", which stores a group of objects in an efficient
compressed format; the details of how pack files are formatted can be
found in <ulink url="technical/pack-format.txt">technical/pack-format.txt</ulink>.</simpara>
<simpara>To put the loose objects into a pack, just run git repack:</simpara>
<literallayout>$ git repack
Generating pack...
Done counting 6020 objects.
Deltifying 6020 objects.
 100% (6020/6020) done
Writing 6020 objects.
 100% (6020/6020) done
Total 6020, written 6020 (delta 4070), reused 0 (delta 0)
Pack pack-3e54ad29d5b2e05838c75df582c65257b8d08e1c created.</literallayout>
<simpara>You can then run</simpara>
<literallayout>$ git prune</literallayout>
<simpara>to remove any of the "loose" objects that are now contained in the
pack.  This will also remove any unreferenced objects (which may be
created when, for example, you use "git-reset" to remove a commit).
You can verify that the loose objects are gone by looking at the
.git/objects directory or by running</simpara>
<literallayout>$ git count-objects
0 objects, 0 kilobytes</literallayout>
<simpara>Although the object files are gone, any commands that refer to those
objects will work exactly as they did before.</simpara>
<simpara>The <ulink url="git-gc.html">git-gc(1)</ulink> command performs packing, pruning, and more for
you, so is normally the only high-level command you need.</simpara>
</section>
<section id="dangling-objects">
<title>Dangling objects</title>
<simpara>The <ulink url="git-fsck.html">git-fsck(1)</ulink> command will sometimes complain about dangling
objects.  They are not a problem.</simpara>
<simpara>The most common cause of dangling objects is that you&#8217;ve rebased a
branch, or you have pulled from somebody else who rebased a branch&#8212;see
<xref linkend="cleaning-up-history"/>.  In that case, the old head of the original
branch still exists, as does everything it pointed to. The branch
pointer itself just doesn&#8217;t, since you replaced it with another one.</simpara>
<simpara>There are also other situations that cause dangling objects. For
example, a "dangling blob" may arise because you did a "git-add" of a
file, but then, before you actually committed it and made it part of the
bigger picture, you changed something else in that file and committed
that <emphasis role="strong">updated</emphasis> thing&#8212;the old state that you added originally ends up
not being pointed to by any commit or tree, so it&#8217;s now a dangling blob
object.</simpara>
<simpara>Similarly, when the "recursive" merge strategy runs, and finds that
there are criss-cross merges and thus more than one merge base (which is
fairly unusual, but it does happen), it will generate one temporary
midway tree (or possibly even more, if you had lots of criss-crossing
merges and more than two merge bases) as a temporary internal merge
base, and again, those are real objects, but the end result will not end
up pointing to them, so they end up "dangling" in your repository.</simpara>
<simpara>Generally, dangling objects aren&#8217;t anything to worry about. They can
even be very useful: if you screw something up, the dangling objects can
be how you recover your old tree (say, you did a rebase, and realized
that you really didn&#8217;t want to&#8212;you can look at what dangling objects
you have, and decide to reset your head to some old dangling state).</simpara>
<simpara>For commits, you can just use:</simpara>
<literallayout>$ gitk &lt;dangling-commit-sha-goes-here&gt; --not --all</literallayout>
<simpara>This asks for all the history reachable from the given commit but not
from any branch, tag, or other reference.  If you decide it&#8217;s something
you want, you can always create a new reference to it, e.g.,</simpara>
<literallayout>$ git branch recovered-branch &lt;dangling-commit-sha-goes-here&gt;</literallayout>
<simpara>For blobs and trees, you can&#8217;t do the same, but you can still examine
them.  You can just do</simpara>
<literallayout>$ git show &lt;dangling-blob/tree-sha-goes-here&gt;</literallayout>
<simpara>to show what the contents of the blob were (or, for a tree, basically
what the "ls" for that directory was), and that may give you some idea
of what the operation was that left that dangling object.</simpara>
<simpara>Usually, dangling blobs and trees aren&#8217;t very interesting. They&#8217;re
almost always the result of either being a half-way mergebase (the blob
will often even have the conflict markers from a merge in it, if you
have had conflicting merges that you fixed up by hand), or simply
because you interrupted a "git-fetch" with ^C or something like that,
leaving <emphasis>some</emphasis> of the new objects in the object database, but just
dangling and useless.</simpara>
<simpara>Anyway, once you are sure that you&#8217;re not interested in any dangling
state, you can just prune all unreachable objects:</simpara>
<literallayout>$ git prune</literallayout>
<simpara>and they&#8217;ll be gone. But you should only run "git prune" on a quiescent
repository&#8212;it&#8217;s kind of like doing a filesystem fsck recovery: you
don&#8217;t want to do that while the filesystem is mounted.</simpara>
<simpara>(The same is true of "git-fsck" itself, btw, but since
git-fsck never actually <emphasis role="strong">changes</emphasis> the repository, it just reports
on what it found, git-fsck itself is never "dangerous" to run.
Running it while somebody is actually changing the repository can cause
confusing and scary messages, but it won&#8217;t actually do anything bad. In
contrast, running "git prune" while somebody is actively changing the
repository is a <emphasis role="strong">BAD</emphasis> idea).</simpara>
</section>
<section id="recovering-from-repository-corruption">
<title>Recovering from repository corruption</title>
<simpara>By design, git treats data trusted to it with caution.  However, even in
the absence of bugs in git itself, it is still possible that hardware or
operating system errors could corrupt data.</simpara>
<simpara>The first defense against such problems is backups.  You can back up a
git directory using clone, or just using cp, tar, or any other backup
mechanism.</simpara>
<simpara>As a last resort, you can search for the corrupted objects and attempt
to replace them by hand.  Back up your repository before attempting this
in case you corrupt things even more in the process.</simpara>
<simpara>We&#8217;ll assume that the problem is a single missing or corrupted blob,
which is sometimes a solvable problem.  (Recovering missing trees and
especially commits is <emphasis role="strong">much</emphasis> harder).</simpara>
<simpara>Before starting, verify that there is corruption, and figure out where
it is with <ulink url="git-fsck.html">git-fsck(1)</ulink>; this may be time-consuming.</simpara>
<simpara>Assume the output looks like this:</simpara>
<literallayout>$ git fsck --full
broken link from    tree 2d9263c6d23595e7cb2a21e5ebbb53655278dff8
              to    blob 4b9458b3786228369c63936db65827de3cc06200
missing blob 4b9458b3786228369c63936db65827de3cc06200</literallayout>
<simpara>(Typically there will be some "dangling object" messages too, but they
aren&#8217;t interesting.)</simpara>
<simpara>Now you know that blob 4b9458b3 is missing, and that the tree 2d9263c6
points to it.  If you could find just one copy of that missing blob
object, possibly in some other repository, you could move it into
.git/objects/4b/9458b3&#8230; and be done.  Suppose you can&#8217;t.  You can
still examine the tree that pointed to it with <ulink url="git-ls-tree.html">git-ls-tree(1)</ulink>,
which might output something like:</simpara>
<literallayout>$ git ls-tree 2d9263c6d23595e7cb2a21e5ebbb53655278dff8
100644 blob 8d14531846b95bfa3564b58ccfb7913a034323b8    .gitignore
100644 blob ebf9bf84da0aab5ed944264a5db2a65fe3a3e883    .mailmap
100644 blob ca442d313d86dc67e0a2e5d584b465bd382cbf5c    COPYING
...
100644 blob 4b9458b3786228369c63936db65827de3cc06200    myfile
...</literallayout>
<simpara>So now you know that the missing blob was the data for a file named
"myfile".  And chances are you can also identify the directory&#8212;let&#8217;s
say it&#8217;s in "somedirectory".  If you&#8217;re lucky the missing copy might be
the same as the copy you have checked out in your working tree at
"somedirectory/myfile"; you can test whether that&#8217;s right with
<ulink url="git-hash-object.html">git-hash-object(1)</ulink>:</simpara>
<literallayout>$ git hash-object -w somedirectory/myfile</literallayout>
<simpara>which will create and store a blob object with the contents of
somedirectory/myfile, and output the sha1 of that object.  if you&#8217;re
extremely lucky it might be 4b9458b3786228369c63936db65827de3cc06200, in
which case you&#8217;ve guessed right, and the corruption is fixed!</simpara>
<simpara>Otherwise, you need more information.  How do you tell which version of
the file has been lost?</simpara>
<simpara>The easiest way to do this is with:</simpara>
<literallayout>$ git log --raw --all --full-history -- somedirectory/myfile</literallayout>
<simpara>Because you&#8217;re asking for raw output, you&#8217;ll now get something like</simpara>
<literallayout>commit abc
Author:
Date:
...
:100644 100644 4b9458b... newsha... M somedirectory/myfile


commit xyz
Author:
Date:

...
:100644 100644 oldsha... 4b9458b... M somedirectory/myfile</literallayout>
<simpara>This tells you that the immediately preceding version of the file was
"newsha", and that the immediately following version was "oldsha".
You also know the commit messages that went with the change from oldsha
to 4b9458b and with the change from 4b9458b to newsha.</simpara>
<simpara>If you&#8217;ve been committing small enough changes, you may now have a good
shot at reconstructing the contents of the in-between state 4b9458b.</simpara>
<simpara>If you can do that, you can now recreate the missing object with</simpara>
<literallayout>$ git hash-object -w &lt;recreated-file&gt;</literallayout>
<simpara>and your repository is good again!</simpara>
<simpara>(Btw, you could have ignored the fsck, and started with doing a</simpara>
<literallayout>$ git log --raw --all</literallayout>
<simpara>and just looked for the sha of the missing object (4b9458b..) in that
whole thing. It&#8217;s up to you - git does <emphasis role="strong">have</emphasis> a lot of information, it is
just missing one particular blob version.</simpara>
</section>
</section>
<section id="the-index">
<title>The index</title>
<simpara>The index is a binary file (generally kept in .git/index) containing a
sorted list of path names, each with permissions and the SHA1 of a blob
object; <ulink url="git-ls-files.html">git-ls-files(1)</ulink> can show you the contents of the index:</simpara>
<literallayout>$ git ls-files --stage
100644 63c918c667fa005ff12ad89437f2fdc80926e21c 0       .gitignore
100644 5529b198e8d14decbe4ad99db3f7fb632de0439d 0       .mailmap
100644 6ff87c4664981e4397625791c8ea3bbb5f2279a3 0       COPYING
100644 a37b2152bd26be2c2289e1f57a292534a51a93c7 0       Documentation/.gitignore
100644 fbefe9a45b00a54b58d94d06eca48b03d40a50e0 0       Documentation/Makefile
...
100644 2511aef8d89ab52be5ec6a5e46236b4b6bcd07ea 0       xdiff/xtypes.h
100644 2ade97b2574a9f77e7ae4002a4e07a6a38e46d07 0       xdiff/xutils.c
100644 d5de8292e05e7c36c4b68857c1cf9855e3d2f70a 0       xdiff/xutils.h</literallayout>
<simpara>Note that in older documentation you may see the index called the
"current directory cache" or just the "cache".  It has three important
properties:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>
The index contains all the information necessary to generate a single
(uniquely determined) tree object.
</simpara>
<simpara>For example, running <ulink url="git-commit.html">git-commit(1)</ulink> generates this tree object
from the index, stores it in the object database, and uses it as the
tree object associated with the new commit.</simpara>
</listitem>
<listitem>
<simpara>
The index enables fast comparisons between the tree object it defines
and the working tree.
</simpara>
<simpara>It does this by storing some additional data for each entry (such as
the last modified time).  This data is not displayed above, and is not
stored in the created tree object, but it can be used to determine
quickly which files in the working directory differ from what was
stored in the index, and thus save git from having to read all of the
data from such files to look for changes.</simpara>
</listitem>
<listitem>
<simpara>
It can efficiently represent information about merge conflicts
between different tree objects, allowing each pathname to be
associated with sufficient information about the trees involved that
you can create a three-way merge between them.
</simpara>
<simpara>We saw in <xref linkend="conflict-resolution"/> that during a merge the index can
store multiple versions of a single file (called "stages").  The third
column in the <ulink url="git-ls-files.html">git-ls-files(1)</ulink> output above is the stage
number, and will take on values other than 0 for files with merge
conflicts.</simpara>
</listitem>
</orderedlist>
<simpara>The index is thus a sort of temporary staging area, which is filled with
a tree which you are in the process of working on.</simpara>
<simpara>If you blow the index away entirely, you generally haven&#8217;t lost any
information as long as you have the name of the tree that it described.</simpara>
</section>
</section>
<section id="submodules">
<title>Submodules</title>
<simpara>Large projects are often composed of smaller, self-contained modules.  For
example, an embedded Linux distribution&#8217;s source tree would include every
piece of software in the distribution with some local modifications; a movie
player might need to build against a specific, known-working version of a
decompression library; several independent programs might all share the same
build scripts.</simpara>
<simpara>With centralized revision control systems this is often accomplished by
including every module in one single repository.  Developers can check out
all modules or only the modules they need to work with.  They can even modify
files across several modules in a single commit while moving things around
or updating APIs and translations.</simpara>
<simpara>Git does not allow partial checkouts, so duplicating this approach in Git
would force developers to keep a local copy of modules they are not
interested in touching.  Commits in an enormous checkout would be slower
than you&#8217;d expect as Git would have to scan every directory for changes.
If modules have a lot of local history, clones would take forever.</simpara>
<simpara>On the plus side, distributed revision control systems can much better
integrate with external sources.  In a centralized model, a single arbitrary
snapshot of the external project is exported from its own revision control
and then imported into the local revision control on a vendor branch.  All
the history is hidden.  With distributed revision control you can clone the
entire external history and much more easily follow development and re-merge
local changes.</simpara>
<simpara>Git&#8217;s submodule support allows a repository to contain, as a subdirectory, a
checkout of an external project.  Submodules maintain their own identity;
the submodule support just stores the submodule repository location and
commit ID, so other developers who clone the containing project
("superproject") can easily clone all the submodules at the same revision.
Partial checkouts of the superproject are possible: you can tell Git to
clone none, some or all of the submodules.</simpara>
<simpara>The <ulink url="git-submodule.html">git-submodule(1)</ulink> command is available since Git 1.5.3.  Users
with Git 1.5.2 can look up the submodule commits in the repository and
manually check them out; earlier versions won&#8217;t recognize the submodules at
all.</simpara>
<simpara>To see how submodule support works, create (for example) four example
repositories that can be used later as a submodule:</simpara>
<literallayout>$ mkdir ~/git
$ cd ~/git
$ for i in a b c d
do
        mkdir $i
        cd $i
        git init
        echo "module $i" &gt; $i.txt
        git add $i.txt
        git commit -m "Initial commit, submodule $i"
        cd ..
done</literallayout>
<simpara>Now create the superproject and add all the submodules:</simpara>
<literallayout>$ mkdir super
$ cd super
$ git init
$ for i in a b c d
do
        git submodule add ~/git/$i $i
done</literallayout>
<note><simpara>Do not use local URLs here if you plan to publish your superproject!</simpara></note>
<simpara>See what files <literal>git-submodule</literal> created:</simpara>
<literallayout>$ ls -a
.  ..  .git  .gitmodules  a  b  c  d</literallayout>
<simpara>The <literal>git-submodule add &lt;repo&gt; &lt;path&gt;</literal> command does a couple of things:</simpara>
<itemizedlist>
<listitem>
<simpara>
It clones the submodule from &lt;repo&gt; to the given &lt;path&gt; under the
  current directory and by default checks out the master branch.
</simpara>
</listitem>
<listitem>
<simpara>
It adds the submodule&#8217;s clone path to the <ulink url="gitmodules.html">gitmodules(5)</ulink> file and
  adds this file to the index, ready to be committed.
</simpara>
</listitem>
<listitem>
<simpara>
It adds the submodule&#8217;s current commit ID to the index, ready to be
  committed.
</simpara>
</listitem>
</itemizedlist>
<simpara>Commit the superproject:</simpara>
<literallayout>$ git commit -m "Add submodules a, b, c and d."</literallayout>
<simpara>Now clone the superproject:</simpara>
<literallayout>$ cd ..
$ git clone super cloned
$ cd cloned</literallayout>
<simpara>The submodule directories are there, but they&#8217;re empty:</simpara>
<literallayout>$ ls -a a
.  ..
$ git submodule status
-d266b9873ad50488163457f025db7cdd9683d88b a
-e81d457da15309b4fef4249aba9b50187999670d b
-c1536a972b9affea0f16e0680ba87332dc059146 c
-d96249ff5d57de5de093e6baff9e0aafa5276a74 d</literallayout>
<note><simpara>The commit object names shown above would be different for you, but they
should match the HEAD commit object names of your repositories.  You can check
it by running <literal>git ls-remote ../a</literal>.</simpara></note>
<simpara>Pulling down the submodules is a two-step process. First run <literal>git submodule
init</literal> to add the submodule repository URLs to <literal>.git/config</literal>:</simpara>
<literallayout>$ git submodule init</literallayout>
<simpara>Now use <literal>git-submodule update</literal> to clone the repositories and check out the
commits specified in the superproject:</simpara>
<literallayout>$ git submodule update
$ cd a
$ ls -a
.  ..  .git  a.txt</literallayout>
<simpara>One major difference between <literal>git-submodule update</literal> and <literal>git-submodule add</literal> is
that <literal>git-submodule update</literal> checks out a specific commit, rather than the tip
of a branch. It&#8217;s like checking out a tag: the head is detached, so you&#8217;re not
working on a branch.</simpara>
<literallayout>$ git branch
* (no branch)
  master</literallayout>
<simpara>If you want to make a change within a submodule and you have a detached head,
then you should create or checkout a branch, make your changes, publish the
change within the submodule, and then update the superproject to reference the
new commit:</simpara>
<literallayout>$ git checkout master</literallayout>
<simpara>or</simpara>
<literallayout>$ git checkout -b fix-up</literallayout>
<simpara>then</simpara>
<literallayout>$ echo "adding a line again" &gt;&gt; a.txt
$ git commit -a -m "Updated the submodule from within the superproject."
$ git push
$ cd ..
$ git diff
diff --git a/a b/a
index d266b98..261dfac 160000
--- a/a
+++ b/a
@@ -1 +1 @@
-Subproject commit d266b9873ad50488163457f025db7cdd9683d88b
+Subproject commit 261dfac35cb99d380eb966e102c1197139f7fa24
$ git add a
$ git commit -m "Updated submodule a."
$ git push</literallayout>
<simpara>You have to run <literal>git submodule update</literal> after <literal>git pull</literal> if you want to update
submodules, too.</simpara>
<section id="_pitfalls_with_submodules">
<title>Pitfalls with submodules</title>
<simpara>Always publish the submodule change before publishing the change to the
superproject that references it. If you forget to publish the submodule change,
others won&#8217;t be able to clone the repository:</simpara>
<literallayout>$ cd ~/git/super/a
$ echo i added another line to this file &gt;&gt; a.txt
$ git commit -a -m "doing it wrong this time"
$ cd ..
$ git add a
$ git commit -m "Updated submodule a again."
$ git push
$ cd ~/git/cloned
$ git pull
$ git submodule update
error: pathspec '261dfac35cb99d380eb966e102c1197139f7fa24' did not match any file(s) known to git.
Did you forget to 'git add'?
Unable to checkout '261dfac35cb99d380eb966e102c1197139f7fa24' in submodule path 'a'</literallayout>
<simpara>You also should not rewind branches in a submodule beyond commits that were
ever recorded in any superproject.</simpara>
<simpara>It&#8217;s not safe to run <literal>git submodule update</literal> if you&#8217;ve made and committed
changes within a submodule without checking out a branch first. They will be
silently overwritten:</simpara>
<literallayout>$ cat a.txt
module a
$ echo line added from private2 &gt;&gt; a.txt
$ git commit -a -m "line added inside private2"
$ cd ..
$ git submodule update
Submodule path 'a': checked out 'd266b9873ad50488163457f025db7cdd9683d88b'
$ cd a
$ cat a.txt
module a</literallayout>
<note><simpara>The changes are still visible in the submodule&#8217;s reflog.</simpara></note>
<simpara>This is not the case if you did not commit your changes.</simpara>
</section>
</section>
<section id="low-level-operations">
<title>Low-level git operations</title>
<simpara>Many of the higher-level commands were originally implemented as shell
scripts using a smaller core of low-level git commands.  These can still
be useful when doing unusual things with git, or just as a way to
understand its inner workings.</simpara>
<section id="object-manipulation">
<title>Object access and manipulation</title>
<simpara>The <ulink url="git-cat-file.html">git-cat-file(1)</ulink> command can show the contents of any object,
though the higher-level <ulink url="git-show.html">git-show(1)</ulink> is usually more useful.</simpara>
<simpara>The <ulink url="git-commit-tree.html">git-commit-tree(1)</ulink> command allows constructing commits with
arbitrary parents and trees.</simpara>
<simpara>A tree can be created with <ulink url="git-write-tree.html">git-write-tree(1)</ulink> and its data can be
accessed by <ulink url="git-ls-tree.html">git-ls-tree(1)</ulink>.  Two trees can be compared with
<ulink url="git-diff-tree.html">git-diff-tree(1)</ulink>.</simpara>
<simpara>A tag is created with <ulink url="git-mktag.html">git-mktag(1)</ulink>, and the signature can be
verified by <ulink url="git-verify-tag.html">git-verify-tag(1)</ulink>, though it is normally simpler to
use <ulink url="git-tag.html">git-tag(1)</ulink> for both.</simpara>
</section>
<section id="the-workflow">
<title>The Workflow</title>
<simpara>High-level operations such as <ulink url="git-commit.html">git-commit(1)</ulink>,
<ulink url="git-checkout.html">git-checkout(1)</ulink> and <ulink url="git-reset.html">git-reset(1)</ulink> work by moving data
between the working tree, the index, and the object database.  Git
provides low-level operations which perform each of these steps
individually.</simpara>
<simpara>Generally, all "git" operations work on the index file. Some operations
work <emphasis role="strong">purely</emphasis> on the index file (showing the current state of the
index), but most operations move data between the index file and either
the database or the working directory. Thus there are four main
combinations:</simpara>
<section id="working-directory-to-index">
<title>working directory &#8594; index</title>
<simpara>The <ulink url="git-update-index.html">git-update-index(1)</ulink> command updates the index with
information from the working directory.  You generally update the
index information by just specifying the filename you want to update,
like so:</simpara>
<literallayout>$ git update-index filename</literallayout>
<simpara>but to avoid common mistakes with filename globbing etc, the command
will not normally add totally new entries or remove old entries,
i.e. it will normally just update existing cache entries.</simpara>
<simpara>To tell git that yes, you really do realize that certain files no
longer exist, or that new files should be added, you
should use the <literal>--remove</literal> and <literal>--add</literal> flags respectively.</simpara>
<simpara>NOTE! A <literal>--remove</literal> flag does <emphasis>not</emphasis> mean that subsequent filenames will
necessarily be removed: if the files still exist in your directory
structure, the index will be updated with their new status, not
removed. The only thing <literal>--remove</literal> means is that update-index will be
considering a removed file to be a valid thing, and if the file really
does not exist any more, it will update the index accordingly.</simpara>
<simpara>As a special case, you can also do <literal>git update-index --refresh</literal>, which
will refresh the "stat" information of each index to match the current
stat information. It will <emphasis>not</emphasis> update the object status itself, and
it will only update the fields that are used to quickly test whether
an object still matches its old backing store object.</simpara>
<simpara>The previously introduced <ulink url="git-add.html">git-add(1)</ulink> is just a wrapper for
<ulink url="git-update-index.html">git-update-index(1)</ulink>.</simpara>
</section>
<section id="index-to-object-database">
<title>index &#8594; object database</title>
<simpara>You write your current index file to a "tree" object with the program</simpara>
<literallayout>$ git write-tree</literallayout>
<simpara>that doesn&#8217;t come with any options&#8212;it will just write out the
current index into the set of tree objects that describe that state,
and it will return the name of the resulting top-level tree. You can
use that tree to re-generate the index at any time by going in the
other direction:</simpara>
</section>
<section id="object-database-to-index">
<title>object database &#8594; index</title>
<simpara>You read a "tree" file from the object database, and use that to
populate (and overwrite&#8212;don&#8217;t do this if your index contains any
unsaved state that you might want to restore later!) your current
index.  Normal operation is just</simpara>
<literallayout>$ git read-tree &lt;sha1 of tree&gt;</literallayout>
<simpara>and your index file will now be equivalent to the tree that you saved
earlier. However, that is only your <emphasis>index</emphasis> file: your working
directory contents have not been modified.</simpara>
</section>
<section id="index-to-working-directory">
<title>index &#8594; working directory</title>
<simpara>You update your working directory from the index by "checking out"
files. This is not a very common operation, since normally you&#8217;d just
keep your files updated, and rather than write to your working
directory, you&#8217;d tell the index files about the changes in your
working directory (i.e. <literal>git-update-index</literal>).</simpara>
<simpara>However, if you decide to jump to a new version, or check out somebody
else&#8217;s version, or just restore a previous tree, you&#8217;d populate your
index file with read-tree, and then you need to check out the result
with</simpara>
<literallayout>$ git checkout-index filename</literallayout>
<simpara>or, if you want to check out all of the index, use <literal>-a</literal>.</simpara>
<simpara>NOTE! git-checkout-index normally refuses to overwrite old files, so
if you have an old version of the tree already checked out, you will
need to use the "-f" flag (<emphasis>before</emphasis> the "-a" flag or the filename) to
<emphasis>force</emphasis> the checkout.</simpara>
<simpara>Finally, there are a few odds and ends which are not purely moving
from one representation to the other:</simpara>
</section>
<section id="tying-it-all-together">
<title>Tying it all together</title>
<simpara>To commit a tree you have instantiated with "git write-tree", you&#8217;d
create a "commit" object that refers to that tree and the history
behind it&#8212;most notably the "parent" commits that preceded it in
history.</simpara>
<simpara>Normally a "commit" has one parent: the previous state of the tree
before a certain change was made. However, sometimes it can have two
or more parent commits, in which case we call it a "merge", due to the
fact that such a commit brings together ("merges") two or more
previous states represented by other commits.</simpara>
<simpara>In other words, while a "tree" represents a particular directory state
of a working directory, a "commit" represents that state in "time",
and explains how we got there.</simpara>
<simpara>You create a commit object by giving it the tree that describes the
state at the time of the commit, and a list of parents:</simpara>
<literallayout>$ git commit-tree &lt;tree&gt; -p &lt;parent&gt; [-p &lt;parent2&gt; ..]</literallayout>
<simpara>and then giving the reason for the commit on stdin (either through
redirection from a pipe or file, or by just typing it at the tty).</simpara>
<simpara>git-commit-tree will return the name of the object that represents
that commit, and you should save it away for later use. Normally,
you&#8217;d commit a new <literal>HEAD</literal> state, and while git doesn&#8217;t care where you
save the note about that state, in practice we tend to just write the
result to the file pointed at by <literal>.git/HEAD</literal>, so that we can always see
what the last committed state was.</simpara>
<simpara>Here is an ASCII art by Jon Loeliger that illustrates how
various pieces fit together.</simpara>
<literallayout>                     commit-tree
                      commit obj
                       +----+
                       |    |
                       |    |
                       V    V
                    +-----------+
                    | Object DB |
                    |  Backing  |
                    |   Store   |
                    +-----------+
                       ^
           write-tree  |     |
             tree obj  |     |
                       |     |  read-tree
                       |     |  tree obj
                             V
                    +-----------+
                    |   Index   |
                    |  "cache"  |
                    +-----------+
         update-index  ^
             blob obj  |     |
                       |     |
    checkout-index -u  |     |  checkout-index
             stat      |     |  blob obj
                             V
                    +-----------+
                    |  Working  |
                    | Directory |
                    +-----------+</literallayout>
</section>
</section>
<section id="examining-the-data">
<title>Examining the data</title>
<simpara>You can examine the data represented in the object database and the
index with various helper tools. For every object, you can use
<ulink url="git-cat-file.html">git-cat-file(1)</ulink> to examine details about the
object:</simpara>
<literallayout>$ git cat-file -t &lt;objectname&gt;</literallayout>
<simpara>shows the type of the object, and once you have the type (which is
usually implicit in where you find the object), you can use</simpara>
<literallayout>$ git cat-file blob|tree|commit|tag &lt;objectname&gt;</literallayout>
<simpara>to show its contents. NOTE! Trees have binary content, and as a result
there is a special helper for showing that content, called
<literal>git-ls-tree</literal>, which turns the binary content into a more easily
readable form.</simpara>
<simpara>It&#8217;s especially instructive to look at "commit" objects, since those
tend to be small and fairly self-explanatory. In particular, if you
follow the convention of having the top commit name in <literal>.git/HEAD</literal>,
you can do</simpara>
<literallayout>$ git cat-file commit HEAD</literallayout>
<simpara>to see what the top commit was.</simpara>
</section>
<section id="merging-multiple-trees">
<title>Merging multiple trees</title>
<simpara>Git helps you do a three-way merge, which you can expand to n-way by
repeating the merge procedure arbitrary times until you finally
"commit" the state.  The normal situation is that you&#8217;d only do one
three-way merge (two parents), and commit it, but if you like to, you
can do multiple parents in one go.</simpara>
<simpara>To do a three-way merge, you need the two sets of "commit" objects
that you want to merge, use those to find the closest common parent (a
third "commit" object), and then use those commit objects to find the
state of the directory ("tree" object) at these points.</simpara>
<simpara>To get the "base" for the merge, you first look up the common parent
of two commits with</simpara>
<literallayout>$ git merge-base &lt;commit1&gt; &lt;commit2&gt;</literallayout>
<simpara>which will return you the commit they are both based on.  You should
now look up the "tree" objects of those commits, which you can easily
do with (for example)</simpara>
<literallayout>$ git cat-file commit &lt;commitname&gt; | head -1</literallayout>
<simpara>since the tree object information is always the first line in a commit
object.</simpara>
<simpara>Once you know the three trees you are going to merge (the one "original"
tree, aka the common tree, and the two "result" trees, aka the branches
you want to merge), you do a "merge" read into the index. This will
complain if it has to throw away your old index contents, so you should
make sure that you&#8217;ve committed those&#8212;in fact you would normally
always do a merge against your last commit (which should thus match what
you have in your current index anyway).</simpara>
<simpara>To do the merge, do</simpara>
<literallayout>$ git read-tree -m -u &lt;origtree&gt; &lt;yourtree&gt; &lt;targettree&gt;</literallayout>
<simpara>which will do all trivial merge operations for you directly in the
index file, and you can just write the result out with
<literal>git write-tree</literal>.</simpara>
</section>
<section id="merging-multiple-trees-2">
<title>Merging multiple trees, continued</title>
<simpara>Sadly, many merges aren&#8217;t trivial. If there are files that have
been added, moved or removed, or if both branches have modified the
same file, you will be left with an index tree that contains "merge
entries" in it. Such an index tree can <emphasis>NOT</emphasis> be written out to a tree
object, and you will have to resolve any such merge clashes using
other tools before you can write out the result.</simpara>
<simpara>You can examine such index state with <literal>git ls-files --unmerged</literal>
command.  An example:</simpara>
<literallayout>$ git read-tree -m $orig HEAD $target
$ git ls-files --unmerged
100644 263414f423d0e4d70dae8fe53fa34614ff3e2860 1       hello.c
100644 06fa6a24256dc7e560efa5687fa84b51f0263c3a 2       hello.c
100644 cc44c73eb783565da5831b4d820c962954019b69 3       hello.c</literallayout>
<simpara>Each line of the <literal>git ls-files --unmerged</literal> output begins with
the blob mode bits, blob SHA1, <emphasis>stage number</emphasis>, and the
filename.  The <emphasis>stage number</emphasis> is git&#8217;s way to say which tree it
came from: stage 1 corresponds to <literal>$orig</literal> tree, stage 2 <literal>HEAD</literal>
tree, and stage3 <literal>$target</literal> tree.</simpara>
<simpara>Earlier we said that trivial merges are done inside
<literal>git-read-tree -m</literal>.  For example, if the file did not change
from <literal>$orig</literal> to <literal>HEAD</literal> nor <literal>$target</literal>, or if the file changed
from <literal>$orig</literal> to <literal>HEAD</literal> and <literal>$orig</literal> to <literal>$target</literal> the same way,
obviously the final outcome is what is in <literal>HEAD</literal>.  What the
above example shows is that file <literal>hello.c</literal> was changed from
<literal>$orig</literal> to <literal>HEAD</literal> and <literal>$orig</literal> to <literal>$target</literal> in a different way.
You could resolve this by running your favorite 3-way merge
program, e.g.  <literal>diff3</literal>, <literal>merge</literal>, or git&#8217;s own merge-file, on
the blob objects from these three stages yourself, like this:</simpara>
<literallayout>$ git cat-file blob 263414f... &gt;hello.c~1
$ git cat-file blob 06fa6a2... &gt;hello.c~2
$ git cat-file blob cc44c73... &gt;hello.c~3
$ git merge-file hello.c~2 hello.c~1 hello.c~3</literallayout>
<simpara>This would leave the merge result in <literal>hello.c~2</literal> file, along
with conflict markers if there are conflicts.  After verifying
the merge result makes sense, you can tell git what the final
merge result for this file is by:</simpara>
<literallayout>$ mv -f hello.c~2 hello.c
$ git update-index hello.c</literallayout>
<simpara>When a path is in the "unmerged" state, running <literal>git-update-index</literal> for
that path tells git to mark the path resolved.</simpara>
<simpara>The above is the description of a git merge at the lowest level,
to help you understand what conceptually happens under the hood.
In practice, nobody, not even git itself, runs <literal>git-cat-file</literal> three times
for this.  There is a <literal>git-merge-index</literal> program that extracts the
stages to temporary files and calls a "merge" script on it:</simpara>
<literallayout>$ git merge-index git-merge-one-file hello.c</literallayout>
<simpara>and that is what higher level <literal>git-merge -s resolve</literal> is implemented with.</simpara>
</section>
</section>
<section id="hacking-git">
<title>Hacking git</title>
<simpara>This chapter covers internal details of the git implementation which
probably only git developers need to understand.</simpara>
<section id="object-details">
<title>Object storage format</title>
<simpara>All objects have a statically determined "type" which identifies the
format of the object (i.e. how it is used, and how it can refer to other
objects).  There are currently four different object types: "blob",
"tree", "commit", and "tag".</simpara>
<simpara>Regardless of object type, all objects share the following
characteristics: they are all deflated with zlib, and have a header
that not only specifies their type, but also provides size information
about the data in the object.  It&#8217;s worth noting that the SHA1 hash
that is used to name the object is the hash of the original data
plus this header, so <literal>sha1sum</literal> <emphasis>file</emphasis> does not match the object name
for <emphasis>file</emphasis>.
(Historical note: in the dawn of the age of git the hash
was the sha1 of the <emphasis>compressed</emphasis> object.)</simpara>
<simpara>As a result, the general consistency of an object can always be tested
independently of the contents or the type of the object: all objects can
be validated by verifying that (a) their hashes match the content of the
file and (b) the object successfully inflates to a stream of bytes that
forms a sequence of &lt;ascii type without space&gt; &#43; &lt;space&gt; &#43; &lt;ascii decimal
size&gt; &#43; &lt;byte\0&gt; &#43; &lt;binary object data&gt;.</simpara>
<simpara>The structured objects can further have their structure and
connectivity to other objects verified. This is generally done with
the <literal>git-fsck</literal> program, which generates a full dependency graph
of all objects, and verifies their internal consistency (in addition
to just verifying their superficial consistency through the hash).</simpara>
</section>
<section id="birdview-on-the-source-code">
<title>A birds-eye view of Git&#8217;s source code</title>
<simpara>It is not always easy for new developers to find their way through Git&#8217;s
source code.  This section gives you a little guidance to show where to
start.</simpara>
<simpara>A good place to start is with the contents of the initial commit, with:</simpara>
<literallayout>$ git checkout e83c5163</literallayout>
<simpara>The initial revision lays the foundation for almost everything git has
today, but is small enough to read in one sitting.</simpara>
<simpara>Note that terminology has changed since that revision.  For example, the
README in that revision uses the word "changeset" to describe what we
now call a <link linkend="def_commit_object">commit</link>.</simpara>
<simpara>Also, we do not call it "cache" any more, but rather "index"; however, the
file is still called <literal>cache.h</literal>.  Remark: Not much reason to change it now,
especially since there is no good single name for it anyway, because it is
basically <emphasis>the</emphasis> header file which is included by <emphasis>all</emphasis> of Git&#8217;s C sources.</simpara>
<simpara>If you grasp the ideas in that initial commit, you should check out a
more recent version and skim <literal>cache.h</literal>, <literal>object.h</literal> and <literal>commit.h</literal>.</simpara>
<simpara>In the early days, Git (in the tradition of UNIX) was a bunch of programs
which were extremely simple, and which you used in scripts, piping the
output of one into another. This turned out to be good for initial
development, since it was easier to test new things.  However, recently
many of these parts have become builtins, and some of the core has been
"libified", i.e. put into libgit.a for performance, portability reasons,
and to avoid code duplication.</simpara>
<simpara>By now, you know what the index is (and find the corresponding data
structures in <literal>cache.h</literal>), and that there are just a couple of object types
(blobs, trees, commits and tags) which inherit their common structure from
<literal>struct object</literal>, which is their first member (and thus, you can cast e.g.
<literal>(struct object *)commit</literal> to achieve the <emphasis>same</emphasis> as <literal>&amp;commit&#8594;object</literal>, i.e.
get at the object name and flags).</simpara>
<simpara>Now is a good point to take a break to let this information sink in.</simpara>
<simpara>Next step: get familiar with the object naming.  Read <xref linkend="naming-commits"/>.
There are quite a few ways to name an object (and not only revisions!).
All of these are handled in <literal>sha1_name.c</literal>. Just have a quick look at
the function <literal>get_sha1()</literal>. A lot of the special handling is done by
functions like <literal>get_sha1_basic()</literal> or the likes.</simpara>
<simpara>This is just to get you into the groove for the most libified part of Git:
the revision walker.</simpara>
<simpara>Basically, the initial version of <literal>git-log</literal> was a shell script:</simpara>
<literallayout>$ git-rev-list --pretty $(git-rev-parse --default HEAD "$@") | \
        LESS=-S ${PAGER:-less}</literallayout>
<simpara>What does this mean?</simpara>
<simpara><literal>git-rev-list</literal> is the original version of the revision walker, which
<emphasis>always</emphasis> printed a list of revisions to stdout.  It is still functional,
and needs to, since most new Git programs start out as scripts using
<literal>git-rev-list</literal>.</simpara>
<simpara><literal>git-rev-parse</literal> is not as important any more; it was only used to filter out
options that were relevant for the different plumbing commands that were
called by the script.</simpara>
<simpara>Most of what <literal>git-rev-list</literal> did is contained in <literal>revision.c</literal> and
<literal>revision.h</literal>.  It wraps the options in a struct named <literal>rev_info</literal>, which
controls how and what revisions are walked, and more.</simpara>
<simpara>The original job of <literal>git-rev-parse</literal> is now taken by the function
<literal>setup_revisions()</literal>, which parses the revisions and the common command line
options for the revision walker. This information is stored in the struct
<literal>rev_info</literal> for later consumption. You can do your own command line option
parsing after calling <literal>setup_revisions()</literal>. After that, you have to call
<literal>prepare_revision_walk()</literal> for initialization, and then you can get the
commits one by one with the function <literal>get_revision()</literal>.</simpara>
<simpara>If you are interested in more details of the revision walking process,
just have a look at the first implementation of <literal>cmd_log()</literal>; call
<literal>git show v1.3.0&#126;155^2&#126;4</literal> and scroll down to that function (note that you
no longer need to call <literal>setup_pager()</literal> directly).</simpara>
<simpara>Nowadays, <literal>git-log</literal> is a builtin, which means that it is <emphasis>contained</emphasis> in the
command <literal>git</literal>.  The source side of a builtin is</simpara>
<itemizedlist>
<listitem>
<simpara>
a function called <literal>cmd_&lt;bla&gt;</literal>, typically defined in <literal>builtin-&lt;bla&gt;.c</literal>,
  and declared in <literal>builtin.h</literal>,
</simpara>
</listitem>
<listitem>
<simpara>
an entry in the <literal>commands[]</literal> array in <literal>git.c</literal>, and
</simpara>
</listitem>
<listitem>
<simpara>
an entry in <literal>BUILTIN_OBJECTS</literal> in the <literal>Makefile</literal>.
</simpara>
</listitem>
</itemizedlist>
<simpara>Sometimes, more than one builtin is contained in one source file.  For
example, <literal>cmd_whatchanged()</literal> and <literal>cmd_log()</literal> both reside in <literal>builtin-log.c</literal>,
since they share quite a bit of code.  In that case, the commands which are
<emphasis>not</emphasis> named like the <literal>.c</literal> file in which they live have to be listed in
<literal>BUILT_INS</literal> in the <literal>Makefile</literal>.</simpara>
<simpara><literal>git-log</literal> looks more complicated in C than it does in the original script,
but that allows for a much greater flexibility and performance.</simpara>
<simpara>Here again it is a good point to take a pause.</simpara>
<simpara>Lesson three is: study the code.  Really, it is the best way to learn about
the organization of Git (after you know the basic concepts).</simpara>
<simpara>So, think about something which you are interested in, say, "how can I
access a blob just knowing the object name of it?".  The first step is to
find a Git command with which you can do it.  In this example, it is either
<literal>git-show</literal> or <literal>git-cat-file</literal>.</simpara>
<simpara>For the sake of clarity, let&#8217;s stay with <literal>git-cat-file</literal>, because it</simpara>
<itemizedlist>
<listitem>
<simpara>
is plumbing, and
</simpara>
</listitem>
<listitem>
<simpara>
was around even in the initial commit (it literally went only through
  some 20 revisions as <literal>cat-file.c</literal>, was renamed to <literal>builtin-cat-file.c</literal>
  when made a builtin, and then saw less than 10 versions).
</simpara>
</listitem>
</itemizedlist>
<simpara>So, look into <literal>builtin-cat-file.c</literal>, search for <literal>cmd_cat_file()</literal> and look what
it does.</simpara>
<literallayout>        git_config(git_default_config);
        if (argc != 3)
                usage("git-cat-file [-t|-s|-e|-p|&lt;type&gt;] &lt;sha1&gt;");
        if (get_sha1(argv[2], sha1))
                die("Not a valid object name %s", argv[2]);</literallayout>
<simpara>Let&#8217;s skip over the obvious details; the only really interesting part
here is the call to <literal>get_sha1()</literal>.  It tries to interpret <literal>argv[2]</literal> as an
object name, and if it refers to an object which is present in the current
repository, it writes the resulting SHA-1 into the variable <literal>sha1</literal>.</simpara>
<simpara>Two things are interesting here:</simpara>
<itemizedlist>
<listitem>
<simpara>
<literal>get_sha1()</literal> returns 0 on <emphasis>success</emphasis>.  This might surprise some new
  Git hackers, but there is a long tradition in UNIX to return different
  negative numbers in case of different errors&#8212;and 0 on success.
</simpara>
</listitem>
<listitem>
<simpara>
the variable <literal>sha1</literal> in the function signature of <literal>get_sha1()</literal> is <literal>unsigned
  char *</literal>, but is actually expected to be a pointer to <literal>unsigned
  char[20]</literal>.  This variable will contain the 160-bit SHA-1 of the given
  commit.  Note that whenever a SHA-1 is passed as <literal>unsigned char \*</literal>, it
  is the binary representation, as opposed to the ASCII representation in
  hex characters, which is passed as <literal>char *</literal>.
</simpara>
</listitem>
</itemizedlist>
<simpara>You will see both of these things throughout the code.</simpara>
<simpara>Now, for the meat:</simpara>
<literallayout>        case 0:
                buf = read_object_with_reference(sha1, argv[1], &amp;size, NULL);</literallayout>
<simpara>This is how you read a blob (actually, not only a blob, but any type of
object).  To know how the function <literal>read_object_with_reference()</literal> actually
works, find the source code for it (something like <literal>git grep
read_object_with | grep ":[a-z]"</literal> in the git repository), and read
the source.</simpara>
<simpara>To find out how the result can be used, just read on in <literal>cmd_cat_file()</literal>:</simpara>
<literallayout>        write_or_die(1, buf, size);</literallayout>
<simpara>Sometimes, you do not know where to look for a feature.  In many such cases,
it helps to search through the output of <literal>git log</literal>, and then <literal>git-show</literal> the
corresponding commit.</simpara>
<simpara>Example: If you know that there was some test case for <literal>git-bundle</literal>, but
do not remember where it was (yes, you <emphasis>could</emphasis> <literal>git grep bundle t/</literal>, but that
does not illustrate the point!):</simpara>
<literallayout>$ git log --no-merges t/</literallayout>
<simpara>In the pager (<literal>less</literal>), just search for "bundle", go a few lines back,
and see that it is in commit 18449ab0&#8230;  Now just copy this object name,
and paste it into the command line</simpara>
<literallayout>$ git show 18449ab0</literallayout>
<simpara>Voila.</simpara>
<simpara>Another example: Find out what to do in order to make some script a
builtin:</simpara>
<literallayout>$ git log --no-merges --diff-filter=A builtin-*.c</literallayout>
<simpara>You see, Git is actually the best tool to find out about the source of Git
itself!</simpara>
</section>
</section>
<section id="glossary">
<title>GIT Glossary</title>
<variablelist>
<varlistentry>
<term>
<anchor id="def_alternate_object_database" xreflabel="[def_alternate_object_database]"/>alternate object database
</term>
<listitem>
<simpara>
        Via the alternates mechanism, a <link linkend="def_repository">repository</link>
        can inherit part of its <link linkend="def_object_database">object database</link>
        from another object database, which is called "alternate".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_bare_repository" xreflabel="[def_bare_repository]"/>bare repository
</term>
<listitem>
<simpara>
        A bare repository is normally an appropriately
        named <link linkend="def_directory">directory</link> with a <literal>.git</literal> suffix that does not
        have a locally checked-out copy of any of the files under
        revision control. That is, all of the <literal>git</literal>
        administrative and control files that would normally be present in the
        hidden <literal>.git</literal> sub-directory are directly present in the
        <literal>repository.git</literal> directory instead,
        and no other files are present and checked out. Usually publishers of
        public repositories make bare repositories available.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_blob_object" xreflabel="[def_blob_object]"/>blob object
</term>
<listitem>
<simpara>
        Untyped <link linkend="def_object">object</link>, e.g. the contents of a file.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_branch" xreflabel="[def_branch]"/>branch
</term>
<listitem>
<simpara>
        A "branch" is an active line of development.  The most recent
        <link linkend="def_commit">commit</link> on a branch is referred to as the tip of
        that branch.  The tip of the branch is referenced by a branch
        <link linkend="def_head">head</link>, which moves forward as additional development
        is done on the branch.  A single git
        <link linkend="def_repository">repository</link> can track an arbitrary number of
        branches, but your <link linkend="def_working_tree">working tree</link> is
        associated with just one of them (the "current" or "checked out"
        branch), and <link linkend="def_HEAD">HEAD</link> points to that branch.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_cache" xreflabel="[def_cache]"/>cache
</term>
<listitem>
<simpara>
        Obsolete for: <link linkend="def_index">index</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_chain" xreflabel="[def_chain]"/>chain
</term>
<listitem>
<simpara>
        A list of objects, where each <link linkend="def_object">object</link> in the list contains
        a reference to its successor (for example, the successor of a
        <link linkend="def_commit">commit</link> could be one of its <link linkend="def_parent">parents</link>).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_changeset" xreflabel="[def_changeset]"/>changeset
</term>
<listitem>
<simpara>
        BitKeeper/cvsps speak for "<link linkend="def_commit">commit</link>". Since git does not
        store changes, but states, it really does not make sense to use the term
        "changesets" with git.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_checkout" xreflabel="[def_checkout]"/>checkout
</term>
<listitem>
<simpara>
        The action of updating all or part of the
        <link linkend="def_working_tree">working tree</link> with a <link linkend="def_tree_object">tree object</link>
        or <link linkend="def_blob_object">blob</link> from the
        <link linkend="def_object_database">object database</link>, and updating the
        <link linkend="def_index">index</link> and <link linkend="def_HEAD">HEAD</link> if the whole working tree has
        been pointed at a new <link linkend="def_branch">branch</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_cherry-picking" xreflabel="[def_cherry-picking]"/>cherry-picking
</term>
<listitem>
<simpara>
        In <link linkend="def_SCM">SCM</link> jargon, "cherry pick" means to choose a subset of
        changes out of a series of changes (typically commits) and record them
        as a new series of changes on top of a different codebase. In GIT, this is
        performed by the "git cherry-pick" command to extract the change introduced
        by an existing <link linkend="def_commit">commit</link> and to record it based on the tip
        of the current <link linkend="def_branch">branch</link> as a new commit.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_clean" xreflabel="[def_clean]"/>clean
</term>
<listitem>
<simpara>
        A <link linkend="def_working_tree">working tree</link> is clean, if it
        corresponds to the <link linkend="def_revision">revision</link> referenced by the current
        <link linkend="def_head">head</link>. Also see "<link linkend="def_dirty">dirty</link>".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_commit" xreflabel="[def_commit]"/>commit
</term>
<listitem>
<simpara>
        As a noun: A single point in the
        git history; the entire history of a project is represented as a
        set of interrelated commits.  The word "commit" is often
        used by git in the same places other revision control systems
        use the words "revision" or "version".  Also used as a short
        hand for <link linkend="def_commit_object">commit object</link>.
</simpara>
<simpara>As a verb: The action of storing a new snapshot of the project&#8217;s
state in the git history, by creating a new commit representing the current
state of the <link linkend="def_index">index</link> and advancing <link linkend="def_HEAD">HEAD</link>
to point at the new commit.</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_commit_object" xreflabel="[def_commit_object]"/>commit object
</term>
<listitem>
<simpara>
        An <link linkend="def_object">object</link> which contains the information about a
        particular <link linkend="def_revision">revision</link>, such as <link linkend="def_parent">parents</link>, committer,
        author, date and the <link linkend="def_tree_object">tree object</link> which corresponds
        to the top <link linkend="def_directory">directory</link> of the stored
        revision.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_core_git" xreflabel="[def_core_git]"/>core git
</term>
<listitem>
<simpara>
        Fundamental data structures and utilities of git. Exposes only limited
        source code management tools.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_DAG" xreflabel="[def_DAG]"/>DAG
</term>
<listitem>
<simpara>
        Directed acyclic graph. The <link linkend="def_commit_object">commit objects</link> form a
        directed acyclic graph, because they have parents (directed), and the
        graph of commit objects is acyclic (there is no <link linkend="def_chain">chain</link>
        which begins and ends with the same <link linkend="def_object">object</link>).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_dangling_object" xreflabel="[def_dangling_object]"/>dangling object
</term>
<listitem>
<simpara>
        An <link linkend="def_unreachable_object">unreachable object</link> which is not
        <link linkend="def_reachable">reachable</link> even from other unreachable objects; a
        dangling object has no references to it from any
        reference or <link linkend="def_object">object</link> in the <link linkend="def_repository">repository</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_detached_HEAD" xreflabel="[def_detached_HEAD]"/>detached HEAD
</term>
<listitem>
<simpara>
        Normally the <link linkend="def_HEAD">HEAD</link> stores the name of a
        <link linkend="def_branch">branch</link>.  However, git also allows you to <link linkend="def_checkout">check out</link>
        an arbitrary <link linkend="def_commit">commit</link> that isn&#8217;t necessarily the tip of any
        particular branch.  In this case HEAD is said to be "detached".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_dircache" xreflabel="[def_dircache]"/>dircache
</term>
<listitem>
<simpara>
        You are <emphasis role="strong">waaaaay</emphasis> behind. See <link linkend="def_index">index</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_directory" xreflabel="[def_directory]"/>directory
</term>
<listitem>
<simpara>
        The list you get with "ls" :-)
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_dirty" xreflabel="[def_dirty]"/>dirty
</term>
<listitem>
<simpara>
        A <link linkend="def_working_tree">working tree</link> is said to be "dirty" if
        it contains modifications which have not been <link linkend="def_commit">committed</link> to the current
        <link linkend="def_branch">branch</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_ent" xreflabel="[def_ent]"/>ent
</term>
<listitem>
<simpara>
        Favorite synonym to "<link linkend="def_tree-ish">tree-ish</link>" by some total geeks. See
        <literal>http://en.wikipedia.org/wiki/Ent_(Middle-earth)</literal> for an in-depth
        explanation. Avoid this term, not to confuse people.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_evil_merge" xreflabel="[def_evil_merge]"/>evil merge
</term>
<listitem>
<simpara>
        An evil merge is a <link linkend="def_merge">merge</link> that introduces changes that
        do not appear in any <link linkend="def_parent">parent</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_fast_forward" xreflabel="[def_fast_forward]"/>fast forward
</term>
<listitem>
<simpara>
        A fast-forward is a special type of <link linkend="def_merge">merge</link> where you have a
        <link linkend="def_revision">revision</link> and you are "merging" another
        <link linkend="def_branch">branch</link>'s changes that happen to be a descendant of what
        you have. In such these cases, you do not make a new <link linkend="def_merge">merge</link>
        <link linkend="def_commit">commit</link> but instead just update to his
        revision. This will happen frequently on a
        <link linkend="def_tracking_branch">tracking branch</link> of a remote
        <link linkend="def_repository">repository</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_fetch" xreflabel="[def_fetch]"/>fetch
</term>
<listitem>
<simpara>
        Fetching a <link linkend="def_branch">branch</link> means to get the
        branch&#8217;s <link linkend="def_head_ref">head ref</link> from a remote
        <link linkend="def_repository">repository</link>, to find out which objects are
        missing from the local <link linkend="def_object_database">object database</link>,
        and to get them, too.  See also <ulink url="git-fetch.html">git-fetch(1)</ulink>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_file_system" xreflabel="[def_file_system]"/>file system
</term>
<listitem>
<simpara>
        Linus Torvalds originally designed git to be a user space file system,
        i.e. the infrastructure to hold files and directories. That ensured the
        efficiency and speed of git.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_git_archive" xreflabel="[def_git_archive]"/>git archive
</term>
<listitem>
<simpara>
        Synonym for <link linkend="def_repository">repository</link> (for arch people).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_grafts" xreflabel="[def_grafts]"/>grafts
</term>
<listitem>
<simpara>
        Grafts enables two otherwise different lines of development to be joined
        together by recording fake ancestry information for commits. This way
        you can make git pretend the set of <link linkend="def_parent">parents</link> a <link linkend="def_commit">commit</link> has
        is different from what was recorded when the commit was
        created. Configured via the <literal>.git/info/grafts</literal> file.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_hash" xreflabel="[def_hash]"/>hash
</term>
<listitem>
<simpara>
        In git&#8217;s context, synonym to <link linkend="def_object_name">object name</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_head" xreflabel="[def_head]"/>head
</term>
<listitem>
<simpara>
        A <link linkend="def_ref">named reference</link> to the <link linkend="def_commit">commit</link> at the tip of a
        <link linkend="def_branch">branch</link>.  Heads are stored in
        <literal>$GIT_DIR/refs/heads/</literal>, except when using packed refs. (See
        <ulink url="git-pack-refs.html">git-pack-refs(1)</ulink>.)
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_HEAD" xreflabel="[def_HEAD]"/>HEAD
</term>
<listitem>
<simpara>
        The current <link linkend="def_branch">branch</link>.  In more detail: Your <link linkend="def_working_tree">working tree</link> is normally derived from the state of the tree
        referred to by HEAD.  HEAD is a reference to one of the
        <link linkend="def_head">heads</link> in your repository, except when using a
        <link linkend="def_detached_HEAD">detached HEAD</link>, in which case it may
        reference an arbitrary commit.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_head_ref" xreflabel="[def_head_ref]"/>head ref
</term>
<listitem>
<simpara>
        A synonym for <link linkend="def_head">head</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_hook" xreflabel="[def_hook]"/>hook
</term>
<listitem>
<simpara>
        During the normal execution of several git commands, call-outs are made
        to optional scripts that allow a developer to add functionality or
        checking. Typically, the hooks allow for a command to be pre-verified
        and potentially aborted, and allow for a post-notification after the
        operation is done. The hook scripts are found in the
        <literal>$GIT_DIR/hooks/</literal> directory, and are enabled by simply
        removing the <literal>.sample</literal> suffix from the filename. In earlier versions
        of git you had to make them executable.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_index" xreflabel="[def_index]"/>index
</term>
<listitem>
<simpara>
        A collection of files with stat information, whose contents are stored
        as objects. The index is a stored version of your
        <link linkend="def_working_tree">working tree</link>. Truth be told, it can also contain a second, and even
        a third version of a working tree, which are used
        when <link linkend="def_merge">merging</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_index_entry" xreflabel="[def_index_entry]"/>index entry
</term>
<listitem>
<simpara>
        The information regarding a particular file, stored in the
        <link linkend="def_index">index</link>. An index entry can be unmerged, if a
        <link linkend="def_merge">merge</link> was started, but not yet finished (i.e. if
        the index contains multiple versions of that file).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_master" xreflabel="[def_master]"/>master
</term>
<listitem>
<simpara>
        The default development <link linkend="def_branch">branch</link>. Whenever you
        create a git <link linkend="def_repository">repository</link>, a branch named
        "master" is created, and becomes the active branch. In most
        cases, this contains the local development, though that is
        purely by convention and is not required.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_merge" xreflabel="[def_merge]"/>merge
</term>
<listitem>
<simpara>
        As a verb: To bring the contents of another
        <link linkend="def_branch">branch</link> (possibly from an external
        <link linkend="def_repository">repository</link>) into the current branch.  In the
        case where the merged-in branch is from a different repository,
        this is done by first <link linkend="def_fetch">fetching</link> the remote branch
        and then merging the result into the current branch.  This
        combination of fetch and merge operations is called a
        <link linkend="def_pull">pull</link>.  Merging is performed by an automatic process
        that identifies changes made since the branches diverged, and
        then applies all those changes together.  In cases where changes
        conflict, manual intervention may be required to complete the
        merge.
</simpara>
<simpara>As a noun: unless it is a <link linkend="def_fast_forward">fast forward</link>, a
successful merge results in the creation of a new <link linkend="def_commit">commit</link>
representing the result of the merge, and having as
<link linkend="def_parent">parents</link> the tips of the merged <link linkend="def_branch">branches</link>.
This commit is referred to as a "merge commit", or sometimes just a
"merge".</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_object" xreflabel="[def_object]"/>object
</term>
<listitem>
<simpara>
        The unit of storage in git. It is uniquely identified by the
        <link linkend="def_SHA1">SHA1</link> of its contents. Consequently, an
        object can not be changed.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_object_database" xreflabel="[def_object_database]"/>object database
</term>
<listitem>
<simpara>
        Stores a set of "objects", and an individual <link linkend="def_object">object</link> is
        identified by its <link linkend="def_object_name">object name</link>. The objects usually
        live in <literal>$GIT_DIR/objects/</literal>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_object_identifier" xreflabel="[def_object_identifier]"/>object identifier
</term>
<listitem>
<simpara>
        Synonym for <link linkend="def_object_name">object name</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_object_name" xreflabel="[def_object_name]"/>object name
</term>
<listitem>
<simpara>
        The unique identifier of an <link linkend="def_object">object</link>. The <link linkend="def_hash">hash</link>
        of the object&#8217;s contents using the Secure Hash Algorithm
        1 and usually represented by the 40 character hexadecimal encoding of
        the <link linkend="def_hash">hash</link> of the object.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_object_type" xreflabel="[def_object_type]"/>object type
</term>
<listitem>
<simpara>
        One of the identifiers "<link linkend="def_commit_object">commit</link>",
        "<link linkend="def_tree_object">tree</link>", "<link linkend="def_tag_object">tag</link>" or
        "<link linkend="def_blob_object">blob</link>" describing the type of an
        <link linkend="def_object">object</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_octopus" xreflabel="[def_octopus]"/>octopus
</term>
<listitem>
<simpara>
        To <link linkend="def_merge">merge</link> more than two <link linkend="def_branch">branches</link>. Also denotes an
        intelligent predator.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_origin" xreflabel="[def_origin]"/>origin
</term>
<listitem>
<simpara>
        The default upstream <link linkend="def_repository">repository</link>. Most projects have
        at least one upstream project which they track. By default
        <emphasis>origin</emphasis> is used for that purpose. New upstream updates
        will be fetched into remote <link linkend="def_tracking_branch">tracking branches</link> named
        origin/name-of-upstream-branch, which you can see using
        "<literal>git branch -r</literal>".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_pack" xreflabel="[def_pack]"/>pack
</term>
<listitem>
<simpara>
        A set of objects which have been compressed into one file (to save space
        or to transmit them efficiently).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_pack_index" xreflabel="[def_pack_index]"/>pack index
</term>
<listitem>
<simpara>
        The list of identifiers, and other information, of the objects in a
        <link linkend="def_pack">pack</link>, to assist in efficiently accessing the contents of a
        pack.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_parent" xreflabel="[def_parent]"/>parent
</term>
<listitem>
<simpara>
        A <link linkend="def_commit_object">commit object</link> contains a (possibly empty) list
        of the logical predecessor(s) in the line of development, i.e. its
        parents.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_pickaxe" xreflabel="[def_pickaxe]"/>pickaxe
</term>
<listitem>
<simpara>
        The term <link linkend="def_pickaxe">pickaxe</link> refers to an option to the diffcore
        routines that help select changes that add or delete a given text
        string. With the <literal>--pickaxe-all</literal> option, it can be used to view the full
        <link linkend="def_changeset">changeset</link> that introduced or removed, say, a
        particular line of text. See <ulink url="git-diff.html">git-diff(1)</ulink>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_plumbing" xreflabel="[def_plumbing]"/>plumbing
</term>
<listitem>
<simpara>
        Cute name for <link linkend="def_core_git">core git</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_porcelain" xreflabel="[def_porcelain]"/>porcelain
</term>
<listitem>
<simpara>
        Cute name for programs and program suites depending on
        <link linkend="def_core_git">core git</link>, presenting a high level access to
        core git. Porcelains expose more of a <link linkend="def_SCM">SCM</link>
        interface than the <link linkend="def_plumbing">plumbing</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_pull" xreflabel="[def_pull]"/>pull
</term>
<listitem>
<simpara>
        Pulling a <link linkend="def_branch">branch</link> means to <link linkend="def_fetch">fetch</link> it and
        <link linkend="def_merge">merge</link> it.  See also <ulink url="git-pull.html">git-pull(1)</ulink>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_push" xreflabel="[def_push]"/>push
</term>
<listitem>
<simpara>
        Pushing a <link linkend="def_branch">branch</link> means to get the branch&#8217;s
        <link linkend="def_head_ref">head ref</link> from a remote <link linkend="def_repository">repository</link>,
        find out if it is a direct ancestor to the branch&#8217;s local
        head ref, and in that case, putting all
        objects, which are <link linkend="def_reachable">reachable</link> from the local
        head ref, and which are missing from the remote
        repository, into the remote
        <link linkend="def_object_database">object database</link>, and updating the remote
        head ref. If the remote <link linkend="def_head">head</link> is not an
        ancestor to the local head, the push fails.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_reachable" xreflabel="[def_reachable]"/>reachable
</term>
<listitem>
<simpara>
        All of the ancestors of a given <link linkend="def_commit">commit</link> are said to be
        "reachable" from that commit. More
        generally, one <link linkend="def_object">object</link> is reachable from
        another if we can reach the one from the other by a <link linkend="def_chain">chain</link>
        that follows <link linkend="def_tag">tags</link> to whatever they tag,
        <link linkend="def_commit_object">commits</link> to their parents or trees, and
        <link linkend="def_tree_object">trees</link> to the trees or <link linkend="def_blob_object">blobs</link>
        that they contain.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_rebase" xreflabel="[def_rebase]"/>rebase
</term>
<listitem>
<simpara>
        To reapply a series of changes from a <link linkend="def_branch">branch</link> to a
        different base, and reset the <link linkend="def_head">head</link> of that branch
        to the result.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_ref" xreflabel="[def_ref]"/>ref
</term>
<listitem>
<simpara>
        A 40-byte hex representation of a <link linkend="def_SHA1">SHA1</link> or a name that
        denotes a particular <link linkend="def_object">object</link>. These may be stored in
        <literal>$GIT_DIR/refs/</literal>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_reflog" xreflabel="[def_reflog]"/>reflog
</term>
<listitem>
<simpara>
        A reflog shows the local "history" of a ref.  In other words,
        it can tell you what the 3rd last revision in <emphasis>this</emphasis> repository
        was, and what was the current state in <emphasis>this</emphasis> repository,
        yesterday 9:14pm.  See <ulink url="git-reflog.html">git-reflog(1)</ulink> for details.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_refspec" xreflabel="[def_refspec]"/>refspec
</term>
<listitem>
<simpara>
        A "refspec" is used by <link linkend="def_fetch">fetch</link> and
        <link linkend="def_push">push</link> to describe the mapping between remote
        <link linkend="def_ref">ref</link> and local ref. They are combined with a colon in
        the format &lt;src&gt;:&lt;dst&gt;, preceded by an optional plus sign, +.
        For example: <literal>git fetch $URL
        refs/heads/master:refs/heads/origin</literal> means "grab the master
        <link linkend="def_branch">branch</link> <link linkend="def_head">head</link> from the $URL and store
        it as my origin branch head". And <literal>git push
        $URL refs/heads/master:refs/heads/to-upstream</literal> means "publish my
        master branch head as to-upstream branch at $URL". See also
        <ulink url="git-push.html">git-push(1)</ulink>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_repository" xreflabel="[def_repository]"/>repository
</term>
<listitem>
<simpara>
        A collection of <link linkend="def_ref">refs</link> together with an
        <link linkend="def_object_database">object database</link> containing all objects
        which are <link linkend="def_reachable">reachable</link> from the refs, possibly
        accompanied by meta data from one or more <link linkend="def_porcelain">porcelains</link>. A
        repository can share an object database with other repositories
        via <link linkend="def_alternate_object_database">alternates mechanism</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_resolve" xreflabel="[def_resolve]"/>resolve
</term>
<listitem>
<simpara>
        The action of fixing up manually what a failed automatic
        <link linkend="def_merge">merge</link> left behind.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_revision" xreflabel="[def_revision]"/>revision
</term>
<listitem>
<simpara>
        A particular state of files and directories which was stored in the
        <link linkend="def_object_database">object database</link>. It is referenced by a
        <link linkend="def_commit_object">commit object</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_rewind" xreflabel="[def_rewind]"/>rewind
</term>
<listitem>
<simpara>
        To throw away part of the development, i.e. to assign the
        <link linkend="def_head">head</link> to an earlier <link linkend="def_revision">revision</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_SCM" xreflabel="[def_SCM]"/>SCM
</term>
<listitem>
<simpara>
        Source code management (tool).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_SHA1" xreflabel="[def_SHA1]"/>SHA1
</term>
<listitem>
<simpara>
        Synonym for <link linkend="def_object_name">object name</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_shallow_repository" xreflabel="[def_shallow_repository]"/>shallow repository
</term>
<listitem>
<simpara>
        A shallow <link linkend="def_repository">repository</link> has an incomplete
        history some of whose <link linkend="def_commit">commits</link> have <link linkend="def_parent">parents</link> cauterized away (in other
        words, git is told to pretend that these commits do not have the
        parents, even though they are recorded in the <link linkend="def_commit_object">commit         object</link>). This is sometimes useful when you are interested only in the
        recent history of a project even though the real history recorded in the
        upstream is much larger. A shallow repository
        is created by giving the <literal>--depth</literal> option to <ulink url="git-clone.html">git-clone(1)</ulink>, and
        its history can be later deepened with <ulink url="git-fetch.html">git-fetch(1)</ulink>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_symref" xreflabel="[def_symref]"/>symref
</term>
<listitem>
<simpara>
        Symbolic reference: instead of containing the <link linkend="def_SHA1">SHA1</link>
        id itself, it is of the format <emphasis>ref: refs/some/thing</emphasis> and when
        referenced, it recursively dereferences to this reference.
        <emphasis><link linkend="def_HEAD">HEAD</link></emphasis> is a prime example of a symref. Symbolic
        references are manipulated with the <ulink url="git-symbolic-ref.html">git-symbolic-ref(1)</ulink>
        command.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_tag" xreflabel="[def_tag]"/>tag
</term>
<listitem>
<simpara>
        A <link linkend="def_ref">ref</link> pointing to a <link linkend="def_tag_object">tag</link> or
        <link linkend="def_commit_object">commit object</link>. In contrast to a <link linkend="def_head">head</link>,
        a tag is not changed by a <link linkend="def_commit">commit</link>. Tags (not
        <link linkend="def_tag_object">tag objects</link>) are stored in <literal>$GIT_DIR/refs/tags/</literal>. A
        git tag has nothing to do with a Lisp tag (which would be
        called an <link linkend="def_object_type">object type</link> in git&#8217;s context). A
        tag is most typically used to mark a particular point in the
        commit ancestry <link linkend="def_chain">chain</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_tag_object" xreflabel="[def_tag_object]"/>tag object
</term>
<listitem>
<simpara>
        An <link linkend="def_object">object</link> containing a <link linkend="def_ref">ref</link> pointing to
        another object, which can contain a message just like a
        <link linkend="def_commit_object">commit object</link>. It can also contain a (PGP)
        signature, in which case it is called a "signed tag object".
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_topic_branch" xreflabel="[def_topic_branch]"/>topic branch
</term>
<listitem>
<simpara>
        A regular git <link linkend="def_branch">branch</link> that is used by a developer to
        identify a conceptual line of development. Since branches are very easy
        and inexpensive, it is often desirable to have several small branches
        that each contain very well defined concepts or small incremental yet
        related changes.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_tracking_branch" xreflabel="[def_tracking_branch]"/>tracking branch
</term>
<listitem>
<simpara>
        A regular git <link linkend="def_branch">branch</link> that is used to follow changes from
        another <link linkend="def_repository">repository</link>. A tracking
        branch should not contain direct modifications or have local commits
        made to it. A tracking branch can usually be
        identified as the right-hand-side <link linkend="def_ref">ref</link> in a Pull:
        <link linkend="def_refspec">refspec</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_tree" xreflabel="[def_tree]"/>tree
</term>
<listitem>
<simpara>
        Either a <link linkend="def_working_tree">working tree</link>, or a <link linkend="def_tree_object">tree         object</link> together with the dependent <link linkend="def_blob_object">blob</link> and tree objects
        (i.e. a stored representation of a working tree).
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_tree_object" xreflabel="[def_tree_object]"/>tree object
</term>
<listitem>
<simpara>
        An <link linkend="def_object">object</link> containing a list of file names and modes along
        with refs to the associated blob and/or tree objects. A
        <link linkend="def_tree">tree</link> is equivalent to a <link linkend="def_directory">directory</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_tree-ish" xreflabel="[def_tree-ish]"/>tree-ish
</term>
<listitem>
<simpara>
        A <link linkend="def_ref">ref</link> pointing to either a <link linkend="def_commit_object">commit         object</link>, a <link linkend="def_tree_object">tree object</link>, or a <link linkend="def_tag_object">tag         object</link> pointing to a tag or commit or tree object.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_unmerged_index" xreflabel="[def_unmerged_index]"/>unmerged index
</term>
<listitem>
<simpara>
        An <link linkend="def_index">index</link> which contains unmerged
        <link linkend="def_index_entry">index entries</link>.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_unreachable_object" xreflabel="[def_unreachable_object]"/>unreachable object
</term>
<listitem>
<simpara>
        An <link linkend="def_object">object</link> which is not <link linkend="def_reachable">reachable</link> from a
        <link linkend="def_branch">branch</link>, <link linkend="def_tag">tag</link>, or any other reference.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
<anchor id="def_working_tree" xreflabel="[def_working_tree]"/>working tree
</term>
<listitem>
<simpara>
        The tree of actual checked out files.  The working tree is
        normally equal to the <link linkend="def_HEAD">HEAD</link> plus any local changes
        that you have made but not yet committed.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</section>
<appendix id="git-quick-start">
<title>Git Quick Reference</title>
<simpara>This is a quick summary of the major commands; the previous chapters
explain how these work in more detail.</simpara>
<section id="quick-creating-a-new-repository">
<title>Creating a new repository</title>
<simpara>From a tarball:</simpara>
<literallayout>$ tar xzf project.tar.gz
$ cd project
$ git init
Initialized empty Git repository in .git/
$ git add .
$ git commit</literallayout>
<simpara>From a remote repository:</simpara>
<literallayout>$ git clone git://example.com/pub/project.git
$ cd project</literallayout>
</section>
<section id="managing-branches">
<title>Managing branches</title>
<literallayout>$ git branch         # list all local branches in this repo
$ git checkout test  # switch working directory to branch "test"
$ git branch new     # create branch "new" starting at current HEAD
$ git branch -d new  # delete branch "new"</literallayout>
<simpara>Instead of basing a new branch on current HEAD (the default), use:</simpara>
<literallayout>$ git branch new test    # branch named "test"
$ git branch new v2.6.15 # tag named v2.6.15
$ git branch new HEAD^   # commit before the most recent
$ git branch new HEAD^^  # commit before that
$ git branch new test~10 # ten commits before tip of branch "test"</literallayout>
<simpara>Create and switch to a new branch at the same time:</simpara>
<literallayout>$ git checkout -b new v2.6.15</literallayout>
<simpara>Update and examine branches from the repository you cloned from:</simpara>
<literallayout>$ git fetch             # update
$ git branch -r         # list
  origin/master
  origin/next
  ...
$ git checkout -b masterwork origin/master</literallayout>
<simpara>Fetch a branch from a different repository, and give it a new
name in your repository:</simpara>
<literallayout>$ git fetch git://example.com/project.git theirbranch:mybranch
$ git fetch git://example.com/project.git v2.6.15:mybranch</literallayout>
<simpara>Keep a list of repositories you work with regularly:</simpara>
<literallayout>$ git remote add example git://example.com/project.git
$ git remote                    # list remote repositories
example
origin
$ git remote show example       # get details
* remote example
  URL: git://example.com/project.git
  Tracked remote branches
    master
    next
    ...
$ git fetch example             # update branches from example
$ git branch -r                 # list all remote branches</literallayout>
</section>
<section id="exploring-history">
<title>Exploring history</title>
<literallayout>$ gitk                      # visualize and browse history
$ git log                   # list all commits
$ git log src/              # ...modifying src/
$ git log v2.6.15..v2.6.16  # ...in v2.6.16, not in v2.6.15
$ git log master..test      # ...in branch test, not in branch master
$ git log test..master      # ...in branch master, but not in test
$ git log test...master     # ...in one branch, not in both
$ git log -S'foo()'         # ...where difference contain "foo()"
$ git log --since="2 weeks ago"
$ git log -p                # show patches as well
$ git show                  # most recent commit
$ git diff v2.6.15..v2.6.16 # diff between two tagged versions
$ git diff v2.6.15..HEAD    # diff with current head
$ git grep "foo()"          # search working directory for "foo()"
$ git grep v2.6.15 "foo()"  # search old tree for "foo()"
$ git show v2.6.15:a.txt    # look at old version of a.txt</literallayout>
<simpara>Search for regressions:</simpara>
<literallayout>$ git bisect start
$ git bisect bad                # current version is bad
$ git bisect good v2.6.13-rc2   # last known good revision
Bisecting: 675 revisions left to test after this
                                # test here, then:
$ git bisect good               # if this revision is good, or
$ git bisect bad                # if this revision is bad.
                                # repeat until done.</literallayout>
</section>
<section id="making-changes">
<title>Making changes</title>
<simpara>Make sure git knows who to blame:</simpara>
<literallayout>$ cat &gt;&gt;~/.gitconfig &lt;&lt;\EOF
[user]
        name = Your Name Comes Here
        email = you@yourdomain.example.com
EOF</literallayout>
<simpara>Select file contents to include in the next commit, then make the
commit:</simpara>
<literallayout>$ git add a.txt    # updated file
$ git add b.txt    # new file
$ git rm c.txt     # old file
$ git commit</literallayout>
<simpara>Or, prepare and create the commit in one step:</simpara>
<literallayout>$ git commit d.txt # use latest content only of d.txt
$ git commit -a    # use latest content of all tracked files</literallayout>
</section>
<section id="merging">
<title>Merging</title>
<literallayout>$ git merge test   # merge branch "test" into the current branch
$ git pull git://example.com/project.git master
                   # fetch and merge in remote branch
$ git pull . test  # equivalent to git merge test</literallayout>
</section>
<section id="sharing-your-changes">
<title>Sharing your changes</title>
<simpara>Importing or exporting patches:</simpara>
<literallayout>$ git format-patch origin..HEAD # format a patch for each commit
                                # in HEAD but not in origin
$ git am mbox # import patches from the mailbox "mbox"</literallayout>
<simpara>Fetch a branch in a different git repository, then merge into the
current branch:</simpara>
<literallayout>$ git pull git://example.com/project.git theirbranch</literallayout>
<simpara>Store the fetched branch into a local branch before merging into the
current branch:</simpara>
<literallayout>$ git pull git://example.com/project.git theirbranch:mybranch</literallayout>
<simpara>After creating commits on a local branch, update the remote
branch with your commits:</simpara>
<literallayout>$ git push ssh://example.com/project.git mybranch:theirbranch</literallayout>
<simpara>When remote and local branch are both named "test":</simpara>
<literallayout>$ git push ssh://example.com/project.git test</literallayout>
<simpara>Shortcut version for a frequently used remote repository:</simpara>
<literallayout>$ git remote add example ssh://example.com/project.git
$ git push example test</literallayout>
</section>
<section id="repository-maintenance">
<title>Repository maintenance</title>
<simpara>Check for corruption:</simpara>
<literallayout>$ git fsck</literallayout>
<simpara>Recompress, remove unused cruft:</simpara>
<literallayout>$ git gc</literallayout>
</section>
</appendix>
<appendix id="todo">
<title>Notes and todo list for this manual</title>
<simpara>This is a work in progress.</simpara>
<simpara>The basic requirements:</simpara>
<itemizedlist>
<listitem>
<simpara>
It must be readable in order, from beginning to end, by someone
  intelligent with a basic grasp of the UNIX command line, but without
  any special knowledge of git.  If necessary, any other prerequisites
  should be specifically mentioned as they arise.
</simpara>
</listitem>
<listitem>
<simpara>
Whenever possible, section headings should clearly describe the task
  they explain how to do, in language that requires no more knowledge
  than necessary: for example, "importing patches into a project" rather
  than "the git-am command"
</simpara>
</listitem>
</itemizedlist>
<simpara>Think about how to create a clear chapter dependency graph that will
allow people to get to important topics without necessarily reading
everything in between.</simpara>
<simpara>Scan Documentation/ for other stuff left out; in particular:</simpara>
<itemizedlist>
<listitem>
<simpara>
howto&#8217;s
</simpara>
</listitem>
<listitem>
<simpara>
some of technical/?
</simpara>
</listitem>
<listitem>
<simpara>
hooks
</simpara>
</listitem>
<listitem>
<simpara>
list of commands in <ulink url="git.html">git(1)</ulink>
</simpara>
</listitem>
</itemizedlist>
<simpara>Scan email archives for other stuff left out</simpara>
<simpara>Scan man pages to see if any assume more background than this manual
provides.</simpara>
<simpara>Simplify beginning by suggesting disconnected head instead of
temporary branch creation?</simpara>
<simpara>Add more good examples.  Entire sections of just cookbook examples
might be a good idea; maybe make an "advanced examples" section a
standard end-of-chapter section?</simpara>
<simpara>Include cross-references to the glossary, where appropriate.</simpara>
<simpara>Document shallow clones?  See draft 1.5.0 release notes for some
documentation.</simpara>
<simpara>Add a section on working with other version control systems, including
CVS, Subversion, and just imports of series of release tarballs.</simpara>
<simpara>More details on gitweb?</simpara>
<simpara>Write a chapter on using plumbing and writing scripts.</simpara>
<simpara>Alternates, clone -reference, etc.</simpara>
<simpara>More on recovery from repository corruption.  See:
        <ulink url="http://marc.theaimsgroup.com/?l=git&amp;m=117263864820799&amp;w=2">http://marc.theaimsgroup.com/?l=git&amp;m=117263864820799&amp;w=2</ulink>
        <ulink url="http://marc.theaimsgroup.com/?l=git&amp;m=117147855503798&amp;w=2">http://marc.theaimsgroup.com/?l=git&amp;m=117147855503798&amp;w=2</ulink></simpara>
</appendix>
</article>
