-2

I have a file config.txt that is tracked by git, it is downloaded on git clone, but I don't want anybody changing and committing it i.e. if someone changes that file I don't want their changes to be tracked thus committed & accidentally pushed.

I still want to be able to add the potential changes of config.txt later on, but in an explicit way(could be git add -f config.txt or something else).

Also an extra step for doing this is not desirable so git update-index --skip-worktree or git update-index --assume-unchanged is not a good enough solution as it is prone to human error.

Is it possible to do so in git?

Jacob Krieg
  • 2,834
  • 15
  • 68
  • 140
  • 1
    We have config files that work like that. Our approach is: just don't accidentally `add` that file to any commits. Also I keep my version of the file in a private branch so if it ever does get overridden by a pull, I can restore it. – matt Jun 09 '21 at 08:40
  • @matt this is indeed the solution we are currently using too, however, there are some people in this world and for them we need a dumb-safeguard... – Jacob Krieg Jun 09 '21 at 08:44
  • 2
    Well, https://stackoverflow.com/questions/3319479/can-i-git-commit-a-file-and-ignore-its-content-changes – matt Jun 09 '21 at 08:51
  • @matt This is a good solution, however I would prefer something that does not need an extra step and can be configured at repository leve. – Jacob Krieg Jun 09 '21 at 09:29
  • Well the collected wisdom on this topic is on that page. I suggest this is just a duplicate of that. – matt Jun 09 '21 at 09:31
  • @matt Since it does not solve the problem of configuring this behavior in the repository level i.e. w/o having to do an extra step, I believe it is not a duplicate. The answers on that page don't answer the question described. – Jacob Krieg Jun 09 '21 at 09:46
  • 1
    Because there is no better answer. Git has not magically changed in the meantime. – matt Jun 09 '21 at 19:32

2 Answers2

1

You cannot ignore changes to a tracked files in Git. The Git FAQ explains:

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 your goal is to work with a configuration file, then the best thing to do is add an example or template file and then either have the user copy it into place or have a script create the appropriate file. You should then ignore the location of the actual configuration file and only check in the example or the template.

bk2204
  • 64,793
  • 6
  • 84
  • 100
0

One possible solution to that problem is to "skip" tracking the file after the repository is cloned:

git update-index --skip-worktree <path-name>

The downside is that this requires an additional step from all users after cloning the repo, so you could add that in your README

Ehab Ibrahim
  • 515
  • 3
  • 10