Publishing with git + ssh: Difference between revisions

From XPUB & Lens-Based wiki
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
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).  
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,
 
This recipe '''does not require that you have a specialized git server installed like gitea, gitlab, or github.'''
 
You can combine this with also using a git server (as git repos can have multiple remotes).
 
Note: a simpler technique than this one would be [[publishing with scp]].


== On you local computer ==
== On you local computer ==
Line 48: Line 56:


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


''or using your ssh host alias''
''or using your ssh host alias''


     git add remote myserver myvps:git/project.git
     git remote add myserver myvps:git/project.git


and finally push one time to the server, setting it as the "upstream" branch
If you would like to be able to simply type "git push" to publish to your server, you should set the server as the "upstream" branch with the following command (assuming you are using the branch named ''main''):


     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 ==
Line 83: Line 83:
=== Create the webhook ===
=== Create the webhook ===


     cd ~/git/project.git
     cd ~/git/project.git/hooks


You can ls and see the "insides" of the repo:
Ok, let's create post-receive


     branches/  config  description  HEAD  hooks/  info/  objects/  packed-refs  refs/
     nano post-receive


Make sure there's not already a post-receive script...
<pre>
 
echo Updating the website...
    ls hooks
GIT_WORK_TREE=~/public_html/project git checkout
 
</pre>
Ok, let's create one...
 
    nano git/project.git/hooks/post-receive


If you want to use force (overwriting any local changes) add the -f flag.
<pre>
<pre>
echo Updating the website...
echo Updating the website...
GIT_WORK_TREE=~/public_html/project git checkout -f⏎
GIT_WORK_TREE=~/public_html/project git checkout -f
</pre>
</pre>


=== Make the hook executable ===
=== Make the hook executable ===


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


== Test it out (from your local machine) ==
== Test it out (from your local machine) ==
Line 112: Line 110:
     git commit -a -m "local changes"
     git commit -a -m "local changes"


Now when you push...
If you set the upstream branch above, you can:


     git push
     git push


You should see messages from the post-receive hook:
Otherwise (and always) you can specify the remote + branch name when you push:
 
    git push myserver main
 
You should see messages from the post-receive hook, importantly you should see the output of the echo command we put in the hook (Updating website...); when you see this, you know the hook is running.
 
<pre>
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Updating website...
To myserver:git/project.git
  2dabf42..9be0276  main -> main
</pre>
 
If any errors occur, you should see them here at the end of the message stream.
 
[[Category:Cookbook]] [[Category:git]] [[Category:SSH]]

Latest revision as of 08:42, 13 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,

This recipe does not require that you have a specialized git server installed like gitea, gitlab, or github.

You can combine this with also using a git server (as git repos can have multiple remotes).

Note: a simpler technique than this one would be publishing with scp.

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 remote add myserver sshuser@myvps.net:git/project.git

or using your ssh host alias

   git remote add myserver myvps:git/project.git

If you would like to be able to simply type "git push" to publish to your server, you should set the server as the "upstream" branch with the following command (assuming you are using the branch named main):

   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/hooks

Ok, let's create post-receive

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

If you want to use force (overwriting any local changes) add the -f flag.

echo Updating the website...
GIT_WORK_TREE=~/public_html/project git checkout -f

Make the hook executable

   chmod +x 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"

If you set the upstream branch above, you can:

   git push

Otherwise (and always) you can specify the remote + branch name when you push:

   git push myserver main

You should see messages from the post-receive hook, importantly you should see the output of the echo command we put in the hook (Updating website...); when you see this, you know the hook is running.

Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 287 bytes | 287.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Updating website...
To myserver:git/project.git
   2dabf42..9be0276  main -> main

If any errors occur, you should see them here at the end of the message stream.