git - pull 远程更改时不会忽略 .gitignore 文件

标签 git gitignore

<分区>

我有一些文件不想在我的存储库之间共享。我将它们添加到项目根目录下的 .gitignore 中:

# Specifies intentionally untracked files to ignore when using Git
# http://git-scm.com/docs/gitignore
# ...
.idea/

然而,当我使用 git pull 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

我已经看到一些关于此错误消息的问题,但它们是关于覆盖文件的。我想忽略这些更改(我根本不想让 git 跟踪它们,也不想让 git 更改我的本地文件)。

我做错了什么?也许问题是我创建了 .gitignore 文件 after 我已经提交了我想忽略的文件,我需要以某种方式将它们从 repo 中删除......?

最佳答案

如果你不想跟踪他们,你需要先从历史记录中删除他们,然后推送该删除

git rm --cached -- afile
git add -u .
git commit
git push

一旦文件在本地被删除(--cached 将其保存在您的磁盘上),您的 .gitignore 就会工作。
但是如果在远程仓库端对它进行版本控制,任何 git pull 都会取回该文件。因此需要推送(发布)该删除,前提是您确定每个其他用户也不应该再看到该文件。

关于git - pull 远程更改时不会忽略 .gitignore 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33461815/

相关文章:

django - 如何将 .env 文件放入远程主机服务器

python - vscode 启动的许多 rg 命令占用 99% 的 CPU

bash - 如何避免 vim 内部的 vim 进行 git/mercurial merge ?

Git 状态 : show mode (permission) changes

python - 如何在 Jupyter Lab 中编辑 .gitignore?

heroku - 将 .gitignore 文件推送到特定远程

git - .gitignore 文件属于哪里?

git - 防止没有附加参数的 `git stash` 运行

Git删除名称中有空格的文件

git - inotify:除了 .gitignore 中的文件外,还可以观察文件变化吗?