如何.gitignore并删除已提交的文件而不影响其他工作副本? [英] How do I .gitignore and delete an already committed file without affecting other working copies?
问题描述
我有一个裸仓库和两个工作副本 - 一个在我的机器上,另一个在服务器上。
事实证明,我必须 .gitignore
必须针对每台机器特定的文件。我们称之为' settings.py
'。此文件已被提交。
我在.gitignore中放入了< settings.py
'来忽略它。当我在我的机器上更改文件时,git status仍告诉我:
修改:settings.py
我发现我必须像这样删除settings.py:
git rm --cached settings.py
然后 git add。
,然后是 git commit
。
我现在把它推到裸露的仓库,并将它拖到服务器上的工作副本上, settings.py
被删除 - 这是不好的,因为我必须保持这个特定的 settings.py
。
我想我可以复制 settings.py
并在删除后将其放回原处,但我觉得必须有更好的方法来完成此操作。
您可以告诉git完全忽略对被跟踪文件的更改,而无需删除它们。使用这个命令:
git update-index --assume-unchanged [FILENAME]
然后如果您想稍后跟踪文件:
git update-index --no-assume-unchanged [FILENAME]
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 memodified: settings.py
I figured out that I have to remove settings.py like this:
git rm --cached settings.py
Then
git add .
, followed bygit 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 specificsettings.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.解决方案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]
这篇关于如何.gitignore并删除已提交的文件而不影响其他工作副本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!