问题
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 asmudge
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