Publishing with git + ssh

From XPUB & Lens-Based wiki

This recipe is for taking a local folder that's managed by git and configuring a publicly accessible copy on a remote server that you can update just using git. This recipe requires that you have (full) ssh access to the server, that git is installed on the server, but does not require that you habe a specialized git server like gitea, gitlab, or github. You can combine this with also using a git server (as git repos can have multiple remotes).

On you local computer

Starting point, you have a folder name "project" that you want to publish. In a terminal, you cd to the parent directory of your project folder.

Make a (bare) copy of your repo

   git clone --bare project project.git

This command converts project a folder that is already git-managed, into a bare repository named project.git.

A bare repo is basically only the .git folder part of a git-managed folder (it's insides if you like). You could in principle just copy this (inner) folder, but git's clone command has a bare option that does it for you. It's a good idea to name a bare repo .git to remind yourself that it's a bare git repo.

scp the bare repo to your server

Imagine that you have ssh access to your server with:

user: sshuser
hostname: myvps.net

And assuming you have have already created folder called git in the home folder of your (remote) server. You don't have to name the folder git, but it makes sense to have a special place to keep bare repos on your server.

   scp -r project.git sshuser@myvps.net:git/

or if you have configured a host alias in your .ssh/config (in this case called "myvps").

   scp -r project.git myvps:git/

Add the remote to your original repo

At this point you can delete the bare repo (it's now on the server). Let's add the ssh address of the repo on the server as the "myserver" remote. You can name the remote whatever you want including "origin" if you would like the remote server to use this, the default and most common remote name.

first

   cd project

then

   git add remote myserver sshuser@myvps.net:git/project.git

or

   git add remote myserver myvps:git/project.git

and finally

   git push --set-upstream myserver main

On the remote server

ssh to your server

   ssh sshuser@myvps.net

or again if you have an ssh host alias, you can just use it:

   ssh myvps

Clone the bare repo to a (public) working directory

   cd public_html
   git clone ~/git/project.git

Create the webhook

   cd ~/git/project.git

You can ls and see the "insides" of the repo:

   branches/  config  description  HEAD  hooks/  info/  objects/  packed-refs  refs/

Make sure there's not already a post-receive script...

   ls hooks

Ok, let's create one...

   nano git/project.git/hooks/post-receive
echo Updating the website...
GIT_WORK_TREE=~/public_html/project git checkout -f⏎

Make the hook executable

   chmod +x ~/git/project.git/hooks/post-receive

Test it out (from your local machine)

Imagine you've made some changes, and then you (as usual)...

   git commit -a -m "local changes"

Now when you push...

   git push

You should see messages from the post-receive hook: