Publishing with git + ssh: Difference between revisions

From XPUB & Lens-Based wiki
Line 54: Line 54:
     git add remote myserver myvps:git/project.git
     git add remote myserver myvps:git/project.git


and finally
and finally push one time to the server, setting it as the "upstream" branch


     git push --set-upstream myserver main
     git push --set-upstream myserver main
You don't have to do this last step, if you don't want myserver to be your default / aka upstream branch that you push to, in this case you would just:
    git push myserver main
Whenever you want to publish instead of just
    git push


== On the remote server ==
== On the remote server ==

Revision as of 16:57, 9 October 2024

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

If you have ssh access to your server with credentials like:

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 could also configure a host alias in your .ssh/config:

Host myvps
    Hostname myvps.net
    User sshuser

And then just (instead of the scp command above):

   scp -r project.git myvps:git/

Add the remote to your original repo

At this point you can delete the bare repo, aka the local folder project.git, as 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 using your ssh host alias

   git add remote myserver myvps:git/project.git

and finally push one time to the server, setting it as the "upstream" branch

   git push --set-upstream myserver main

You don't have to do this last step, if you don't want myserver to be your default / aka upstream branch that you push to, in this case you would just:

   git push myserver main

Whenever you want to publish instead of just

   git push

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: