Deploy with git
In this blog post, I’m going to describe how I (used to) deploy my website via git, without a single additional command. When I push master branch to my github repo, I want it to deploy to production. Also, when I push dev branch it will deploy to development environment.
Note: {folder}
is meant to be replaced.

Server side
This part is intended to be run on your server machine that you want to deploy to.
git init --bare {folder}
- initialize a bare git repo- (optional) look up how to do a “git shared repository” if you want multiple people pushing to same repo
- create
{folder}/hooks/post-receive
- bash script, that will be executed whenever you push chmod +x {folder}/hooks/post-receive
- I forgot it so many times I put this in it’s own step
Example of hook script:
#!/bin/sh
# colors
GREEN="\033[1;32m"
RED="\033[1;31m"
YELLOW="\033[1;33m"
NOCOLOR="\033[0m"
# temp folder used for building
TEMP="/tmp/{folder}"
# path to the bare repo
REPO="{repo}"
# fetches branch name
while read oldrev newrev refname
branch=$(git rev-parse --symbolic --abbrev-ref $refname 2>/dev/null)
if [ "$branch" == "master" ]; then
TARGET="{prod_folder}"
echo -e "${YELLOW}Pushing to production.${NOCOLOR}"
elif [ "$branch" == "dev" ]; then
TARGET="{dev_folder}"
echo -e "${YELLOW}Pushing to development.${NOCOLOR}"
else
echo -e "${YELLOW}Not master/develop branch, ignoring.${NOCOLOR}"
exit 1
fi
mkdir -p $TEMP
cd $TEMP
git --work-tree=$TEMP --git-dir=$REPO checkout -f $branch
# execute all your build scripts here
# example for my website:
npm install
npm run build
# copy your project to target here
# in my example
rm -rf $TARGET/*
mv ./dist/* $TARGET/
# cleanup
rm -rf $TEMP
Client side
This part is intended to be run on your development machine that you want to deploy from.
Also, the {folder}
is an absolute path of folder created in previous part.
git remote add deploy ssh://{user}@{ip}/{folder}
- if you want to have a separate remotegit remote set-url --add {remote} ssh://{user}@{ip}/{folder}
- if you want to deploy when you push to specified remote