Git: Add .gitignore after commit

Robert Meyer 04 Oct 2017 GitHub gitignore permalink

I had created a new repository in GitHub and forgot to add the .gitignore file to ignore all my Visual Studio and debugging files. However, I did not realize it until after I created the first commit.

These are the steps to apply the gitignore file later:

  1. Create a .gitignore file in the root of your local repository. Run this command. This removes everything from the index:
    git rm -r --cached .
  2. Re-add all files. Now the files and folders from gitignore are ignored.
    git add .
  3. Then commit the changes again and push it to your GitHub repository
    git commit -m ".gitignore is now working"
    git push