Git ignore changes to committed file

最后都变了- 提交于 2019-12-07 06:46:16

问题


I have a config file in my repo that shouldn't get changes committed to it.

The problem is that, when I make changes to the file they are picked up by git status. I've tried various ways to ignore changes to it.

How can it be automatically ignored when I clone the repo, so that any changes I make to the file shouldn't be picked up by git?

Solutions I've tried:

  1. Adding the file to .gitignore. Doesn't seem to work. Is this because the file is already on the index?

  2. Using git update-index --assume-unchanged path/to/file. Seemed to work some what but is only local to my cloned repo. Others who clone need to apply the same command.

I've tried looking at other answers to this but other solutions don't seem to work properly. I feel like this should be quite simple, so any guidance would be helpful!


回答1:


I have a config file in my repo that shouldn't get changes committed to it.

Then... don't commit that file in the first place.
Commit a generic version of that file, a template used to generate the actual file (as a private non-versioned file, added to your .gitignore).

That generation would be automatic on git checkout.

First, rename your existing file:

git mv afileToIgnore afile.tpl
git commit -m "record template value file"

Then add to afileToIgnore to a .gitignore file: a git status won't show it anymore to be added/changed.

Now, declare a smudge content filter driver which, automatically, will re-generate that file (ignored, since it is in .gitignore)

smudge script (which you can version): YourScript

copy aFileToIgnore.tpl aFileToIgnore

Declare the content filter driver in a versioned .gitattributes:

echo 'aFileToIgnore.tpl config' >> .gitattributes

That smudge 'config' content filter driver needs to be activated locally by each user cloning that repo.

cd /path/to/repo
git config filter.config.smudge YourScript

That last step is the only one each user need to do in order to benefit from the actual config file to be automatically generated on each git checkout.


Others who clone need to apply the same command.

True, but here, if they don't, they won't have the config file at all.
That is more visible than having the file ready to be modified when it should not be.



来源:https://stackoverflow.com/questions/27839263/git-ignore-changes-to-committed-file

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