Up until today I was using git in the following manner:
-
a master repo /path/www on my webserver as the document root
-
a clone on my PC
I normally made large changes on the PC and pushed to the server. After doing this, I had to “git reset HEAD” and “git checkout .” on the server. Sometimes I would make a small change on the server and pull this back to the PC. This all worked fine until today when, on pushing, git complained
remote: error: refusing to update checked out branch: refs/heads/master
etc.
Why, after 6-12 months working fine, should this fail now?
After finding http://toroid.org/ams/git-website-howto thanks to a StackOverflow question, I copied /path/www to /path/www.git and made this a ‘bare’ repo. The document root remains at /path/www. I added a .git/hooks/post-receive script containing “GIT_WORK_TREE=/path/www checkout -f”.
After modifying the config on the PC to point to the http://www.git repo, I can now push as before and the changes get uploaded and updated in the document root. I have a few questions about this:
-
the howto implies that the working tree has no .git metadata. Is this really so? I tried removing its .git directory but “git status” no longer worked. Setting GIT_DIR to /path/www.git did not help as git said the repo (the bare one at /path/www.git) was not in fact a repo.
-
If I update index.html and add somefile.html on the PC and push these changes, these files are updated/added-to in the server’s doc root. But if I use “git status” in the doc root, it tells me that index.html has been changed and somefile.html is an untracked file. I naively expected that the “checkout -f” would have updated the working tree to exactly what I pushed from the PC. Can I achieve that?
Yes. You have a bare repo setup and to actually get your files for your website, you “temporarily” assign a working directory to the bare repo and check it out to the website path that you want. That is what
GIT_WORK_TREE=/path/www checkout -fdoes. The.gitthat was present would have been an artifact of your previous setup before you moved to the bare repo.Again, this seem like the effect of the presence of the
.gitfrom your previous setup. Delete the/var/www/.gitYour expectation is right and this how it is supposed to work. Remove the
.gitas mentioned above.To actually do a git status, go to
/var/www.gitand doGIT_WORK_TREE=/var/www git statusPS: Why are you doing so many changes and git status on your server? Limit yourself to your PC.