How to Set Up One-Command Deploys From Git to Your Host

How to Set Up One-Command Deploys From Git to Your Host

To set up a one-command git deploy to hosting, you push a branch to a Git remote on the server and let a post-receive hook check the code out into the live directory. The push is the command: once the remote and hook exist, git push production main is the whole release process.

The reason this works is that Git's push protocol sends objects first and ref updates second. The server writes the objects into the repository, updates the ref, and only then runs the hook, so the hook always sees a complete tree. Deciding what the hook should do with that tree, and how to make it safe to run repeatedly, is most of the work.

Choosing the transport for your git deploy to hosting

There are three realistic ways to reach the server, and they differ in what you have to configure on each side.

SSH is the usual choice on a VPS or dedicated box. You create a system user, put your public key in ~/.ssh/authorized_keys, and give the repository a path. Git runs its own server-side binary through the SSH connection, so nothing extra listens on the network. The remote looks like ssh://[email protected]/var/repo/site.git or, with a host alias in ~/.ssh/config, just [email protected]:site.git.

Git's own daemon, git-daemon, speaks the native protocol on port 9418 with no authentication at all. It is fine for read-only mirrors on a trusted network and a bad idea for a deploy target, because anyone who can reach the port can push.

Smart HTTP is the option that survives restrictive firewalls, because it rides on port 443. The server needs a CGI or FastCGI handler for git-http-backend, and the push arrives as a POST to a path ending in /git-receive-pack. Authentication is whatever your web server already does, typically basic auth over TLS. The client side is a plain URL:

git remote add production https://[email protected]/site.git
git push production main

Whichever transport you pick, the server-side mechanics are identical. The difference is only how the bytes get there.

Creating the bare repository and the hook

The deploy target is a bare repository, meaning it has no working tree of its own. It exists to receive objects. You create it on the server:

git init --bare /var/repo/site.git
chown -R deploy:deploy /var/repo/site.git

Inside it, hooks/post-receive is an executable script that reads one line per updated ref from standard input. Each line has three fields: the old commit, the new commit, and the ref name. A minimal hook checks out the new tree into the document root:

#!/bin/sh
set -e
TARGET=/var/www/site
while read old new ref; do
  if [ "$ref" = "refs/heads/main" ]; then
    GIT_WORK_TREE="$TARGET" git checkout -f "$new"
  fi
done

Make it executable with chmod +x hooks/post-receive. The -f flag on checkout discards any local changes in the target directory, which is what you want, because the target is a build artefact and not a place to edit files. The GIT_WORK_TREE variable tells Git where to materialise the files, since the bare repository has no work tree of its own.

Two details bite people here. First, the hook runs as the user who owns the pushing SSH session, so the target directory must be writable by that user, not by the web server user. Second, the checkout leaves files that were deleted in Git sitting in the target directory, because checkout does not remove untracked files. If stale files matter, check out into a fresh directory and swap a symlink, or run git clean against the work tree with care.

Rejecting pushes you do not want

A pre-receive hook runs before any ref is updated and can refuse the push by exiting non-zero. Use it to reject force pushes to the deploy branch, which otherwise let a rewritten history silently replace what is live. It also gives you a place to run a syntax check or a test suite before the tree ever reaches the document root. If the pre-receive hook fails, the client sees the reason on stderr and the ref is untouched.

Pinning the deploy branch and keeping history sane

A common mistake is deploying from whatever branch the developer happens to be on. Make the remote explicit about its refspec so a push of a side branch cannot trigger a deploy. In your local repository:

git remote set-url --push production ssh://[email protected]/var/repo/site.git
git config push.default nothing
git config remote.production.push refs/heads/main:refs/heads/main

With push.default set to nothing, a bare git push production refuses to guess which branch you meant, and the explicit refspec in the remote configuration means the only thing that can land on the server is main. The hook then only needs to handle one ref, which keeps it small.

You will also want the server repository to be readable by whoever debugs a bad deploy. Set receive.denyCurrentBranch to ignore or refuse as appropriate for your layout, and set core.logAllRefUpdates to true so the reflog records every push. That reflog is the fastest way to answer the question of what was live an hour ago.

Handling dependencies and build steps

Checking out files is not the same as deploying an application. If your project needs a dependency install or an asset build, put those commands in the hook after the checkout, and make the hook fail loudly if they fail. A failed build should leave the previous release serving traffic, which means the checkout and the build must not both write to the live directory. The safe pattern is to check out into a release directory named after the commit, run the build there, and only then repoint a symlink that the web server follows. That gives you atomic releases and a trivial rollback: point the symlink at the previous directory.

Keep secrets out of the repository. The hook runs with the server user's environment, so database credentials belong in a file outside the work tree that the application reads at runtime, not in a committed config file that the checkout overwrites on every push.

What to do next

Set up the bare repository and the hook on a staging account first, push a throwaway commit, and confirm that the files land where you expect and that a deliberately broken build leaves the old release intact. Once that behaves, add the production remote, tighten the refspec, and treat the deploy command as the only way code reaches the server. Keep a note of the reflog command you will reach for when something looks wrong.

Related articles

Subscribe to our newsletter

Get the latest hosting tips, performance insights, and industry news.