Search code examples
gitgithubgitignore

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


I am developing an app and I am constantly pushing it on my Github.

I prepared a .gitignore and then I pushed my project on Github.

Then I realized I needed to prevent the upload of some more files from Github, so I added them in .gitignore list.

However, after I typed

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

I still get those files in my Github repository.

So I tried to delete the unwanted files directly from github, by clicking on them. Then I did another push and I got this error:

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.

I have read the hint, did some search on the internet, then I couldn't solve the problem, so I deleted my repository on github, created another with the same name, added it as origin of my local project, and then I did the push again.

With my surprise, I get the same result of before: the unwanted files are still uploaded on Github.

What am I doing wrong?

Here is my .gitignore:

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

Here are the unwanted files that still get uploaded:

  • .directory.txt
  • .github info.txt
  • .appunti progetto social network.txt

I attach a screenshot below: on the left is my github repo, on the right my VScode where you can see the files I am trying to hide.

enter image description here


Solution

  • That happens because the files I wanted to remove are still saved in the index.

    As explained here , this can be prevented by running, in my case:

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