“git update-index --assume-unchanged” by default

大兔子大兔子 提交于 2020-03-17 11:22:10

问题


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


回答1:


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.


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



来源:https://stackoverflow.com/questions/46353292/git-update-index-assume-unchanged-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!