2

Is there a way to gitignore files for a selected duration?

I'm aware that we can use git update-index --assume-unchanged as mentioned here. But

  • using this method, I need to manually set & unset this, so there's a chance that I might forget to unset.
  • more importantly, its not a recommended approach as documented in git-scm - (thanks to bk2204's comment below for the reference)

So, I'm looking for a way to automatically start including the files in git changes.

The use-case here is that I use Obsidian which is a Personal Knowledge Management application. And this applications saves the configuration setting of the UI layout in a json format anytime I open a file. It keeps track of

  1. all the currently active files and top 10 recently opened files which constantly change and I don't want that to trigger the git change
  2. the layout of the application (similar to VS Code workspace layouts) - which I need to save whenever its changed

So I don't want to include them in git changes regularly, but I want to include them at-least every 2 days to ensure that any layout changes are included.

How do I achieve this?


To add more clarity based on the comments:

  • The challenge is not to commit them on a regular basis, it is to ignore the files whenever I commit in the 2 days span. The reason for that is that I have been using git add . or git commit -a -m 'My commit comments' as a practice for a very long time. And it has become second nature to me at this point. And I want git to ignore these files when I do that within the span of 2 days.

    So an ideal solution would mean that I don't have to keep a lookout for these files every time I commmit and manually ignore them and then manually add them the 2nd day - since they will be automatically un-ignored the 2nd day

  • The use-case I mentioned above is just one example, there can be other use-cases like that for other kind of tools. So i'm looking for a solution using git workflow.

Gangula
  • 5,193
  • 4
  • 30
  • 59
  • Write a script that commits the files every two days? – matt Jun 10 '23 at 13:56
  • 2
    [The Git FAQ is very clear](https://git-scm.com/docs/gitfaq#ignore-tracked-files) that you should not use `git update-index` to ignore changes to tracked files because it doesn't work. – bk2204 Jun 10 '23 at 14:16
  • @matt, because I have been using `git add .` or `git commit -a -m 'My commit comments'` as a practice for a very long time. And it has become second nature to me at this point. And I want git to ignore these files when I do that. – Gangula Jun 10 '23 at 14:19
  • @bk2204, thank you for the reference. Something new I learned. It's also in favor of my question. – Gangula Jun 10 '23 at 14:20

1 Answers1

0

So, your toolkit is saving config and activity in a single state file, and you only want to record the config, just the chosen settings?

The Git way to handle this is with a clean filter. There's a json munger jq that can do basically anything you want to json.

So say it's keeping recent files in a "history" array at the object top level, the way to not bother recording any of it is

jq '.+{history:[]}'

which just overwrites the input object's (.) history with an empty array. It takes a bit to get into the syntax, you're basically writing an expression that constructs the result from pieces of the input, you learn to recognize which parts of the expression refer to the input and which are things you're adding or doing to it.

So set a filter on that file so when you add it you only add the bits you want to save. Filters are a file attribute, so (making up all the names here) in .gitattributes

/.obsidian-cfg.json filter=strip-obsidian-history

and in your git config, either the repo or user-wide one

git config filter.strip-obsidian-history.clean "jq '.+{history:[]}'"
git config filter.strip-obsidian-history.smudge cat

Now: I haven't looked up the structure obsidian actually uses, I don't know if it's really an array, you'll have to adapt this.

jthill
  • 55,082
  • 5
  • 77
  • 137
  • Wow, this sounds interesting. First time coming across something like this. But am I understanding that this overwrite the json file? so if the json has a `history["abc", "efg"]`, will this filter re-write it to `history[]`? – Gangula Jun 10 '23 at 18:34
  • Only in the copy added to the repository -- so if only the history changed, that won't alter the commit. If you check out the file with that `cat` filter, yes, it'll check out the empty-history version, but you could also write your filters to restore it from the existing file or possibly a saved cache.There's reasons for the xdg separation of config and state, this is one. – jthill Jun 10 '23 at 20:56
  • Understood. Although this is helpful and a really useful resource. I would prefer a method to periodically include these files rather than configuring git with filter for each of the item I want to ignore. Because I might end up spending more time on adding the filter for multiple criteria. – Gangula Jun 10 '23 at 21:27