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

Re: [question] When an identical file is added

From
Konstantin Ryabitsev <konstantin@linuxfoundation.org>
Date
Nov 26, 2024, 15:19 UTC
Message-ID
<ortjg2pjq6eb6ruz72mcalemqgjr7lqoleqxpiqlghzu3ibwzj@rhvqxlpnkcl2>
In-Reply-To
<lSW86a4L1xWHfSTWIq732o26YCBCIN_B0rd6B1bCP8VIAY8d1V9pD1WEcCyW7Wyw2T14VVYka5XMTwJGFZEKcovBYIgOBUO5lbPWQ44cIfM=@proton.me>
On Sun, Nov 24, 2024 at 02:24:19AM +0000, A bughunter wrote:
> Q. May this overwrite the same object file because it is the same file? or because it is immutable will it reuse the old object file?
> A. When an identical file is added the old object shall be _ANSWER_HERE_

The file contents are represented as a "blob" inside the git repository, so when you add a file with identical contents, the existing blob will be reused. The file metadata (permissions, name, timestamp, etc) will be recorded as part of the tree information. If the tree information is exactly identical to an existing tree in the repository, then that tree object will also be reused.

Basically, for any object that it operates on, git will calculate the checksum. If it then finds an identical checksum already present in the tree, git will just use that existing object instead.

Reusing objects happens all the time in git. For example, imagine the scenario:
0. you have an existing repo
1. you add a file foo.c, and git commit
2. you change your mind and delete foo.c, and git commit
3. you decide you want foo.c after all, re-add it, and git commit

All tree and blob objects are identical in steps 0,2, and 1,3. However, commit objects will be different in all 4 states.

-K
Previous: A bughunter
Message 2 of 2 in “[question] When an identical file is added”
  1. A bughunterNov 24, 2024
  2. Konstantin RyabitsevNov 26, 2024

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.