1

I saw many questions regarding this issue but I didn't saw appropriate solution for this problem. lets say that I have a template.xml file in remote and I want all users to get it when they first cloning the repository but when they changing it will be ignored. I assume-unchanged but it is only locally and I would like to have a solution for all users

  • 3
    This is not possible really, afaik. Instead commit a file like `template-example.xml` and ignore `template.xml` – evolutionxbox Jun 28 '22 at 14:04
  • Does this answer your question? [Can I 'git commit' a file and ignore its content changes?](https://stackoverflow.com/questions/3319479/can-i-git-commit-a-file-and-ignore-its-content-changes) – 1615903 Jun 29 '22 at 03:42
  • @1615903 The answer to that question is not correct according to the Git documentation, and suggesting it as an answer is therefore not helpful. – bk2204 Jun 29 '22 at 22:02
  • @bk2204 there's plenty of answers in that question, the one suggesting use of template file is a solution to OP's problem. – 1615903 Jun 30 '22 at 03:35

2 Answers2

1

Your question is a specific case of "How do I ignore tracked files in Git?", which is answered in the Git FAQ:

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.

The FAQ also mentions an approach for template or configuration files:

[I]t can often be helpful to have a file checked into the repository which is a template or set of defaults which can then be copied alongside and modified as appropriate. This second, modified file is usually ignored to prevent accidentally committing it.

You can use some sort of setup script to copy it into place if it's missing.

bk2204
  • 64,793
  • 6
  • 84
  • 100
  • Thanks! my case it is a project, so coping it cause change also in solution file that sometime developer forget to undo the change there – Ori Spokoini Jun 29 '22 at 14:32
-2

If I understand your problem correctly, you could commit and push the template.xml file to your remote and then add a .gitignore file. The template.xml file will then be available on the remote for all, and if you clone the repository you will also get the file. But if you change the file locally it will not be recognized by git.

Basically after you add the .gitignore file you can't edit the file on the remote anymore.

gitignore file docs: https://git-scm.com/docs/gitignore

kaffarell
  • 639
  • 5
  • 16
  • That's not accurate. Putting a file in `.gitignore` doesn't prevent Git from seeing it as tracked or noticing modifications to it. – bk2204 Jun 28 '22 at 21:12