git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Can I checkout a single file without altering index?

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 15, 2010, 23:22 UTC
Message-ID
<7viq13avn0.fsf@alter.siamese.dyndns.org>
In-Reply-To
<i9a6kn$d7o$1@dough.gmane.org>
"Neal Kreitzinger" <neal@rsss.com> writes:
> FWIW, my understanding of the index is that it is the middle-man for moving 
> things from your work-tree to the object-store AND for moving things from 
> the object-store to your work-tree.

While there is nothing technically incorrect in the above, it probably is easier to explain/understand why it works in the direction from the object store and to the working tree if you state it this way instead:

  The index is where you build the contents for your next commit.
   - You build your next commit starting from the current commit,
     so when you do "checkout" from the object store, both the
     index and the working tree are populated with the blob.
   - You then modify that state in your working tree, hopefully
     testing, reviewing, and thinking about your change while
     doing so.
   - There may be good changes, not good enough changes, and
     perhaps only for debugging changes in your working tree.  You
     can decide to make commits out of only the good bits, leaving
     others uncommitted.  You add only good bits to your next
     commit, and the command to do so is naturally called "git
     add" (and "git add -p" for add changes to a file partially by
     picking patch hunks).
   - There may be a time where you regret that some changes you
     made to your working tree are not good, and want to start
     over.  You may even do so after you added some changes to
     the index (i.e. your next commit).  By checking the path
     out of the current commit would give you the original
     version of the path both in the index and in the working
     tree file to help you start over.
   - After making that commit, you will keep working to create
     the commit next to that commit you created.  To help you
     work incrementally, the index at that point contains what
     you committed.
 
> However, there is an option in git-commit to copy files directly from the 
> working-tree to the object-store by totally bypassing the index, but no one 
> seems to do this or recommend doing this as normative practice.
This is wrong.  People do this all the time with "git commit $path".
What happens behind the scene is:
 - git prepares a temporary index that matches the contents of the
   current commit;
 - the contents for $path from the working tree is then added to that
   temporary index;
 - a new commit is written out of that temporary index; and
 - the $path is also added to the real index (this is a very important
   detail---otherwise the next commit will lose the change to $path).

Notice that in no step the index is really bypassed. Everything literally goes through the index.

If you want to bypass the index, you can do so with cat-file or show; it just is not a useful operation in a normal workflow of building the next commit on top of the current one, and that is the only reason why there is no option such as "checkout --no-index HEAD~47 path". If somebody can write a convincing use case that shows why it is useful, such an option shouldn't be very hard to add. But I don't think of any. For example, this is not it:

    I start from a clean slate and start working.
    $ git checkout
    $ edit; git diff; compile; test; git add path ;# repeat
    At this point I have some cooked contents added for the next commit
    in the index for path.  But I realize that the contents of that path
    in another branch might be even better.  But I do not want to lose
    the state I arrived at, which might be better than that alternative.
    I cannot decide, so I'll keep that in the index for now.
    $ git checkout --no-index the-other-branch path
    $ edit; compile; test; ...
    case I. yes the other one indeed is better
    $ git add path
    case II. no the other one is inferiour
    $ git checkout path ;# out of index

While superficially this looks promising, this is unwieldy. For one thing, you cannot easily check what you changed anymore, as "git diff path" would show the difference between the index, i.e. a version that was modified in a different way from the current version, and the working tree, i.e. another version that was modified starting from a totally different version in the-other-branch.

Previous: Andreas SchwabNext: Jonathan Nieder
Message 15 of 16 in “Can I checkout a single file without altering index?”
  1. Christian HalstrickOct 12, 2010
  2. Michael J GruberOct 12, 2010
  3. Stefan NaeweOct 12, 2010
  4. Jared HanceOct 13, 2010
  5. Alex RiesenOct 12, 2010
  6. Tomas CarneckyOct 12, 2010
  7. Christian HalstrickOct 14, 2010
  8. Neal KreitzingerOct 15, 2010
  9. Jonathan NiederOct 15, 2010
  10. Jeff KingOct 15, 2010
  11. Jonathan NiederOct 15, 2010
  12. Jeff KingOct 15, 2010
  13. Neal KreitzingerOct 15, 2010
  14. Andreas SchwabOct 15, 2010
  15. Junio C HamanoOct 15, 2010
  16. Jonathan NiederOct 15, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.