{
    "content": [
        {
            "type": "text",
            "text": "# git-read-tree(1) (man)\n\n**Summary:** git-read-tree - Reads tree information into the index\n\n**Synopsis:** git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>]\n[-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n(--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])\n\n## Flags\n\n| Flag | Long | Arg | Description |\n|------|------|-----|-------------|\n| -m | — | — | Perform a merge, not just a read. The command will refuse to run if your index file has unmerged entries, indicating tha |\n| — | --reset | — | Same as -m, except that unmerged entries are discarded instead of failing. When used with -u, updates leading to loss of |\n| -u | — | — | After a successful merge, update the files in the work tree with the result of the merge. |\n| -i | — | — | Usually a merge requires the index file as well as the files in the working tree to be up to date with the current head  |\n| -n | --dry-run | — | Check if the command would error out, without updating the index or the files in the working tree for real. |\n| -v | — | — | Show the progress of checking files out. |\n| — | --trivial | — | Restrict three-way merge by git read-tree to happen only if there is no file-level merging required, instead of resolvin |\n| — | --aggressive | — | Usually a three-way merge by git read-tree resolves the merge for really trivial cases and leaves other cases unresolved |\n| — | --no-sparse-checkout | — | Disable sparse checkout support even if core.sparseCheckout is true. |\n| — | --empty | — | Instead of reading tree object(s) into the index, just empty it. |\n| -q | --quiet | — | Quiet, suppress feedback messages. <tree-ish#> The id of the tree object(s) to be read/merged. |\n\n## See Also\n\n- git-write-tree(1)\n- git-ls-files(1)\n- gitignore(5)\n- git-sparse-checkout(1)\n\n## Section Outline\n\n- **NAME** (2 lines)\n- **SYNOPSIS** (5 lines)\n- **DESCRIPTION** (10 lines)\n- **OPTIONS** (1 lines) — 11 subsections\n  - -m (3 lines)\n  - --reset (4 lines)\n  - -u (2 lines)\n  - -i (6 lines)\n  - -n, --dry-run (3 lines)\n  - -v (2 lines)\n  - --trivial (4 lines)\n  - --aggressive (30 lines)\n  - --no-sparse-checkout (2 lines)\n  - --empty (2 lines)\n  - -q, --quiet (5 lines)\n- **MERGING** (4 lines) — 4 subsections\n  - Single Tree Merge (6 lines)\n  - -a (2 lines)\n  - Two Tree Merge (73 lines)\n  - 3-Way Merge (111 lines)\n- **SPARSE CHECKOUT** (37 lines)\n- **SEE ALSO** (2 lines)\n- **GIT** (5 lines)\n\n## Full Content\n\n### NAME\n\ngit-read-tree - Reads tree information into the index\n\n### SYNOPSIS\n\ngit read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>]\n[-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n(--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])\n\n### DESCRIPTION\n\nReads the tree information given by <tree-ish> into the index, but does not actually update\nany of the files it \"caches\". (see: git-checkout-index(1))\n\nOptionally, it can merge a tree into the index, perform a fast-forward (i.e. 2-way) merge, or\na 3-way merge, with the -m flag. When used with -m, the -u flag causes it to also update the\nfiles in the work tree with the result of the merge.\n\nTrivial merges are done by git read-tree itself. Only conflicting paths will be in unmerged\nstate when git read-tree returns.\n\n### OPTIONS\n\n#### -m\n\nPerform a merge, not just a read. The command will refuse to run if your index file has\nunmerged entries, indicating that you have not finished previous merge you started.\n\n#### --reset\n\nSame as -m, except that unmerged entries are discarded instead of failing. When used with\n-u, updates leading to loss of working tree changes or untracked files or directories\nwill not abort the operation.\n\n#### -u\n\nAfter a successful merge, update the files in the work tree with the result of the merge.\n\n#### -i\n\nUsually a merge requires the index file as well as the files in the working tree to be up\nto date with the current head commit, in order not to lose local changes. This flag\ndisables the check with the working tree and is meant to be used when creating a merge of\ntrees that are not directly related to the current working tree status into a temporary\nindex file.\n\n#### -n, --dry-run\n\nCheck if the command would error out, without updating the index or the files in the\nworking tree for real.\n\n#### -v\n\nShow the progress of checking files out.\n\n#### --trivial\n\nRestrict three-way merge by git read-tree to happen only if there is no file-level\nmerging required, instead of resolving merge for trivial cases and leaving conflicting\nfiles unresolved in the index.\n\n#### --aggressive\n\nUsually a three-way merge by git read-tree resolves the merge for really trivial cases\nand leaves other cases unresolved in the index, so that porcelains can implement\ndifferent merge policies. This flag makes the command resolve a few more cases\ninternally:\n\n•   when one side removes a path and the other side leaves the path unmodified. The\nresolution is to remove that path.\n\n•   when both sides remove a path. The resolution is to remove that path.\n\n•   when both sides add a path identically. The resolution is to add that path.\n\n--prefix=<prefix>\nKeep the current index contents, and read the contents of the named tree-ish under the\ndirectory at <prefix>. The command will refuse to overwrite entries that already existed\nin the original index file.\n\n--index-output=<file>\nInstead of writing the results out to $GITINDEXFILE, write the resulting index in the\nnamed file. While the command is operating, the original index file is locked with the\nsame mechanism as usual. The file must allow to be rename(2)ed into from a temporary file\nthat is created next to the usual index file; typically this means it needs to be on the\nsame filesystem as the index file itself, and you need write permission to the\ndirectories the index file and index output file are located in.\n\n--[no-]recurse-submodules\nUsing --recurse-submodules will update the content of all active submodules according to\nthe commit recorded in the superproject by calling read-tree recursively, also setting\nthe submodules' HEAD to be detached at that commit.\n\n#### --no-sparse-checkout\n\nDisable sparse checkout support even if core.sparseCheckout is true.\n\n#### --empty\n\nInstead of reading tree object(s) into the index, just empty it.\n\n#### -q, --quiet\n\nQuiet, suppress feedback messages.\n\n<tree-ish#>\nThe id of the tree object(s) to be read/merged.\n\n### MERGING\n\nIf -m is specified, git read-tree can perform 3 kinds of merge, a single tree merge if only 1\ntree is given, a fast-forward merge with 2 trees, or a 3-way merge if 3 or more trees are\nprovided.\n\n#### Single Tree Merge\n\nIf only 1 tree is specified, git read-tree operates as if the user did not specify -m, except\nthat if the original index has an entry for a given pathname, and the contents of the path\nmatch with the tree being read, the stat info from the index is used. (In other words, the\nindex’s stat()s take precedence over the merged tree’s).\n\nThat means that if you do a git read-tree -m <newtree> followed by a git checkout-index -f -u\n\n#### -a\n\nThis is used to avoid unnecessary false hits when git diff-files is run after git read-tree.\n\n#### Two Tree Merge\n\nTypically, this is invoked as git read-tree -m $H $M, where $H is the head commit of the\ncurrent repository, and $M is the head of a foreign tree, which is simply ahead of $H (i.e.\nwe are in a fast-forward situation).\n\nWhen two trees are specified, the user is telling git read-tree the following:\n\n1. The current index and work tree is derived from $H, but the user may have local changes\nin them since $H.\n\n2. The user wants to fast-forward to $M.\n\nIn this case, the git read-tree -m $H $M command makes sure that no local change is lost as\nthe result of this \"merge\". Here are the \"carry forward\" rules, where \"I\" denotes the index,\n\"clean\" means that index and work tree coincide, and \"exists\"/\"nothing\" refer to the presence\nof a path in the specified commit:\n\nI                   H        M        Result\n-------------------------------------------------------\n0  nothing             nothing  nothing  (does not happen)\n1  nothing             nothing  exists   use M\n2  nothing             exists   nothing  remove path from index\n3  nothing             exists   exists,  use M if \"initial checkout\",\nH == M   keep index otherwise\nexists,  fail\nH != M\n\nclean I==H  I==M\n------------------\n4  yes   N/A   N/A     nothing  nothing  keep index\n5  no    N/A   N/A     nothing  nothing  keep index\n\n6  yes   N/A   yes     nothing  exists   keep index\n7  no    N/A   yes     nothing  exists   keep index\n8  yes   N/A   no      nothing  exists   fail\n9  no    N/A   no      nothing  exists   fail\n\n10 yes   yes   N/A     exists   nothing  remove path from index\n11 no    yes   N/A     exists   nothing  fail\n12 yes   no    N/A     exists   nothing  fail\n13 no    no    N/A     exists   nothing  fail\n\nclean (H==M)\n------\n14 yes                 exists   exists   keep index\n15 no                  exists   exists   keep index\n\nclean I==H  I==M (H!=M)\n------------------\n16 yes   no    no      exists   exists   fail\n17 no    no    no      exists   exists   fail\n18 yes   no    yes     exists   exists   keep index\n19 no    no    yes     exists   exists   keep index\n20 yes   yes   no      exists   exists   use M\n21 no    yes   no      exists   exists   fail\n\nIn all \"keep index\" cases, the index entry stays as in the original index file. If the entry\nis not up to date, git read-tree keeps the copy in the work tree intact when operating under\nthe -u flag.\n\nWhen this form of git read-tree returns successfully, you can see which of the \"local\nchanges\" that you made were carried forward by running git diff-index --cached $M. Note that\nthis does not necessarily match what git diff-index --cached $H would have produced before\nsuch a two tree merge. This is because of cases 18 and 19 --- if you already had the changes\nin $M (e.g. maybe you picked it up via e-mail in a patch form), git diff-index --cached $H\nwould have told you about the change before this merge, but it would not show in git\ndiff-index --cached $M output after the two-tree merge.\n\nCase 3 is slightly tricky and needs explanation. The result from this rule logically should\nbe to remove the path if the user staged the removal of the path and then switching to a new\nbranch. That however will prevent the initial checkout from happening, so the rule is\nmodified to use M (new tree) only when the content of the index is empty. Otherwise the\nremoval of the path is kept as long as $H and $M are the same.\n\n#### 3-Way Merge\n\nEach \"index\" entry has two bits worth of \"stage\" state. stage 0 is the normal one, and is the\nonly one you’d see in any kind of normal use.\n\nHowever, when you do git read-tree with three trees, the \"stage\" starts out at 1.\n\nThis means that you can do\n\n$ git read-tree -m <tree1> <tree2> <tree3>\n\n\nand you will end up with an index with all of the <tree1> entries in \"stage1\", all of the\n<tree2> entries in \"stage2\" and all of the <tree3> entries in \"stage3\". When performing a\nmerge of another branch into the current branch, we use the common ancestor tree as <tree1>,\nthe current branch head as <tree2>, and the other branch head as <tree3>.\n\nFurthermore, git read-tree has special-case logic that says: if you see a file that matches\nin all respects in the following states, it \"collapses\" back to \"stage0\":\n\n•   stage 2 and 3 are the same; take one or the other (it makes no difference - the same work\nhas been done on our branch in stage 2 and their branch in stage 3)\n\n•   stage 1 and stage 2 are the same and stage 3 is different; take stage 3 (our branch in\nstage 2 did not do anything since the ancestor in stage 1 while their branch in stage 3\nworked on it)\n\n•   stage 1 and stage 3 are the same and stage 2 is different take stage 2 (we did something\nwhile they did nothing)\n\nThe git write-tree command refuses to write a nonsensical tree, and it will complain about\nunmerged entries if it sees a single entry that is not stage 0.\n\nOK, this all sounds like a collection of totally nonsensical rules, but it’s actually exactly\nwhat you want in order to do a fast merge. The different stages represent the \"result tree\"\n(stage 0, aka \"merged\"), the original tree (stage 1, aka \"orig\"), and the two trees you are\ntrying to merge (stage 2 and 3 respectively).\n\nThe order of stages 1, 2 and 3 (hence the order of three <tree-ish> command-line arguments)\nare significant when you start a 3-way merge with an index file that is already populated.\nHere is an outline of how the algorithm works:\n\n•   if a file exists in identical format in all three trees, it will automatically collapse\nto \"merged\" state by git read-tree.\n\n•   a file that has any difference what-so-ever in the three trees will stay as separate\nentries in the index. It’s up to \"porcelain policy\" to determine how to remove the non-0\nstages, and insert a merged version.\n\n•   the index file saves and restores with all this information, so you can merge things\nincrementally, but as long as it has entries in stages 1/2/3 (i.e., \"unmerged entries\")\nyou can’t write the result. So now the merge algorithm ends up being really simple:\n\n•   you walk the index in order, and ignore all entries of stage 0, since they’ve already\nbeen done.\n\n•   if you find a \"stage1\", but no matching \"stage2\" or \"stage3\", you know it’s been\nremoved from both trees (it only existed in the original tree), and you remove that\nentry.\n\n•   if you find a matching \"stage2\" and \"stage3\" tree, you remove one of them, and turn\nthe other into a \"stage0\" entry. Remove any matching \"stage1\" entry if it exists too.\n.. all the normal trivial rules ..\n\nYou would normally use git merge-index with supplied git merge-one-file to do this last step.\nThe script updates the files in the working tree as it merges each path and at the end of a\nsuccessful merge.\n\nWhen you start a 3-way merge with an index file that is already populated, it is assumed that\nit represents the state of the files in your work tree, and you can even have files with\nchanges unrecorded in the index file. It is further assumed that this state is \"derived\" from\nthe stage 2 tree. The 3-way merge refuses to run if it finds an entry in the original index\nfile that does not match stage 2.\n\nThis is done to prevent you from losing your work-in-progress changes, and mixing your random\nchanges in an unrelated merge commit. To illustrate, suppose you start from what has been\ncommitted last to your repository:\n\n$ JC=`git rev-parse --verify \"HEAD^0\"`\n$ git checkout-index -f -u -a $JC\n\n\nYou do random edits, without running git update-index. And then you notice that the tip of\nyour \"upstream\" tree has advanced since you pulled from him:\n\n$ git fetch git://.... linus\n$ LT=`git rev-parse FETCHHEAD`\n\n\nYour work tree is still based on your HEAD ($JC), but you have some edits since. Three-way\nmerge makes sure that you have not added or modified index entries since $JC, and if you\nhaven’t, then does the right thing. So with the following sequence:\n\n$ git read-tree -m -u `git merge-base $JC $LT` $JC $LT\n$ git merge-index git-merge-one-file -a\n$ echo \"Merge with Linus\" | \\\ngit commit-tree `git write-tree` -p $JC -p $LT\n\n\nwhat you would commit is a pure merge between $JC and $LT without your work-in-progress\nchanges, and your work tree would be updated to the result of the merge.\n\nHowever, if you have local changes in the working tree that would be overwritten by this\nmerge, git read-tree will refuse to run to prevent your changes from being lost.\n\nIn other words, there is no need to worry about what exists only in the working tree. When\nyou have local changes in a part of the project that is not involved in the merge, your\nchanges do not interfere with the merge, and are kept intact. When they do interfere, the\nmerge does not even start (git read-tree complains loudly and fails without modifying\nanything). In such a case, you can simply continue doing what you were in the middle of\ndoing, and when your working tree is ready (i.e. you have finished your work-in-progress),\nattempt the merge again.\n\n### SPARSE CHECKOUT\n\n\"Sparse checkout\" allows populating the working directory sparsely. It uses the skip-worktree\nbit (see git-update-index(1)) to tell Git whether a file in the working directory is worth\nlooking at.\n\ngit read-tree and other merge-based commands (git merge, git checkout...) can help\nmaintaining the skip-worktree bitmap and working directory update.\n$GITDIR/info/sparse-checkout is used to define the skip-worktree reference bitmap. When git\nread-tree needs to update the working directory, it resets the skip-worktree bit in the index\nbased on this file, which uses the same syntax as .gitignore files. If an entry matches a\npattern in this file, skip-worktree will not be set on that entry. Otherwise, skip-worktree\nwill be set.\n\nThen it compares the new skip-worktree value with the previous one. If skip-worktree turns\nfrom set to unset, it will add the corresponding file back. If it turns from unset to set,\nthat file will be removed.\n\nWhile $GITDIR/info/sparse-checkout is usually used to specify what files are in, you can\nalso specify what files are not in, using negate patterns. For example, to remove the file\nunwanted:\n\n/*\n!unwanted\n\n\nAnother tricky thing is fully repopulating the working directory when you no longer want\nsparse checkout. You cannot just disable \"sparse checkout\" because skip-worktree bits are\nstill in the index and your working directory is still sparsely populated. You should\nre-populate the working directory with the $GITDIR/info/sparse-checkout file content as\nfollows:\n\n/*\n\n\nThen you can disable sparse checkout. Sparse checkout support in git read-tree and similar\ncommands is disabled by default. You need to turn core.sparseCheckout on in order to have\nsparse checkout support.\n\n### SEE ALSO\n\ngit-write-tree(1); git-ls-files(1); gitignore(5); git-sparse-checkout(1);\n\n### GIT\n\nPart of the git(1) suite\n\n\n\nGit 2.34.1                                   02/26/2026                             GIT-READ-TREE(1)\n\n"
        }
    ],
    "structuredContent": {
        "command": "git-read-tree",
        "section": "1",
        "mode": "man",
        "summary": "git-read-tree - Reads tree information into the index",
        "synopsis": "git read-tree [[-m [--trivial] [--aggressive] | --reset | --prefix=<prefix>]\n[-u | -i]] [--index-output=<file>] [--no-sparse-checkout]\n(--empty | <tree-ish1> [<tree-ish2> [<tree-ish3>]])",
        "flags": [
            {
                "flag": "-m",
                "long": null,
                "arg": null,
                "description": "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."
            },
            {
                "flag": "",
                "long": "--reset",
                "arg": null,
                "description": "Same as -m, except that unmerged entries are discarded instead of failing. When used with -u, updates leading to loss of working tree changes or untracked files or directories will not abort the operation."
            },
            {
                "flag": "-u",
                "long": null,
                "arg": null,
                "description": "After a successful merge, update the files in the work tree with the result of the merge."
            },
            {
                "flag": "-i",
                "long": null,
                "arg": null,
                "description": "Usually a merge requires the index file as well as the files in the working tree to be 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."
            },
            {
                "flag": "-n",
                "long": "--dry-run",
                "arg": null,
                "description": "Check if the command would error out, without updating the index or the files in the working tree for real."
            },
            {
                "flag": "-v",
                "long": null,
                "arg": null,
                "description": "Show the progress of checking files out."
            },
            {
                "flag": "",
                "long": "--trivial",
                "arg": null,
                "description": "Restrict three-way merge by git read-tree 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."
            },
            {
                "flag": "",
                "long": "--aggressive",
                "arg": null,
                "description": "Usually a three-way merge by git read-tree 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 resolve a few more cases internally: • when one side removes a path and the other side leaves the path unmodified. The resolution is to remove that path. • when both sides remove a path. The resolution is to remove that path. • when both sides add a path identically. The resolution is to add that path. --prefix=<prefix> Keep the current index contents, and read the contents of the named tree-ish under the directory at <prefix>. The command will refuse to overwrite entries that already existed in the original index file. --index-output=<file> Instead of writing the results out to $GITINDEXFILE, 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. --[no-]recurse-submodules Using --recurse-submodules will update the content of all active submodules according to the commit recorded in the superproject by calling read-tree recursively, also setting the submodules' HEAD to be detached at that commit."
            },
            {
                "flag": "",
                "long": "--no-sparse-checkout",
                "arg": null,
                "description": "Disable sparse checkout support even if core.sparseCheckout is true."
            },
            {
                "flag": "",
                "long": "--empty",
                "arg": null,
                "description": "Instead of reading tree object(s) into the index, just empty it."
            },
            {
                "flag": "-q",
                "long": "--quiet",
                "arg": null,
                "description": "Quiet, suppress feedback messages. <tree-ish#> The id of the tree object(s) to be read/merged."
            }
        ],
        "examples": [],
        "see_also": [
            {
                "name": "git-write-tree",
                "section": "1",
                "url": "https://www.chedong.com/phpMan.php/man/git-write-tree/1/json"
            },
            {
                "name": "git-ls-files",
                "section": "1",
                "url": "https://www.chedong.com/phpMan.php/man/git-ls-files/1/json"
            },
            {
                "name": "gitignore",
                "section": "5",
                "url": "https://www.chedong.com/phpMan.php/man/gitignore/5/json"
            },
            {
                "name": "git-sparse-checkout",
                "section": "1",
                "url": "https://www.chedong.com/phpMan.php/man/git-sparse-checkout/1/json"
            }
        ],
        "section_outline": [
            {
                "name": "NAME",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "SYNOPSIS",
                "lines": 5,
                "subsections": []
            },
            {
                "name": "DESCRIPTION",
                "lines": 10,
                "subsections": []
            },
            {
                "name": "OPTIONS",
                "lines": 1,
                "subsections": [
                    {
                        "name": "-m",
                        "lines": 3,
                        "flag": "-m"
                    },
                    {
                        "name": "--reset",
                        "lines": 4,
                        "long": "--reset"
                    },
                    {
                        "name": "-u",
                        "lines": 2,
                        "flag": "-u"
                    },
                    {
                        "name": "-i",
                        "lines": 6,
                        "flag": "-i"
                    },
                    {
                        "name": "-n, --dry-run",
                        "lines": 3,
                        "flag": "-n",
                        "long": "--dry-run"
                    },
                    {
                        "name": "-v",
                        "lines": 2,
                        "flag": "-v"
                    },
                    {
                        "name": "--trivial",
                        "lines": 4,
                        "long": "--trivial"
                    },
                    {
                        "name": "--aggressive",
                        "lines": 30,
                        "long": "--aggressive"
                    },
                    {
                        "name": "--no-sparse-checkout",
                        "lines": 2,
                        "long": "--no-sparse-checkout"
                    },
                    {
                        "name": "--empty",
                        "lines": 2,
                        "long": "--empty"
                    },
                    {
                        "name": "-q, --quiet",
                        "lines": 5,
                        "flag": "-q",
                        "long": "--quiet"
                    }
                ]
            },
            {
                "name": "MERGING",
                "lines": 4,
                "subsections": [
                    {
                        "name": "Single Tree Merge",
                        "lines": 6
                    },
                    {
                        "name": "-a",
                        "lines": 2,
                        "flag": "-a"
                    },
                    {
                        "name": "Two Tree Merge",
                        "lines": 73
                    },
                    {
                        "name": "3-Way Merge",
                        "lines": 111
                    }
                ]
            },
            {
                "name": "SPARSE CHECKOUT",
                "lines": 37,
                "subsections": []
            },
            {
                "name": "SEE ALSO",
                "lines": 2,
                "subsections": []
            },
            {
                "name": "GIT",
                "lines": 5,
                "subsections": []
            }
        ]
    }
}