How do I .gitignore and delete an already committed file without affecting other working copies?

倖福魔咒の 提交于 2019-11-27 14:41:31

问题


I have a bare repository and two working copies - one on my machine, the other on the server.
It turned out that I have to .gitignore a certain file that has to be specific for every machine. Let's call it 'settings.py'. This file is already committed.

I did put 'settings.py' in .gitignore to ignore it. When I now change the file on my machine git status still tells me

modified:  settings.py

I figured out that I have to remove settings.py like this:

git rm --cached settings.py

Then git add ., followed by git commit.

But when I now push this to the bare repo and pull it to the working copy on the server, settings.py is deleted there - which is bad because I have to keep this specific settings.py.

I figured that I just could make a copy of settings.py and put it back in once it is deleted, but I feel like there has to be a better way of doing this.


回答1:


You can tell git to completely ignore changes to tracked files without having to remove them. Use this command:

git update-index --assume-unchanged [FILENAME]

Then if you want to track the file later:

git update-index --no-assume-unchanged [FILENAME]



回答2:


Could you rather keep settings.py deleted permanently (both locally and on remote), and:

  • version a setting_remote file for the remote side
  • version a setting_local file for the local side (ie with local specific settings)
  • add a filter driver able to rebuild the right settings.py file on checkout.

That way, settings.py is kept "private" (non-versioned). But the specific values for each environment are versioned, each set in its own file, without any merging issue.



来源:https://stackoverflow.com/questions/3833757/how-do-i-gitignore-and-delete-an-already-committed-file-without-affecting-other

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