Skip to content

Instantly share code, notes, and snippets.

@khoipro
Created January 7, 2021 02:10
Show Gist options
  • Select an option

  • Save khoipro/9fcef0d5a59c2a8c99ab81db63231a8a to your computer and use it in GitHub Desktop.

Select an option

Save khoipro/9fcef0d5a59c2a8c99ab81db63231a8a to your computer and use it in GitHub Desktop.
.git/hooks/post-receive to auto update when Git has changed to specific branch. Example folder with cPanel user.
#!/bin/bash
set -eu
TARGET="/home/<username>/public_html/"
GIT_DIR="/home/<username>/public_html/.git"
BRANCH="production"
while read oldrev newrev ref
do
# only checking out the master (or whatever branch you would like to deploy)
if [[ $ref = refs/heads/"$BRANCH" ]];
then
echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
git --work-tree="$TARGET" --git-dir="$GIT_DIR" checkout -f
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
fi
done
@khoipro
Copy link
Author

khoipro commented Jan 7, 2021

To make it work, you must copy this file to folder .git/hooks and rename to post-receive. Remember to chmod 777 if needed.

chmod 777 post-receive

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment