0

I have a git repo that I created in visual studio code. I accidentally let some of the .vscode configuration files slip into my initial commit and pushed to Github. I later realized that I didn't want these files in my remote, so I added them to my .gitignore so they won't be tracked anymore. However, they're still in my remote from the initial commit. Is there an easy way to remove the files from the remote but not locally, as if I had done the gitignore correctly?

A Tyshka
  • 3,830
  • 7
  • 24
  • 46

2 Answers2

3

Remove everything from Git's staging area:

git rm -r --cached .

Add everything again, but this time it'll respect your edited .gitignore

git add -A

Overwrite the bad commit and force push

git commit --amend
git push -f

....................................................................................................

iBug
  • 35,554
  • 7
  • 89
  • 134
  • Yeah this was a duplicate but thanks for the answer! I don't think I can amend the commit at this point since I'm a good 10 commits ahead but as long as it can be removed in a new commit I'll be happy – A Tyshka Oct 09 '18 at 18:22
0

You say that the configuration file slipped in the initial commit. I understand that he you want to remove the file from the whole history and not only from the last commit as suggested in the answer of @iBug. If I am right, you should use the git filter-branch command. If I am wrong, the answer of @iBug is clearly the right one.

If you want to remove the file .vscode from the whole history :

git filter-branch -f --index-filter '
    git rm --cached --quiet --force ".vscode"
' --prune-empty -- HEAD

Then you'll have to force push to the remote. Every commits of the remote history will be rewritten so be careful, to warn everyone in your team if you choose to do this.

Romain Warnan
  • 1,022
  • 1
  • 7
  • 13