在我将不需要的文件添加到 .gitignore 后,我仍然在 Github 上上传了不需要的文件

I still get unwanted files uploaded on Github after I added them in .gitignore

我正在开发一个应用程序,我不断地在我的 Github 上推送它。

我准备了一个 .gitignore 然后我把我的项目推到了 Github。

然后我意识到我需要阻止从 Github 上传更多文件,所以我将它们添加到 .gitignore 列表中。

然而,在我输入之后

git add .
git commit -m "smth"
and git push -u origin master

我的 Github 存储库中仍然有这些文件。

所以我尝试通过单击直接从 github 中删除不需要的文件。然后我又做了一次推送,我得到了这个错误:

error: failed to push some refs to 'https://github.com/tommasosansone91/simplesocial.git'

hint: Updates were rejected because the remote contains work that you do

hint: not have locally. This is usually caused by another repository pushing

hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again.

hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我已经阅读了提示,在互联网上做了一些搜索,然后我无法解决问题,所以我删除了我在 github 上的存储库,创建了另一个同名的存储库,并将其添加为来源我的本地项目,然后我又做了一次推送。

令我惊讶的是,我得到了与之前相同的结果:不需要的文件仍在 Github 上上传。

我做错了什么?

这是我的 .gitignore:

.env
.credenziali.txt
.directory.txt
.github info.txt
.appunti progetto social network.txt

以下是仍在上传的不需要的文件:

我在下面附上一张截图:左边是我的 github 存储库,右边是我的 VScode,你可以在其中看到我试图隐藏的文件。

发生这种情况是因为我要删除的文件仍保存在索引中。

here 所述,这可以通过 运行 来防止,在我的例子中:

git rm --cached ".directory.txt"
git rm --cached ".github info.txt"
git rm --cached ".appunti progetto social network.txt"