threads / discuss / 62529

[question] When an identical file is added

Subject: [question] When an identical file is added

## tl;dr

2 messages between Nov 24, 2024 and Nov 26, 2024.

replies: 1people: 2as markdown or json

A bughunter· Nov 24, 2024, 02:24 UTC · lore
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA512
[question] When an identical file is added

While objects are immutable, if I do mixed mode reset switching to an old-commit under HEAD then 'add' the same files again Q. Will it keep two copies as objects? A. No because when an identical file is added the object file is identical. These become tree entries pointing to the same object. 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_

from A_bughunter@proton.me
Sent with Proton Mail secure email.
-----BEGIN PGP SIGNATURE-----
Version: ProtonMail

wnUEARYKACcFgmdCjlAJkKkWZTlQrvKZFiEEZlQIBcAycZ2lO9z2qRZlOVCu 8pkAADfCAP0fuC5Rc0gHALam7YkQHjZg54dGrnEUa6tZJZpSzNuiBgEA5c0a G+ZwfchdHP0gMPwHx4+TBL6LhW7ogj7u2IubQA4= =xVzR -----END PGP SIGNATURE-----

Konstantin Ryabitsev· Nov 26, 2024, 15:19 UTC · re: A bughunter · lore

Re: [question] When an identical file is added

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

← back to recent threads