🐳 Set up a local-to-VPS git

Sun 17 Apr, 2022




Server side:

 

Worktree directory:

~/example.jcaston.uk

Repo directory:

~/repos/example.jcaston.uk.git

Creating the repo:

ssh into VPS..

cd ~/repos
mkdir example.jcaston.uk.git && cd example.jcaston.uk.git
git init --bare
--bare means that our folder will have no source files, just the version control.  --bare repos accept pushes.

Hooks

Git repos have a folder called 'hooks'. It contains some sample files for custom actions that can be taken at various stages of a push.

I want something to happen once a successful push has been made to my bare repo - I want the directory to be copied to another directory in the filesystem.
cd hooks
Now create the post-receive file by typing:
cat > post-receive
When you do this you will have a blank line indicating that what you type will be saved to the file you mentioned, in this case 'post-receive'.

Type:
#!/bin/sh
git --work-tree=/home/jamie/example.jcaston.uk --git-dir=/home/jamie/repos/example.jcaston.uk.git checkout -f
When done, press 'ctrl + d' to save.

In order to be able to execute (as in, read/write/execute permissions) this file we need to set the proper permissions:
chmod +x post-receive

'git-dir' is the path to the git repo. With 'work-tree', you can define a path to where your files will actually be transferred to. The post-receive file will be looked at every time a push is completed. It says - these files need to be in '~/example.jcaston.uk'.

 

 

Local Machine:

 

Go to the top of the project's filesystem (eg. ~/websites/example.jcaston.uk) and initiate the local repo:
git init
We then need to configure the remote path of our repo. This tells git to add a remote repo called 'live', and it will refer to the remote repo we have already set-up:

From the project directory:
git remote add live ssh://user@mydomain.com/home/jamie/repos/example.jcaston.uk.git
Then, push your work to your server with:
git push live master


With special thanks to Yingchi@peiyingchi.com <3

blog.jcaston.uk // 2025