Skip to content

Instantly share code, notes, and snippets.

@eduardobc88
Last active March 5, 2021 03:28
Show Gist options
  • Save eduardobc88/f0b9bd5f0f9b7df2e643929a25a82534 to your computer and use it in GitHub Desktop.
Save eduardobc88/f0b9bd5f0f9b7df2e643929a25a82534 to your computer and use it in GitHub Desktop.
Steps to configure the server and client for push git branch into the server side
# 1- (server) create a git repository on server side
git init --bare ~/<projec-name>
# 2- (server) into the directory: '~/<project-name>/hooks/' create a file: 'post-receive' and add this:
!/bin/bash
while read oldrev newrev ref
do
if [[ $ref =~ .*/master$ ]];
then
echo "== GIT HOOKS : Master ref received. Deploying master branch to production... =="
git --work-tree=~/<dir-name>/<dir-name>/ checkout -f master
else
echo "== GIT HOOKS : Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server. =="
fi
done
# 3- (server) save file and give execution permission to the file 'post-receive'
chmod +x post-receive
# 4- (client) now in your local git project add new remote like this:
git remote add production <user>@<ip>:<project-name>
# 5- (client) now you can push changes like this
git push production main
# 6- (client) you should see the next message into the logs
# remote: == GIT HOOKS : Ref refs/heads/main successfully received.
# this because only the 'master' branch can be deployed into the server to change this edit the script like this:
if [[ $ref =~ .*/<branch-name>$ ]];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment