Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"git update-index --assume-unchanged" by default

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"?

like image 207
Heitor Castro Avatar asked Sep 21 '17 21:09

Heitor Castro


1 Answers

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".

like image 133
VonC Avatar answered Nov 05 '22 19:11

VonC