// tools · version control

Git for WordPress

On a WordPress project Git keeps the history of code, not of the whole site. Content lives in the database, media in uploads. Get this boundary right, and every change can be reviewed, deployed and rolled back. Shown on the real project 4wp.dev.

  • .gitignore
  • staging → main
  • pre-commit hook
  • CI
4wp.dev — terminal
# hooks are switched on by Composer
$ composer install
# one change, one commit
$ git add templates/page-git.html
$ git commit -m "Git page"
✓ checks passed
# staging deploys itself
$ git push origin staging
Free LMS4WP courseGit Basics Course

// 01 — What Git is

Three places a change lives

Git is a version control system: it keeps snapshots of your files, who made them and why. A change moves through three places before it becomes history. Press the commands to move the file.

Working directory

You edit files

Staging area

You choose what goes into the next commit

Repository

A commit: a snapshot with a message

page-git.html

// 02 — What goes into Git

Code in, content out

Plugins and WordPress core are not copied into the repository: Composer installs them by version. So the site repository stays small and every environment gets the same code.

In Git

  • Theme code
  • Own single-file mu-plugins, configuration
  • composer.json and composer.lock
  • Deploy workflows and scripts
  • .env.example

Not in Git

  • WordPress core (web/wp)
  • vendor/ and plugins installed by Composer
  • .env, keys, auth.json, database dumps
  • Uploads
# 4wp.dev .gitignore (shortened)
web/app/plugins/*
web/app/themes/*
!web/app/themes/4wpdev/
web/app/uploads/*
web/wp
.env
.env.*
!.env.example
/vendor
auth.json

// 03 — Branches and environments

A branch is an environment

On 4wp.dev two branches map directly to two environments. Pick one to see what happens after a push.

Every push deploys to staging

The Deploy staging workflow starts on every push to staging. It runs the quality gate first and deploys only if it passes.

  1. git push origin staging
  2. Quality gate: bin/checks.sh
  3. composer install --no-dev
  4. Deploy to staging

// 05 — Pre-commit hook

Stop secrets before they leave your machine

Our hook lives in the repository (.githooks/pre-commit) and is switched on by composer install. It runs the same script as CI, but only on staged files.

1

Forbidden files

.env, keys, auth.json, SQL dumps

2

Secrets in code

Private keys, GitHub tokens, cloud and API keys

3

PHP syntax

php -l on every changed PHP file

4

Composer

composer validate: the lock file is in sync

# .githooks/pre-commit
exec bash "$(git rev-parse --show-toplevel)/bin/checks.sh" --staged
# composer.json
"hooks": "git config core.hooksPath .githooks",
"post-install-cmd": ["@hooks"]

A local hook can be skipped. That is why GitHub Actions runs the same bin/checks.sh on all tracked files before every deploy.

// 06 — Rules we work by

Three rules

No secrets in Git or chat

Passwords and keys never go into a commit. The checks block them.

Pull before you merge locally

Do not mix merging a pull request on GitHub with a local merge without git pull first.

Test after the deploy

Activate plugins and test the site only after a successful deploy.

// 07 — Videos

Git in short videos

All Git Basics videos →

// 08 — LMS4WP course · 7 modules · free

Git for WordPress Developers

From the first commit to auto-deploy, with a terminal in every lesson where you type the commands yourself.

Open the course
  1. First repository
  2. Daily workflow
  3. Branches and conflicts
  4. GitHub and remotes
  5. Pull requests
  6. Clean history
  7. CI/CD on GitHub

Every chapter has a short video. Start with the course trailer.

// 09 — FAQ

Questions

Should WordPress core be in the repository?

No. Composer installs it by version, like any other dependency.

How do we get the database into Git?

We do not. The database and uploads are synced between environments separately, with a database backup before each production deploy.

Is a pre-commit hook enough?

No. It is the fast first check; CI repeats it where it cannot be skipped.

Why doesn’t the hook run after I clone the project?

Git does not turn on hooks from a repository by itself. On 4wp.dev composer install runs git config core.hooksPath .githooks for you.

Can a developer skip the hook?

Yes, with git commit --no-verify. The same checks still run in GitHub Actions before any deploy.