How to ignore files which are in repository?

若如初见. 提交于 2019-11-30 06:13:13

问题


I have a file (config.php), that is already commited to Git repository, but I want to ignore locally, i.e. I want that file to remain in repository, but force Git to ignore any changes to it.

I put the file into .gitignore, but it is still marked as changed and Git still is attempting to commit changes to it, every time I commit something. Any idea, what am I missing or doing wrong?


回答1:


If the file is still displayed in the status, even though it is in the .gitignore, make sure it isn't already tracked.

git rm --cached config.php

If you just want to ignore it locally, you could also make it ignored by the git status:

git update-index --assume-unchanged config.php



回答2:


Ignore checked in file:

git update-index --assume-unchanged file

To revert

git update-index --no-assume-unchanged file

Revert All

git update-index --really-refresh 



回答3:


If the file is already in the repository, and hence the Index/Staging area, then an update to .gitignore won't change that situation - It would keep beinging committed.

To remove the file from the Index/Staging area use git rm <file>.




回答4:


Once a file is listed in the .gitignore you cannot change it.

So you could remove the file from the list in one commit, commit changes the next, then re-add the file to the ignore list in a 3rd commit afterwards.

What I usually do is split the file into two, one that is allowed and I can change, which includes the second file if it exists, then devs can make config changes in the ignored file optionally.



来源:https://stackoverflow.com/questions/7231608/how-to-ignore-files-which-are-in-repository

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