DevOps & Tools

Git Basics for Deployment: From clone to post-receive

With git you get history, rollbacks and one-command deploys: a complete guide to a bare repository and a deploy hook on your server.

Git Basics for Deployment: From clone to post-receive
Table of contents
  1. The basics in ten minutes
  2. Branches
  3. A realistic .gitignore for WordPress
  4. Deploying with a bare repo and a hook
  5. Sources

If you still move your site’s files around over FTP, this article is for you: with git you get history, the ability to roll back, and one-command deploys.

The basics in ten minutes

git init                 # start tracking the project folder
git status               # what has changed
git add .                # stage all changes
git commit -m "Update contact page"
git log --oneline        # short history

Branches

git switch -c redesign      # new branch to experiment on
# ... changes, commits ...
git switch main && git merge redesign

A realistic .gitignore for WordPress

.env
wp-content/uploads/
backup/
*.sql.gz

Passwords and backups have no place in git; these few lines cover the most common slips.

Deploying with a bare repo and a hook

On the server you create a “bare” repository and use a hook to check out every push into the site folder:

# on the server:
git init --bare /var/repo/site.git
cat > /var/repo/site.git/hooks/post-receive <<'EOF'
#!/bin/sh
GIT_WORK_TREE=/var/www/site git checkout -f
EOF
chmod +x /var/repo/site.git/hooks/post-receive

# on your laptop:
git remote add prod ssh://user@server/var/repo/site.git
git push prod main

From now on, “deploy” means git push prod main; every change is in the log, and going back to the previous version is one command. When you outgrow this, a CI pipeline is the next step; see CI/CD with a self-hosted GitHub Actions runner.

Sources

  • The official git book: git-scm.com/book
  • Hooks documentation: git-scm.com/docs/githooks

About the author

Sina Farahmand

DevOps and back-end developer, into automation, CI/CD and command-line tools. If he has to do something twice, he writes a script for it.

All articles by Sina Farahmand

Comments

No comments yet. For technical questions, include the details (OS version, the exact error and the command you ran) to get a precise answer.

Leave a comment