<?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" id="git-read-tree(1)">
<articleinfo>
    <title>git-read-tree(1)</title>
        <indexterm>
                <primary>git-read-tree(1)</primary>
        </indexterm>
</articleinfo>
<simplesect id="_name">
<title>NAME</title>
<simpara>git-read-tree - Reads tree information into the index</simpara>
</simplesect>
<simplesect id="_synopsis">
<title>SYNOPSIS</title>
<simpara><emphasis>git read-tree</emphasis> (&lt;tree-ish&gt; | [[-m [--trivial] [--aggressive] | --reset | --prefix=&lt;prefix&gt;] [-u | -i]] [--exclude-per-directory=&lt;gitignore&gt;] [--index-output=&lt;file&gt;] &lt;tree-ish1&gt; [&lt;tree-ish2&gt; [&lt;tree-ish3&gt;]])</simpara>
</simplesect>
<simplesect id="_description">
<title>DESCRIPTION</title>
<simpara>Reads the tree information given by &lt;tree-ish&gt; into the index,
but does not actually <emphasis role="strong">update</emphasis> any of the files it "caches". (see:
<xref linkend="git-checkout-index(1)"/>)</simpara>
<simpara>Optionally, it can merge a tree into the index, perform a
fast-forward (i.e. 2-way) merge, or a 3-way merge, with the <literal>-m</literal>
flag.  When used with <literal>-m</literal>, the <literal>-u</literal> flag causes it to also update
the files in the work tree with the result of the merge.</simpara>
<simpara>Trivial merges are done by <emphasis>git-read-tree</emphasis> itself.  Only conflicting paths
will be in unmerged state when <emphasis>git-read-tree</emphasis> returns.</simpara>
</simplesect>
<simplesect id="_options">
<title>OPTIONS</title>
<variablelist>
<varlistentry>
<term>
-m
</term>
<listitem>
<simpara>
        Perform a merge, not just a read.  The command will
        refuse to run if your index file has unmerged entries,
        indicating that you have not finished previous merge you
        started.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--reset
</term>
<listitem>
<simpara>
        Same as -m, except that unmerged entries are discarded
        instead of failing.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-u
</term>
<listitem>
<simpara>
        After a successful merge, update the files in the work
        tree with the result of the merge.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-i
</term>
<listitem>
<simpara>
        Usually a merge requires the index file as well as the
        files in the working tree are up to date with the
        current head commit, in order not to lose local
        changes.  This flag disables the check with the working
        tree and is meant to be used when creating a merge of
        trees that are not directly related to the current
        working tree status into a temporary index file.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
-v
</term>
<listitem>
<simpara>
        Show the progress of checking files out.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--trivial
</term>
<listitem>
<simpara>
        Restrict three-way merge by <emphasis>git-read-tree</emphasis> to happen
        only if there is no file-level merging required, instead
        of resolving merge for trivial cases and leaving
        conflicting files unresolved in the index.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--aggressive
</term>
<listitem>
<simpara>
        Usually a three-way merge by <emphasis>git-read-tree</emphasis> resolves
        the merge for really trivial cases and leaves other
        cases unresolved in the index, so that Porcelains can
        implement different merge policies.  This flag makes the
        command to resolve a few more cases internally:
</simpara>
<itemizedlist>
<listitem>
<simpara>
when one side removes a path and the other side leaves the path
  unmodified.  The resolution is to remove that path.
</simpara>
</listitem>
<listitem>
<simpara>
when both sides remove a path.  The resolution is to remove that path.
</simpara>
</listitem>
<listitem>
<simpara>
when both sides adds a path identically.  The resolution
  is to add that path.
</simpara>
</listitem>
</itemizedlist>
</listitem>
</varlistentry>
<varlistentry>
<term>
--prefix=&lt;prefix&gt;/
</term>
<listitem>
<simpara>
        Keep the current index contents, and read the contents
        of named tree-ish under directory at <literal>&lt;prefix&gt;</literal>.  The
        original index file cannot have anything at the path
        <literal>&lt;prefix&gt;</literal> itself, and have nothing in <literal>&lt;prefix&gt;/</literal>
        directory.  Note that the <literal>&lt;prefix&gt;/</literal> value must end
        with a slash.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--exclude-per-directory=&lt;gitignore&gt;
</term>
<listitem>
<simpara>
        When running the command with <literal>-u</literal> and <literal>-m</literal> options, the
        merge result may need to overwrite paths that are not
        tracked in the current branch.  The command usually
        refuses to proceed with the merge to avoid losing such a
        path.  However this safety valve sometimes gets in the
        way.  For example, it often happens that the other
        branch added a file that used to be a generated file in
        your branch, and the safety valve triggers when you try
        to switch to that branch after you ran <literal>make</literal> but before
        running <literal>make clean</literal> to remove the generated file.  This
        option tells the command to read per-directory exclude
        file (usually <emphasis>.gitignore</emphasis>) and allows such an untracked
        but explicitly ignored file to be overwritten.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
--index-output=&lt;file&gt;
</term>
<listitem>
<simpara>
        Instead of writing the results out to <literal>$GIT_INDEX_FILE</literal>,
        write the resulting index in the named file.  While the
        command is operating, the original index file is locked
        with the same mechanism as usual.  The file must allow
        to be rename(2)ed into from a temporary file that is
        created next to the usual index file; typically this
        means it needs to be on the same filesystem as the index
        file itself, and you need write permission to the
        directories the index file and index output file are
        located in.
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term>
&lt;tree-ish#&gt;
</term>
<listitem>
<simpara>
        The id of the tree object(s) to be read/merged.
</simpara>
</listitem>
</varlistentry>
</variablelist>
</simplesect>
<simplesect id="_merging">
<title>Merging</title>
<simpara>If <literal>-m</literal> is specified, <emphasis>git-read-tree</emphasis> can perform 3 kinds of
merge, a single tree merge if only 1 tree is given, a
fast-forward merge with 2 trees, or a 3-way merge if 3 trees are
provided.</simpara>
<simplesect id="_single_tree_merge">
<title>Single Tree Merge</title>
<simpara>If only 1 tree is specified, <emphasis>git-read-tree</emphasis> operates as if the user did not
specify <literal>-m</literal>, except that if the original index has an entry for a
given pathname, and the contents of the path matches with the tree
being read, the stat info from the index is used. (In other words, the
index&#8217;s stat()s take precedence over the merged tree&#8217;s).</simpara>
<simpara>That means that if you do a <literal>git read-tree -m &lt;newtree&gt;</literal> followed by a
<literal>git checkout-index -f -u -a</literal>, the <emphasis>git-checkout-index</emphasis> only checks out
the stuff that really changed.</simpara>
<simpara>This is used to avoid unnecessary false hits when <emphasis>git-diff-files</emphasis> is
run after <emphasis>git-read-tree</emphasis>.</simpara>
</simplesect>
<simplesect id="_two_tree_merge">
<title>Two Tree Merge</title>
<simpara>Typically, this is invoked as <literal>git read-tree -m $H $M</literal>, where $H
is the head commit of the current repository, and $M is the head
of a foreign tree, which is simply ahead of $H (i.e. we are in a
fast forward situation).</simpara>
<simpara>When two trees are specified, the user is telling <emphasis>git-read-tree</emphasis>
the following:</simpara>
<orderedlist numeration="arabic">
<listitem>
<simpara>
The current index and work tree is derived from $H, but
        the user may have local changes in them since $H;
</simpara>
</listitem>
<listitem>
<simpara>
The user wants to fast-forward to $M.
</simpara>
</listitem>
</orderedlist>
<simpara>In this case, the <literal>git read-tree -m $H $M</literal> command makes sure
that no local change is lost as the result of this "merge".
Here are the "carry forward" rules:</simpara>
<literallayout class="monospaced">  I (index)           H        M        Result
 -------------------------------------------------------
0 nothing             nothing  nothing  (does not happen)
1 nothing             nothing  exists   use M
2 nothing             exists   nothing  remove path from index
3 nothing             exists   exists,  use M if "initial checkout"
                               H == M   keep index otherwise
                               exists   fail
                               H != M</literallayout>
<literallayout class="monospaced">  clean I==H  I==M
 ------------------
4 yes   N/A   N/A     nothing  nothing  keep index
5 no    N/A   N/A     nothing  nothing  keep index</literallayout>
<literallayout class="monospaced">6 yes   N/A   yes     nothing  exists   keep index
7 no    N/A   yes     nothing  exists   keep index
8 yes   N/A   no      nothing  exists   fail
9 no    N/A   no      nothing  exists   fail</literallayout>
<literallayout class="monospaced">10 yes   yes   N/A     exists   nothing  remove path from index
11 no    yes   N/A     exists   nothing  fail
12 yes   no    N/A     exists   nothing  fail
13 no    no    N/A     exists   nothing  fail</literallayout>
<literallayout class="monospaced">   clean (H=M)
  ------
14 yes                 exists   exists   keep index
15 no                  exists   exists   keep index</literallayout>
<literallayout class="monospaced">   clean I==H  I==M (H!=M)
  ------------------
16 yes   no    no      exists   exists   fail
17 no    no    no      exists   exists   fail
18 yes   no    yes     exists   exists   keep index
19 no    no    yes     exists   exists   keep index
20 yes   yes   no      exists   exists   use M
21 no    yes   no      exists   exists   fail</literallayout>
<simpara>In all "keep index" cases, the index entry stays as in the
original index file.  If the entry were not up to date,
<emphasis>git-read-tree</emphasis> keeps the copy in the work tree intact when
operating under the -u flag.</simpara>
<simpara>When this form of <emphasis>git-read-tree</emphasis> returns successfully, you can
see what "local changes" you made are carried forward by running
<literal>git diff-index --cached $M</literal>.  Note that this does not
necessarily match <literal>git diff-index --cached $H</literal> would have
produced before such a two tree merge.  This is because of cases
18 and 19 --- if you already had the changes in $M (e.g. maybe
you picked it up via e-mail in a patch form), <literal>git diff-index
--cached $H</literal> would have told you about the change before this
merge, but it would not show in <literal>git diff-index --cached $M</literal>
output after two-tree merge.</simpara>
<simpara>Case #3 is slightly tricky and needs explanation.  The result from this
rule logically should be to remove the path if the user staged the removal
of the path and then switching to a new branch.  That however will prevent
the initial checkout from happening, so the rule is modified to use M (new
tree) only when the contents of the index is empty.  Otherwise the removal
of the path is kept as long as $H and $M are the same.</simpara>
</simplesect>
<simplesect id="_3_way_merge">
<title>3-Way Merge</title>
<simpara>Each "index" entry has two bits worth of "stage" state. stage 0 is the
normal one, and is the only one you&#8217;d see in any kind of normal use.</simpara>
<simpara>However, when you do <emphasis>git-read-tree</emphasis> with three trees, the "stage"
starts out at 1.</simpara>
<simpara>This means that you can do</simpara>
<literallayout>$ git read-tree -m &lt;tree1&gt; &lt;tree2&gt; &lt;tree3&gt;</literallayout>
<simpara>and you will end up with an index with all of the &lt;tree1&gt; entries in
"stage1", all of the &lt;tree2&gt; entries in "stage2" and all of the
&lt;tree3&gt; entries in "stage3".  When performing a merge of another
branch into the current branch, we use the common ancestor tree
as &lt;tree1&gt;, the current branch head as &lt;tree2&gt;, and the other
branch head as &lt;tree3&gt;.</simpara>
<simpara>Furthermore, <emphasis>git-read-tree</emphasis> has special-case logic that says: if you see
a file that matches in all respects in the following states, it
"collapses" back to "stage0":</simpara>
<itemizedlist>
<listitem>
<simpara>
stage 2 and 3 are the same; take one or the other (it makes no
     difference - the same work has been done on our branch in
     stage 2 and their branch in stage 3)
</simpara>
</listitem>
<listitem>
<simpara>
stage 1 and stage 2 are the same and stage 3 is different; take
     stage 3 (our branch in stage 2 did not do anything since the
     ancestor in stage 1 while their branch in stage 3 worked on
     it)
</simpara>
</listitem>
<listitem>
<simpara>
stage 1 and stage 3 are the same and stage 2 is different take
     stage 2 (we did something while they did nothing)
</simpara>
</listitem>
</itemizedlist>
<simpara>The <emphasis>git-write-tree</emphasis> command refuses to write a nonsensical tree, and it
will complain about unmerged entries if it sees a single entry that is not
stage 0.</simpara>
<simpara>OK, this all sounds like a collection of totally nonsensical rules,
but it&#8217;s actually exactly what you want in order to do a fast
merge. The different stages represent the "result tree" (stage 0, aka
"merged"), the original tree (stage 1, aka "orig"), and the two trees
you are trying to merge (stage 2 and 3 respectively).</simpara>
<simpara>The order of stages 1, 2 and 3 (hence the order of three
&lt;tree-ish&gt; command line arguments) are significant when you
start a 3-way merge with an index file that is already
populated.  Here is an outline of how the algorithm works:</simpara>
<itemizedlist>
<listitem>
<simpara>
if a file exists in identical format in all three trees, it will
  automatically collapse to "merged" state by <emphasis>git-read-tree</emphasis>.
</simpara>
</listitem>
<listitem>
<simpara>
a file that has <emphasis>any</emphasis> difference what-so-ever in the three trees
  will stay as separate entries in the index. It&#8217;s up to "porcelain
  policy" to determine how to remove the non-0 stages, and insert a
  merged version.
</simpara>
</listitem>
<listitem>
<simpara>
the index file saves and restores with all this information, so you
  can merge things incrementally, but as long as it has entries in
  stages 1/2/3 (i.e., "unmerged entries") you can&#8217;t write the result. So
  now the merge algorithm ends up being really simple:
</simpara>
<itemizedlist>
<listitem>
<simpara>
you walk the index in order, and ignore all entries of stage 0,
    since they&#8217;ve already been done.
</simpara>
</listitem>
<listitem>
<simpara>
if you find a "stage1", but no matching "stage2" or "stage3", you
    know it&#8217;s been removed from both trees (it only existed in the
    original tree), and you remove that entry.
</simpara>
</listitem>
<listitem>
<simpara>
if you find a matching "stage2" and "stage3" tree, you remove one
    of them, and turn the other into a "stage0" entry. Remove any
    matching "stage1" entry if it exists too.  .. all the normal
    trivial rules ..
</simpara>
</listitem>
</itemizedlist>
</listitem>
</itemizedlist>
<simpara>You would normally use <emphasis>git-merge-index</emphasis> with supplied
<emphasis>git-merge-one-file</emphasis> to do this last step.  The script updates
the files in the working tree as it merges each path and at the
end of a successful merge.</simpara>
<simpara>When you start a 3-way merge with an index file that is already
populated, it is assumed that it represents the state of the
files in your work tree, and you can even have files with
changes unrecorded in the index file.  It is further assumed
that this state is "derived" from the stage 2 tree.  The 3-way
merge refuses to run if it finds an entry in the original index
file that does not match stage 2.</simpara>
<simpara>This is done to prevent you from losing your work-in-progress
changes, and mixing your random changes in an unrelated merge
commit.  To illustrate, suppose you start from what has been
committed last to your repository:</simpara>
<literallayout>$ JC=`git rev-parse --verify "HEAD^0"`
$ git checkout-index -f -u -a $JC</literallayout>
<simpara>You do random edits, without running <emphasis>git-update-index</emphasis>.  And then
you notice that the tip of your "upstream" tree has advanced
since you pulled from him:</simpara>
<literallayout>$ git fetch git://.... linus
$ LT=`cat .git/FETCH_HEAD`</literallayout>
<simpara>Your work tree is still based on your HEAD ($JC), but you have
some edits since.  Three-way merge makes sure that you have not
added or modified index entries since $JC, and if you haven&#8217;t,
then does the right thing.  So with the following sequence:</simpara>
<literallayout>$ git read-tree -m -u `git merge-base $JC $LT` $JC $LT
$ git merge-index git-merge-one-file -a
$ echo "Merge with Linus" | \
  git commit-tree `git write-tree` -p $JC -p $LT</literallayout>
<simpara>what you would commit is a pure merge between $JC and $LT without
your work-in-progress changes, and your work tree would be
updated to the result of the merge.</simpara>
<simpara>However, if you have local changes in the working tree that
would be overwritten by this merge, <emphasis>git-read-tree</emphasis> will refuse
to run to prevent your changes from being lost.</simpara>
<simpara>In other words, there is no need to worry about what exists only
in the working tree.  When you have local changes in a part of
the project that is not involved in the merge, your changes do
not interfere with the merge, and are kept intact.  When they
<emphasis role="strong">do</emphasis> interfere, the merge does not even start (<emphasis>git-read-tree</emphasis>
complains loudly and fails without modifying anything).  In such
a case, you can simply continue doing what you were in the
middle of doing, and when your working tree is ready (i.e. you
have finished your work-in-progress), attempt the merge again.</simpara>
</simplesect>
</simplesect>
<simplesect id="_see_also">
<title>SEE ALSO</title>
<simpara><xref linkend="git-write-tree(1)"/>; <xref linkend="git-ls-files(1)"/>;
<xref linkend="gitignore(5)"/></simpara>
</simplesect>
<simplesect id="_author">
<title>Author</title>
<simpara>Written by Linus Torvalds &lt;<ulink url="mailto:torvalds@osdl.org">torvalds@osdl.org</ulink>&gt;</simpara>
</simplesect>
<simplesect id="_documentation">
<title>Documentation</title>
<simpara>Documentation by David Greaves, Junio C Hamano and the git-list &lt;<ulink url="mailto:git@vger.kernel.org">git@vger.kernel.org</ulink>&gt;.</simpara>
</simplesect>
<simplesect id="_git">
<title>GIT</title>
<simpara>Part of the <xref linkend="git(1)"/> suite</simpara>
</simplesect>
</article>
