I have my website/cms tracked in git and it works rather well, except for my .htaccess file. The file needs to be substantially different depending which server I am running the code on (local/test/live). As a result, right now I’ve kept .htaccess out of git entirely (it’s in my .gitignore file). But occasionally it gets lost, and at any event it is a part of the site and I’d like it to remain tracked, but I don’t want it pushed to remote repositories where it might overwrite the correct configurations for the other servers. Is there any way to make git keep a file tracked, but only locally? (Or any other way to solve the problem?)
Share
You can do this with Git’s sparse checkout feature. Run the following commands in a repo where you want a special, untracked
.htaccessfile:This will first delete the existing
.htaccessfile. But now it’s ignored from Git’s perspective, so you can put a machine-specific one there and Git won’t bother you about it.You can then add and manage a
.htaccessfile from some other repository (say your local one). Git will be happy to track this file and keep it in the repository, but on a machine with the above sparse checkout configuration, Git will ignore the local.htaccessfile in that working directory, even if it is different from what’s in the repository.