If you accidentally pushed to git a folder that you don’t want in version control, here is what you can do to get rid of it from git completely

Here the example assumes you pushed the folder node_modules, and you want to remove it’s complete history

git filter-branch --tree-filter 'rm -rf node_modules' --prune-empty HEAD

git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d

echo node_modules/ >> .gitignore

git add .gitignore

git commit -m 'Removing node_modules from git history'

git gc

git push origin master --force

Source: stackoverflow