Publishing with git + ssh: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
Line 20: Line 20:


     scp -r project.git remote:git/
     scp -r project.git remote:git/
== clone the bare repo to a (public) working directory on the server ==
    ssh server
    cd public_html
    git clone ~/git/project.git .


== create the webhook on the server ==
== create the webhook on the server ==
Line 25: Line 32:
Now ssh to the server and edit a post-receive githook.
Now ssh to the server and edit a post-receive githook.


    ssh server
     cd ~/git/project.git
     cd git/project.git


You can ls and see the "insides" of the repo:
You can ls and see the "insides" of the repo:
Line 32: Line 38:
     branches/  config  description  HEAD  hooks/  info/  objects/  packed-refs  refs/
     branches/  config  description  HEAD  hooks/  info/  objects/  packed-refs  refs/


If you ls the hooks folder you see some template hooks...
Make sure there's not already a post-receive script...


     ls hooks
     ls hooks
Ok, let's create one...


     nano git/project.git/hooks/post-receive
     nano git/project.git/hooks/post-receive

Revision as of 16:10, 9 October 2024

This recipe is to describe the steps for taking a local folder that's managed by git and using a remote server where you have ssh access, creating an "as-simple-as-possible" remote that can be pushed to (and eventually pulled from) and that is also mirrored on the same server. In other words, there's also a public working directory that automatically gets updated (via a githook) when the repo is pushed to. This recipe does not require that you use a git server like gitea, gitlab, or github. Just that you have ssh access to a server. You can combine this with also using a git server (as git repos can have multiple remotes).

The steps

  • Convert local folder into a bare repo
  • scp the bare repo to a (non-public) part of your server
  • Create the webhook in the bare repo on the server

Convert local folder into a bare repo

   git clone --bare project project.git

Converts normal folder project (that is already git-managed!), into a bare repository 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

Here assuming you have a folder called git in your remote home folder.

   scp -r project.git remote:git/

clone the bare repo to a (public) working directory on the server

   ssh server
   cd public_html
   git clone ~/git/project.git .


create the webhook on the server

Now ssh to the server and edit a post-receive githook.

   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
GIT_WORK_TREE=/home/murtaugh/public_html/project git checkout -f⏎