Jeff Johnson (My apps, PayPal.Me, Mastodon)

Feedback Assistant Boycott

How I git push from my laptop to my website

August 12 2024

I keep this website under version control in a git repository on my MacBook Pro. The tricky part is getting the website files from my laptop onto my web server. In the past I did this manually via SFTP, which obviously sucked. With help from the excellent customer service of my web host Tiger Technologies, whom I've been with since 2006 and recommend highly, I realized that I could just git push from my local repository. This is possible because my web server allows ssh access and also has git installed. That's all you need. I'll provide a little tutorial here, in the hope that someone finds it helpful.

First, I logged in to my web server via ssh and created a "bare" git repository.

git init --bare --initial-branch=main /[home directory]/git.git

The reason for the bare repository is that I never use it to make commits, which are all done on my laptop, and I want to keep the .git directory separate from the publicly hosted website files. You don't want to publish your ugly git history!

After creating the bare repository, I added a "hook" that runs whenever I push to the repository. The hook is an executable shell script file /[home directory]/git.git/hooks/post-receive on the web server.

#!/bin/sh
git --git-dir=/[home directory]/git.git --work-tree=/[home directory]/html checkout --quiet --force

The --work-tree argument needs to be specified because it's a bare repository. The html directory is where the website files are served from.

In the git repository on my laptop, I added the remote repository.

git remote add web [user name]@lapcatsoftware.com:~/git.git

The first time I push from my laptop to the web server, I call --set-upstream to allow the use of a simple git push in the future.

git push --set-upstream web main

And that's it! Whenever I push to the web server, it automatically checks out the latest commit from the main branch, and the files from that commit are served to the public.

I used to have a .gitignore file with the following contents.

.DS_Store
/temp/

You have to love the old .DS_Store, right? I didn't want to serve either .DS_Store files or the .gitignore file from my website, so I simply moved the contents of .gitignore to the file .git/info/exclude in my local repository.

Feedback Assistant Boycott

Jeff Johnson (My apps, PayPal.Me, Mastodon)