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

Re: Git add documentation error

From
Junio C Hamano <gitster@pobox.com>
Date
Oct 27, 2022, 18:44 UTC
Message-ID
<xmqq1qqtm7i0.fsf@gitster.g>
In-Reply-To
<CAB9Jk9BC1PVxuuNggZuDh1OMe3kdTvCzwkoo7Bkm35nRqAaL4g@mail.gmail.com>
Angelo Borsotti <angelo.borsotti@gmail.com> writes:
Show 21 quoted lines
> Hi
>
> I have displayed the contents of the commits with the command you indicate,
> but they still look much the same:
>
> D:\gittest>git ls-tree -r --name-only 91ef45d
> C1.java
> C2.java
> C3.java
> C4.java
> C5.java
>
>
> D:\gittest>git ls-tree -r --name-only 8ec0c2f
> C1.java
> C2.java
> C3.java
> C4.java
> C5.java
>
> I thought that in the second one I would see only the changed file.

Git tracks contents. What it means is that you should stop thinking in terms of "changed files". Each commit (and its tree) represents a complete snapshot. Back when you created 8ec0c2f, did you or did you not have C1.java? You said you did not modify it, so I assume you did have it. As a commit is a complete snapshot of the contents, when you list the contents of 8ec0c2f, it should be included.

To put it another way, if you "git checkout --detach 8ec0c2f" in order to go back to the state you were back when you created that commit, do you or do you not want to see C1.java and C2.java? In order to be able to reproduce the then-current state, of course you do want to see, and because each commit is a complete snapshot, you can see C1 and C2.

"git add C1.java" is a way to make sure that, if you make a commit, the resulting "snapshot" includes the current contents stored in the C1.java file as you have on the filesystem at the time of issuing "git add" command. If you made or did not make changes to C1.java before running "git add C1.java" is immaterial. We may optimize out and make it a no-op if we know you did not make any changes, but it does not change the fact that the resulting commit will store, and more importantly, you will be able to recover, the then-current contents of C1.java out of that commit.

Previous: Angelo BorsottiNext: Angelo Borsotti
Message 7 of 8 in “Git add documentation error”
  1. Angelo BorsottiOct 27, 2022
  2. Philip OakleyOct 27, 2022
  3. Philip OakleyOct 27, 2022
  4. Angelo BorsottiOct 27, 2022
  5. Junio C HamanoOct 27, 2022
  6. Angelo BorsottiOct 27, 2022
  7. Junio C HamanoOct 27, 2022
  8. Angelo BorsottiOct 27, 2022

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.