Let’s say you accidentally commit and push a file full of passwords (like a .env file) that you don’t want other people to want and now it is part of your public project on Github? How do you get rid of that? Fear not, follow these two commands and you’ll be back to before you took that ill-fated action.
Before beginning, make sure that you backup any files and changes you’d like to preserve elsewhere as your work in the repository will be wiped back to where you were at your previous commit.
git reset –hard HEAD^
git push origin +master –force
(Note that here I used “master” as that was the branch I was working on, you’ll want to change “master” to whatever your branch name is and use that in the command)