James Emmrich

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:

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

No Responses to "Using Git to manage online website projects"

Comment Form

Categories

Upcoming Events

  • Sat, Sep 11 – Sun, Sep 12:CMRC Ontario ATV - Eagles Nest
  • Sat, Sep 11 – Sun, Sep 12:WEC Enduro National Championships - Parry Sound, Ontario
  • Sat, Sep 11 – Sun, Sep 12:CMRC South Western Ontario - Walton Vet National
  • Sat, Sep 18 – Sun, Sep 19:WEC Enduro National Championships - Labelle, Quebec
  • Sun, Sep 19:CMRC South Western Ontario - Port Perry Fall Series
  • Sat, Sep 25 – Sun, Sep 26:CMRC Ontario ATV - Grand Bend
  • Sun, Sep 26 – Mon, Sep 27:MXdN Thunder Valley Motocross Park
  • Sun, Sep 26:CMRC Ontario Cross Country Series - Gopher Dunes
  • Sun, Sep 26:CMRC South Western Ontario - RJ Motosport Fall Series
  • Sun, Oct 3:CMRC South Western Ontario - Walton Practice