9

Is there a way in git to have the result of git update-index --assume-unchanged FILE_NAME by default on a given set of files? For example, a git config file that lists files whose changes are not tracked by default, so that we don't have to run the command after we clone the repo? The files would exist in the repository in a default format, be pulled by the developers, and whatever changes they make to the file wouldn't be tracked nor overwritten by posterior pulls.

For example: I have a web.config file with a placeholder for connection strings that are used by the project. Ideally, the developer would clone the repo and replace those placeholders with connection strings pointing to their local resources. No need to run any commands. That way, these changes would be preserved in posterior pulls, unless the format of the web.config file changed in the repo, and their local changes wouldn't be pushed to the repo.

Is this achievable in git with a config file? Or do I always have to run the command for the "set of files to assume unchanged"?

Heitor Castro
  • 833
  • 6
  • 9
  • 3
    The short answer is "no" and the slightly longer answer is "don't do that, but if you must, you probably want `--skip-worktree`". (What you should do instead, in general, is *ignore* those files entirely, never commit them, and instead have the repository contain "sample" or "default" files that, instead of ignoring, an install script copies-and-changes-if-appropriate. You'd need this install script to run `git update-index`, so you can have it copy-and-tweak instead.) – torek Sep 21 '17 at 21:22

1 Answers1

3

A better approach (than trying to twek the index, with assume-unchanged or skip-worktree) is to not track those files at all, but to generate them (and make sure they are in the .gitignore, that is not tracked and ignored)

That way, you can modify them locally at will, without having to manage their Git index state at all (since they are not tracked)

For that, you version and track:

  • a file.tpl (template file with placeholder values).
  • a script able to take a template file, and produce the actual file (which remains untracked, private)
  • a .gitignore which ignores the resulting generated file
  • a .gitattribute declaring a smudge content filter (see below)

The generation of the file is automated through a content filter driver, using a .gitattributes declaration.

https://i.stack.imgur.com/tumAc.png
(image from "Customizing Git - Git Attributes" from "Pro Git book"))

Once you declare that content filer driver in your local config, it will automatically, on git checkout, generate your (not tracked) file for you.
See a complete example in "Best practice - Git + Build automation - Keeping configs separate".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Awesome, that sounds very clean to me! Any ideas on tools that are normally used to generate these kinds of files (web.config, etc.) based on a template? – Heitor Castro Sep 22 '17 at 23:17
  • @HeitorCastro It can be custom. Or it depends on the actual web framework (for instance https://support.microsoft.com/en-us/help/815179/how-to-create-the-web-config-file-for-an-asp-net-application) (or http://www.timschaeps.com/using-t4-templates-to-generate-multiple-web-config-files/ ) – VonC Sep 22 '17 at 23:30