我有一些我不想在我的存储库之间共享的文件。我将它们添加到.gitignore我的项目的根目录:
# Specifies intentionally untracked files to ignore when using Git
# http://git-scm.com/docs/gitignore
# ...
.idea/
Run Code Online (Sandbox Code Playgroud)
然而,在使用git pull以下方法拉远程更改时,我不断收到此错误:
error: Your local changes to the following files would be overwritten by merge :
.idea/workspace.xml
Please, commit your changes or stash them before you can merge.
Aborting
Run Code Online (Sandbox Code Playgroud)
我看到了一些有关此错误消息的问题,但它们是关于覆盖文件的。我想忽略这些更改(我根本不希望 git 跟踪它们,也不希望 git 更改我的本地文件)。
我究竟做错了什么 ?也许问题是我在提交了我想忽略的.gitignore文件之后创建了文件,我需要以某种方式将它们从 repo 中删除......?
如果您不想跟踪它们,则需要先将它们从历史记录中删除,然后推送删除
git rm --cached -- afile
git add -u .
git commit
git push
Run Code Online (Sandbox Code Playgroud)
只要在本地删除文件(将其--cached保留在磁盘上),您.gitignore就可以工作。
但是任何 git pull 都会取回该文件,如果它在远程 repo 端进行了版本控制。因此需要推送(发布)该删除,前提是您确定每个其他用户也不应该再看到该文件。