Git stash in a WordPress workflow
You are halfway through a new header in the theme, and a bug report about the checkout comes in. The changes are not ready to commit, but you need a clean working tree to switch branches. git stash puts the uncommitted changes aside and gives them back later.
Problem: Uncommitted work in progress blocks a branch switch or a pull, and committing half-done code just to get it out of the way pollutes the history.
Solution: Save the changes with git stash push (with -u for new files and -m for a description), do the urgent work, and bring the changes back with git stash pop.
The basic commands
git stash push -m "WIP: new header" # modified and staged tracked files
git stash push -u -m "WIP: new header" # also new, untracked files
git stash list
# stash@{0}: On feature/header: WIP: new header
git stash pop # apply the latest entry and remove it
git stash apply stash@{1} # apply an entry and keep it in the list
git stash drop stash@{1} # delete one entry
Plain git stash is the same as git stash push without a message. Untracked files, for example a new template-parts/header-new.php, stay in the working tree unless you add -u. Ignored files (node_modules, uploads) are never stashed without -a, which you rarely want.
The urgent fix
git stash push -u -m "WIP: new header" git switch main git pull git switch -c hotfix/checkout-total # fix, commit, push, open the pull request git switch feature/header git stash pop
Only some files
git stash push -m "only the header" -- wp-content/themes/hello/header.php git stash push --staged -m "what is staged" # Git 2.35+
Look inside and recover
git stash show stash@{0} # changed files
git stash show -p stash@{0} # the full diff
git stash branch feature/header-wip stash@{0}
git stash branch creates a branch from the commit where the stash was made and applies the stash there. It is the easy way out when pop on a branch that has moved on gives conflicts.
If git stash pop stops with a conflict, Git keeps the entry in the list. Resolve the conflict, git add the files, and drop the entry yourself with git stash drop.
NOTE: The stash is local and easy to forget. An entry that sits in the list for weeks is work that belongs in a commit on its own branch. git stash clear deletes all entries without asking, so check git stash list first. More everyday commands: essential Git commands for WordPress developers.