There are times when you want to commit a file to a public/shared git
repository but don't want further changes to that file being committed. This is common with config files. The updates may contain local development data or public API keys that you don't wish to share with other users when they clone the repo. You can use .gitignore
, but then the file will not be committed at all - this can be a less than optimum solution for config files, as the README.md
ends up containing details of what config files need to be created after cloning. An alternative for this situation is update-index --skip-worktree
Definition of --skip-worktree
from the docs:
When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and reads the index version instead
After committing a file with it's default values (the values you wish to share), run the following command against the file. The current version of the file will be committed, but any changes to the file will be ignored in subsequent commits
git update-index --skip-worktree <filename>
To undo the update-index
we use:
git update-index --no-skip-worktree <filename>
When using --skip-worktree
make sure the target file is up to date (same version) on all branches - otherwise you will get warnings when merging or switching branches. To view a list of files that have had --skip-worktree
applied, use the following git
command:
$ git ls-files -v | grep ^S
Note
--skip-worktree
is not a good option for sensitive data, as the cloned repo will not have the--skip-worktree
associated with the files; inadvertent publishing of sensitive data in cloned repos is a risk with this approach