How to Ignore Local Changes to Your settings.php File in Drupal 7/ Drupal 8 with Git
Why not just use the .gitignore file?
As many of you know, I am a huge Pantheon hosting fanboy and can still remember the days during the beta launch of being blown away that I have three different environments out of the box, with dev, test and live. Another great service they added recently is that all sites receive SSL certificates automatically and all you have to do is redirect all traffic to use HTTPS. In Drupal 8 they suggest doing this in your settings.php file.
After adding the redirect code everything works great until you fire up your local environment (I am currently using Lando) and you are getting a blank screen. After further investigation, you notice it’s the redirect to HTTPS that is causing the issue. My first thought was to make sure my settings.local.php file was correctly being used but for the life of me, I could not get that file to override the redirect code in my settings.php file. If you are reading this and have a better idea on to how to accomplish this then let me know in the comments 🙂
My next thought was to simply add the settings.php file to my .gitignore file but when I went to my production website I was prompted to reinstall my Drupal site. When adding a file to .gitignore the repo pretends it doesn’t exist so therefore Drupal was telling me to reinstall. Whoooops, my production site kind of needs this file hahahah. So I thought to myself,
How can I ignore my settings.php file in my local repo but still have the original file on production?
After attending Google University for 10 minutes, I stumbled upon a medium post by Ian Gloude regarding the
git update-index command. In their article “Git skip-worktree and how I used to hate config files,” there is a great explanation of the concept, but for me the lightbulb really went off when reading the Git documentation hint, “see also git-add for a more user-friendly way to do some of the most common operations on the index.” Basically
git update-index tells Git what to watch in your repo.
Now that we understand what
git update-index does, the real magic happens with the options that you can add to the command. In this case, the option that Ian Gloude suggested is the
--skip-worktreeoption. The Git documentation explains that the skip worktree bit tells the git index to assume the file is unchanged from this point on regardless if there is an actual change. So what does this mean for us? It means you can change your file on your local environment while the original file on your production server remains unchanged.
Here is the command I use prior to uncommenting out the pantheon redirect code.
git update-index –skip-worktree /sites/default/settings.php
When I need to make some changes to the production settings.php file I can tell Git to watch the file again with this command.
git update-index —-no-skip-worktree web/sites/default/settings.php
Anyway, I hope this helps you keep your local and production environments running smoothly while maintaining your settings differently.