Unique identifier for file in Git Repository

2019-08-09 16:45发布

问题:

There exists an unique identifier for files in a Git Repository? This unique identifier must be the same after some commits (modifying file), after reneming and moving that file.

There it exists?

I tried git ls-files --debug but I didn't find an unique identifier with the characteristics listed above.

回答1:

The only unique identifier for a file ("blob") within a git repo is its SHA-1, but that is a checksum of the file's contents (plus the fact of it being a file/blob). It remains constant only while the file's contents remain unchanged. It does not depend on the file's name, but it does depend on its contents, so modifying the file will change the SHA-1.

You could make up your own unique ID but you would also have to do your own tracking, mapping the ID to the current tree-relative name (or git-name, i.e., SHA-1) and changing it when the file is renamed (or modified, respectively).



回答2:

No, there isn't one. If you modify it, it's a different file from Git's point of view.