jsongitgitattributes

Git. Ignore changes in a file only if certain changes have made


My project has a large number of json configuration files. Some of them change their content in accordance with development tasks (new fields are added, old values ​​are changed), but absolutely all of them change 4 fields with each new change: 2 id and 2 dates.(this is done automatically and I cannot influence this in any way)

I need those files for which only 2 ids and 2 dates have changed not to be included in changes (like "assume unchanged"). Changes should only include those files that have changed something other than 2 ids and 2 dates.changes that i need to ignore

Now I have doubts that this is even possible to do using git.

I read about clear and smudge filters, but as I understand it, it can substitute a regex expression so that it is automatically replaced with some constant value, but I need the file to be completely ignored when committing by condition.


Solution

  • Your doubts are correct. Git doesn't provide a way to ignore tracked files.

    The FAQ mentions this:

    Git doesn’t provide a way to do this. The reason is that if Git needs to overwrite this file, such as during a checkout, it doesn’t know whether the changes to the file are precious and should be kept, or whether they are irrelevant and can safely be destroyed. Therefore, it has to take the safe route and always preserve them.

    It’s tempting to try to use certain features of git update-index, namely the assume-unchanged and skip-worktree bits, but these don’t work properly for this purpose and shouldn’t be used this way.

    If you're working with tools that automatically change these files, perhaps you should move the actual files elsewhere and copy them into an ignored location where the tools will look for them, possibly with a script.

    You're correct that a smudge or clean filter won't work here because when you're using the clean filter, you don't know the current state (or, for that matter, the file name). Thus, you can't intrinsically know what output to produce, since your goal is to not modify certain fields at all.