How to remove accidently committed file or directory from Git

If you’ve accidentally committed a file or folder to your GitHub repo and don’t want it there, here’s a solution.

Git mishaps can happen to the best of us! Let’s assume you’ve committed “.????” folder (created by JetBrains IDEs)

To remove it from remote git repo

  1. Blacklist the “.????” folder by adding the “.????” folder to the .gitignore file in ?????? branch, then commit this change.
    ??? ?????? -? “?????? ???????”
  2. In your branch, check this file out from master branch using 
    ??? ???????? ?????? — .?????????
  3. Remove the .idea folder from the git tree
    ??? ?? –?????? -? .????
  4. Commit this change to your branch and push it to GitHub
    ??? ?????? -? “?????? ???????”
    ??? ???? ?????? ??????

This will work to any remote repository, not just GitHub

Share with your friends

One comment

  1. Hi, How we can completely remove the file from GitHub which means remove the commits as well. For example, initially I committed a google-services.json file in GitHub and after that, I removed it from the above git cmd but in GitHub history able to see the history and view the files. So please help me with that.

    Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *