Store custom metadata against individual files in a Git repository

4.7k views Asked by At

I'm working with a Git repository and would like to define custom properties to apply to each file in the repository. These properties could then be updated on each commit. Ideally the properties should be easily searchable and eventually exportable.

Is there an easy way to do this in Git? I've looked into Git notes, but as far as I can tell that's per commit not per file.

Thanks.

2

There are 2 answers

0
max630 On

I'm afraid there is no built-in functionality.

You could put attributes with custom names to .gitattributes but probably it would not give much benefit compared to just placing the data to one or several files in more mature format like json or xml and commiting those files alongside your project.

0
1615903 On

You can in fact use git notes:

Adds, removes, or reads notes attached to objects, without touching the objects themselves.

Objects include single files too. As explained in this answer, you would list the files in your current HEAD with git ls-tree HEAD, and then add a note with git notes add -m "Some text, key-value pairs maybe?" <hash of the file>. To view the notes later, use git notes show <hash of the file>.

The obvious downside is that when you change the file contents, the hash changes. You would have to write your own scripts to traverse the file history and print all the notes.