Using git to update my website

I used to be scared of git, but actually, it’s really handy? For example, it makes it very easy to manage this site on my computer and then just push all the changes at once when I’m done.

(This also works for Gemini capsules and presumably Gopherholes if those are your bag.)

Here’s how I do it.

Make two directories on your server: One for the site itself (in /var/www/ or somewhere equally reasonable) and one in your user directory.

mkdir /var/www/awesome-website
mkdir ~/awesome-website

Then initialize the git repository in the latter directory.

cd ~/awesome-website
git config --global init.defaultBranch main
git init --bare

Then! You want to make a git hook. What this does is when you push your repository from your local machine, it then takes the changes and copies them into your website directory.

nano hooks/post-receive

(Yes, I use nano.) Populate the file with this:

#!/bin/bash

GIT_WORK_TREE=/var/www/awesome-website git checkout -f

And finally make the hook executable:

chmod u+x hooks/post-receive

Leave your remote server (this whole setup assumes you’re using one, if that wasn’t clear). Clone the repo onto your local machine:

git clone ssh://user@remoteserver:/home/user/awesome-website
cd awesome-website
git branch -m main

Put some HTML files in the repository folder. When you’re ready to upload them,

git add .
git commit -m "whatever, no one's going to read this"
git push

git add . adds the entire contents of the folder you’re in. You can also just type in specific file and folder names if you’d rather: git add whatever.html

You might get some errors about the branch being wrong? I usually fix these via trial and error. I’m still not a git expert, I just poke it until it works.

Voila, easy website.