0

There are dozens of questions regarding the problem, that files listed in .gitignore, but I haven't found a solution among these yet.

I have a repo with several config files. But these config-files are only examples. They should be cloned to local, but if changed locally not be written back. They should only be written to the repo, if I add them via git add <filename> instead of git add *.

So far this works locally, if I use git update-index --skip-worktree <path-name>. git update-index --assume-unchanged [path] should work as well, but I didn't try it yet. Once I do this, everything works as expected.

But when I clone the repo to a new machine and change this file, changes are tracked and written back, when using git add *. The expected behaviour would be, that the file is copied to local, but never written back as long as it is mentioned in the .gitignore file.

I'm sure, there is a correct way to do this and I'm doing it wrong, but how?

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
MaestroGlanz
  • 321
  • 3
  • 12
  • 2
    `git update-index` works _per repo_, it's not something that can be pushed/cloned/fetched. – eftshift0 Aug 03 '22 at 15:26
  • @eftshift0 That's clear. I stop to track the file on the local repo, where it originated from. But why is it tracked after cloning, though in the .gitignore-list? Or how can I tell the main repo to not track it in clones? I could protect files, but this results problably in an error when trying to push. – MaestroGlanz Aug 03 '22 at 15:30
  • `.gitignore` never actually causes any committed file to be ignored, ever. (It can't by design.) If you want a file to not be in a commit, don't put it in the commit. If you're building a new commit by starting from an existing commit, and the existing commit has the file, remember that you must explicitly *remove* the file. Don't put configuration files into Git either; see [this FAQ entry](https://git.wiki.kernel.org/index.php/Git_FAQ#How_do_I_tell_Git_to_ignore_tracked_files.3F). – torek Aug 03 '22 at 15:53
  • But if I remove them (remove cached), they are removed in the remote repo also, which I don't want. – MaestroGlanz Aug 03 '22 at 15:57
  • From this FAQ, I have to conclude, that it's not possible, as I want it to be. In my opinion a bug, not a feature. – MaestroGlanz Aug 03 '22 at 15:59
  • What you can do is to have a template config file committed into Git, say `my-config.template`, which each user copies to the active config file name after cloning a repo. The active config file, since it won't be tracked, will be ignored by the `.gitignore` and changes to it will never be committed. When you want to take one specific change and recommend it to everyone, do it in the template too and commit that. – joanis Aug 03 '22 at 18:06
  • And that is in fact what is recommended in the Git FAQ: https://git-scm.com/docs/gitfaq#ignore-tracked-files – joanis Aug 03 '22 at 18:10
  • I've written up [this relevant answer](https://stackoverflow.com/a/70957498/3216427) to a related (but not duplicate IMO) question. Ignore the beginning of it, just pay attention to the section titled "The caveats, or why you should probably not do this". – joanis Aug 03 '22 at 18:13
  • Still not a fan. It's in my opinion not a good solution, just the least bad. – MaestroGlanz Aug 03 '22 at 18:13

1 Answers1

0

Files that are tracked by Git are not affected by .gitignore. Your question is a general case of the question, “How do I ignore changes to tracked files?” which is answered in the Git FAQ roughly as, “you can't”. The FAQ goes on to say:

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.

The Git FAQ recommends using a template or example file that is tracked and filling it our or copying it into its intended destination, which is ignored and not tracked, using a script if necessary.

bk2204
  • 64,793
  • 6
  • 84
  • 100