Deploy Projects with Git

Simple deployment with Git and SSH

A lightweight deployment workflow without a git provider. The repo sits directly on the client server (e.g. cyon.ch). A post-receive hook checks out the production branch into the live folder on push.

Requires SSH keys set up to connect to client hosting. Works with cyon.ch and other providers.

Server folder structure

The bare repo lives outside public_html. Public folders are named after the URLs pointing to them.

/home/felixfde/git/felixf.de.git
/home/felixfde/public_html/felixf.de
/home/felixfde/public_html/dev.felixf.de

Setup steps

  1. Create a standard local git repo
  2. Create a bare git repo on the server (not in /public_html, use /git)
    git init --bare REPONAME.git
  3. Add the server as a remote to the local repo
    git remote add cyon felixfde@felixf.de:git/repository.git
  4. Add the hooks/post-receive script into the bare repo (see below)
  5. Make the post-receive hook executable
    chmod 744 post-receive
  6. Checkout the bare repo into the live folder on the same server
  7. Test by pushing content to the production branch

The post-receive hook

Place this in hooks/post-receive inside the bare repo. Set the working tree paths to match your server layout.

Example working tree for cyon.ch: production_working_tree="/home/maribabi/public_html/studio-biro.de"

#!/bin/bash

production_branch="production"
production_working_tree="CHANGE THIS"

development_branch="development"
development_working_tree="CHANGE THIS"

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ -n "$branch" ] && [ "$production_branch" == "$branch" ]; then

       GIT_WORK_TREE=$production_working_tree git checkout $production_branch -f
       NOW=$(date +"%Y%m%d-%H%M")
       git tag release_$NOW $production_target_branch

       echo "   +--------------------------------------------+"
       echo "   | !!! COMPLETED DEPLOYMENT TO PRODUCTION"
       echo "   +--------------------------------------------+"
       echo "   | used git branch: $production_target_branch"
       echo "   | deployed to: $production_working_tree"
       echo "   | set tag to: release_$NOW"
       echo "   +--------------------------------------------+"
    fi

    if [ -n "$branch" ] && [ "$development_branch" == "$branch" ]; then

       GIT_WORK_TREE=$development_working_tree git checkout $development_branch -f

       echo "   +--------------------------------------------+"
       echo "   | COMPLETED DEPLOYMENT TO DEVELOPMENT"
       echo "   | used git branch: $development_branch"
       echo "   | Target folder: $development_working_tree"
       echo "   +--------------------------------------------+"
    fi
done

The production deploy also creates a timestamped release tag (e.g. release_20240315-1430).

Remove .DS_Store from a git repo

Source: StackOverflow

Remove existing files from the repository:

find . -name .DS_Store -print0 | xargs -0 git rm -f --ignore-unmatch

Add .DS_Store to .gitignore:

echo .DS_Store >> .gitignore

Commit:

git add .gitignore
git commit -m '.DS_Store banished!'