开发者

git version control files on repositories without pushing/pulling

开发者 https://www.devze.com 2023-02-03 10:58 出处:网络
I have my website/cms tracked in git and it works r开发者_开发技巧ather well, except for my .htaccess file. The file needs to be substantially different depending which server I am running the code on

I have my website/cms tracked in git and it works r开发者_开发技巧ather 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?)


You can do this with Git's sparse checkout feature. Run the following commands in a repo where you want a special, untracked .htaccess file:

git config core.sparsecheckout true
echo '*' >.git/info/sparse-checkout
echo '!.htaccess' >>.git/info/sparse-checkout
git read-tree --reset -u HEAD

This will first delete the existing .htaccess file. 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 .htaccess file 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 .htaccess file in that working directory, even if it is different from what's in the repository.


It sounds like the content of your .htaccess files are deployment-specific, and thus aren't really part of the code. In other words, these files can be generated by a deployment script to fit each server, rather than being kept in git and then having to override changes every time one of them is committed.


Keep your .htaccess in a totally separate git repository, and symlink it in the directory that your code is in. (And keep it .gitignore'd in the original repository.)


You could have multiple branches, one for each server, so that each branch has its own version of the server specific configuration files. Then when you make a change in the master branch, those other branches would merge with it and get the latest changes - but they would keep their own configuration. If the configuration files are changed, then a manual merge would be needed, but otherwise this is something that could be automated with scripts and maybe even githooks.


If it's a security issue, the answer is going to be "no", because if git tracks the history of the file, everyone who clones that repository is going to get the full history. But if you have control over all the locations and just want a way to track it, but at those other locations to avoid checking it out, perhaps git smudge and git clean filters might do something for you. They allow you to edit the content of a file when you check it out and commit it. I'm not certain that's a good fit, but I'm trying to come up with a workable solution.

Alternatively, track the file with an alternative name (i.e., not ".htaccess"), and where applicable, and only where applicable, create a symbolic link called .htaccess to the tracked name.

0

精彩评论

暂无评论...
验证码 换一张
取 消