git - 将不需要的文件添加到 .gitignore 后,我仍然会在 Github 上上传这些文件

标签 git github 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

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

  • .directory.txt
  • .github info.txt
  • .appunti progetto 社交网络.txt

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

enter image description here

最佳答案

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

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

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

关于git - 将不需要的文件添加到 .gitignore 后,我仍然会在 Github 上上传这些文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61289408/

相关文章:

ios终端github端口443。操作超时

javascript - 包括来自 raw.github.com 的 js

python - 从私有(private) github repo 中 pip 安装 wheel 版本

git - 如何知道一个提交是否从另一个分支与 gitlab api merge ?

git - 检查 SSH key 的提交详细信息

java - 从java运行git,git提示输入用户名和密码,为什么?

git - .gitignore : track a single file in an ignored directory

ruby-on-rails - git push heroku - 停止 heroku 推送/上传大量文件

git - .gitignore stash 在 VSCode 中

c# - gitignore 文件可以具有 XML 文档文件的通用模式吗?