Git commands every WordPress developer needs
Without version control, a change to a theme on a live site is a gamble: if something breaks and there is no copy of the old file, there is no easy way back. Git keeps the history of every change, lets two developers work on the same theme without overwriting each other, and turns deployment into pushing exact commits instead of copying files by FTP. A dozen commands cover the daily work.
Problem: A theme or plugin is changed directly on the server or by FTP, without history, without rollback and with conflicts between developers.
Solution: Keep the theme or plugin in a Git repository, work with status, add, commit, pull and push, use branches for features, and exclude secrets and uploads with .gitignore.
What goes into the repository
For most projects the repository is the custom theme or plugin folder, not the whole WordPress installation: core and third-party plugins are installed and updated by WordPress itself. A minimal .gitignore for a theme:
node_modules/ vendor/ *.log .DS_Store
If you version the whole site, also exclude wp-config.php (database password), wp-content/uploads/ (media files), caches and backups. A wp-config.php pushed to a public repository is one of the most common ways database passwords leak.
The daily cycle
git status # what changed since the last commit git diff # the changes themselves, line by line git add functions.php # stage one file git add . # stage everything that changed git commit -m "Redirect subscribers to their account page after login" git pull # get the commits of your colleagues git push # publish your commits git log --oneline -10 # the last 10 commits
A commit message describes what the change does and why, so in a year git log answers “why is this here”.
Branches
git switch -c feature/account-page # new branch and switch to it (older Git: git checkout -b) # ... work and commit ... git switch main git merge feature/account-page git branch -d feature/account-page
A branch keeps an unfinished feature away from the code that is deployed. When the work is done, it is merged back, after a review if you work in a team.
Undoing
git restore style.css # discard uncommitted changes in a file (older Git: git checkout -- style.css) git restore --staged style.css # unstage, keep the changes git revert a1b2c3d # a new commit that undoes commit a1b2c3d
git revert is the safe way to undo something that was already pushed: history stays intact and colleagues are not affected. Rewriting pushed history (git reset --hard plus a forced push) causes trouble for everybody who already pulled it.
Deploying
On the server, a clone of the repository in the theme folder is updated with git pull over SSH. That is enough for a small site, as long as nobody edits files on the server directly; otherwise the pull fails with conflicts. Working on the server over SSH is covered in Linux commands every WordPress developer needs.
NOTE: Commit often and small: one commit per change that belongs together. A commit “fix everything” with twenty unrelated changes cannot be reverted partly, while a series of small commits lets you undo exactly the one that caused the problem.