Deploying with git

Added 2022-01-02, Modified 2022-03-12


Update: Using the powers of ChatGPT I have found a new way, simply run

git config receive.denyCurrentBranch updateInstead

On the server from within the repo, and then create the remote as usual on the dev machine:

git remote add server ssh://me@myserver/full/path/to/project/.git

This works even better because git can be used on the server too without issue!


I read an article on setting up deployment via git push to your server. I found it ugly how they had two directories project.git (git dir) and project (work tree).

First I tried pushing to a normal repo, which diden't work except with an ugly workaround.

Then I realized I could do what the article said, but with project/.git being the bare repo

On server

# full path to project
P=$HOME/project

# git will create needed directories
git init --bare $P/.git

cat <<EOF > $P/.git/hooks/post-receive
#!/bin/sh
git --work-tree=$P checkout main --force
EOF
chmod +x $P/.git/hooks/post-receive

On dev machine

git remote add server ssh://me@myserver/full/path/to/project/.git
git push server