If you have already been working in your git repository before you realized that you needed to add something to your .gitignore file, you may have found that it’s not as simple as updating your .gitignore file anymore. The tracked files that you just decided to ignore won’t just magically stop tracking and disappear from git, oh no.
Luckily, it is actually very simple to accomplish this with just a couple commands.
Simple Commands to Remove Gitignored Files
git rm -r --cached . git add .
What those lines do is wipe your Git index (cache) and then re-add everything. If you run them one line at a time, it will look kind of scary in between because it will look to git like you’re trying to delete your entire project. However, don’t fret. The 2nd line puts everything back, except for the files you recently gitignored.
After running those 2 commands, you can run
git status to see all of the files that matched your gitignore that will be deleted from the git repo as soon as you commit and push it.
Commit and Push
If all of that looks good, just commit and push. You can use any git GUI interface that you use, or just run a couple more commands…
git commit -m "Removed gitignored files" git push
Understand the Implications of Removing Gitignored Files
The workspace that you run these commands on will not actually have these gitingored files deleted. All of those files will remain intact on that machine.
HOWEVER, since the commit that you push to the repo consists of “delete” commands for git, they will actually be removed from the git repository, and anyone else that pulls the updates from that repository will have those files deleted.
Therefore, if you are gitignoring a file like wp-config.php that your staging server will need to work and other developers will need on their local machines to work, you should consider saving a version of that file that has something appended.
For instance, you before you commit this, you could duplicate your wp-config.php file and name the copy wp-config-local.php. You could include that in your commit and let the rest of your team know to duplicate from that file when their wp-config.php file disappears.
If your dev or staging server is connected to your git repo, you will probably need to fix that via FTP or cPanel.
I hope this was helpful for you. If it was, please let me know in the comments below.
Also, if you have any questions for me, let me know. Thanks!