Skip to content

Instantly share code, notes, and snippets.

@seancheung
Last active November 8, 2017 07:59
Show Gist options
  • Save seancheung/b438e515f7bc61f8ee99e94b200291a2 to your computer and use it in GitHub Desktop.
Save seancheung/b438e515f7bc61f8ee99e94b200291a2 to your computer and use it in GitHub Desktop.
git distributed update

create bare repo and workdir on master and slaves

cd ~
git init --bare projectname.git
mkdir projectname

create post-receive hooks on each slave

vim projectname.git/hooks/post-receive

with the following content

#!/bin/bash
WORK_DIR=/path/to/workdir
WORK_BRANCH=production

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "$WORK_BRANCH" == "$branch" ]; then
        GIT_WORK_TREE="$WORK_DIR" git checkout $WORK_BRANCH -f
    fi
done

change hook permission

sudo chmod +x projectname.git/hooks/post-receive

create the same hook on master with the following content(remember to change permission)

#!/bin/bash
WORK_DIR=/path/to/workdir
WORK_BRANCH=production

while read oldrev newrev refname
do
    branch=$(git rev-parse --symbolic --abbrev-ref $refname)
    if [ "$WORK_BRANCH" == "$branch" ]; then
        GIT_WORK_TREE="$WORK_DIR" git checkout $WORK_BRANCH -f
        # add this
        git push --force origin $WORK_BRANCH
    fi
done

run the following commands on master to add push remotes for each slave

# add remote
git remote add origin username@hostname1:projectname.git
# add push, don't forget the remote we just added
git remote set-url --add --push origin username@hostname1:projectname.git
git remote set-url --add --push origin username@hostname2:projectname.git
git remote set-url --add --push origin username@hostname3:projectname.git

add master to your local machine's remotes

git remote add dist username@hostname:projectname.git

push to master and let master handle the rest for you

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