15 Mar, 2010
Using Git to manage online website projects
Posted by: James In: Linux|ProTip|Web Development
I have recently been getting into using Git for my more complicated projects. After a bit of google foo, I came across some references to using Git to push and pull website updates. This post is my way of remembering how I set up new git repositories. I did not “figure” this out myself, but I have linked to the originator. But I did modify it slightly.
Related material:
- This is one of the websites that I used as a reference, quite a bit is the same, even the borrowed graphic:
http://dmiessler.com/blog/using-git-to-maintain-your-website - Some basic information on Git usage
http://www.kernel.org/pub/software/scm/git-core/docs/v1.2.6/tutorial.html
First how it works
The server has the htdocs of the live website as well as a htdocs.git as the central main repository. Everything will be pushed there, and once completed, automatically pulled into the live website. The structure is as so:
Install Git on all systems, I use Debian.
sudo apt-get install git-core
On the server enter your websites htdocs:
git init
git add .
git commit -a -m “Initial import of project.”
Now we want to clone this repository.
cd ..
git clone –bare htdocs htdocs.git
On the local system now:
git clone ssh://root@lwp.ca/full/path/to/htdocs.git
On the server:
mv htdocs htdocs.backup
git clone htdocs.git
Note: htdocs.backup can be removed once the system is setup and working.
Final updates to the server
Due to the clone process, file permissions and ownership are now all root. So use the htdocs.backup to figure out what the file ownership/permissions were. Make sure that your new htdocs and all its sub files are back to normal.
chown -R vu2000:vu2000 htdocs
Prevent the versioning of useless files
In the server repository edit htdocs/.gitignore, and make sure we do not back up cache directories and backup files *~ and config files. Do not add trailing slashes as this will prevent the exclude from working. Prep-ending slashes means the root of the git repo. By placing the .gitignore here, it will be part of the repository so anyone else working on the project will be using the same ignore rules.
*~
/wp-content/uploads
/cache
/users/uploads
/configuration.php
Now because we issued a “git add .” we should untrack files we don’t want to send back and forth.
git rm –cache configuration.php
Now when we do pushes and pulls we don’t have to keep updating our config file every time. We should make a backup however so we always have a copy.
cp configuration.php configuration.php.live
Protecting the source
Also, place a file in all .git folders called .htaccess containing:
Deny from all
This will prevent visitors with crafty google foo to download and view source code files.
Auto updating Live site
Now to enable auto updates when updates are pushed from offline dev machine to server.
On the server in our htdocs.git/hooks/post-update, make sure it contains only the following:
cd ../htdocs
env -i git pull
Then make it executable
chmod +x post-update
Done, now test
Now on the development machine make some changes and test.
touch “new file”
git add .
git commit -a -m “Testing first update”
git push
Yay! Everything should be working!
Notes on usage.
Updates on dev machine can be pushed to server like so:
git add .
git commit -a -m “This is an update”
git push
If changes were done on the server from some reason, navigate to the htdocs folder and run:
git add .
git commit -a -m “Changes made online”
git push
Then on your dev machine pull the updates.
git pull
If you need to check what has happened since the last update:
git status
Merge one branch into another:
git checkout master
git merge myBugFixes
Delete your old branch:
git branch -D myBugFixes

Tweet This
Recent Comments