Ask Steves · Daily notes · VPS, 20% off · AI Profit Lab · SEO · Money Hub

12 November 2026 · One command a day

How do I put my changes aside in git so I can pull or switch branches?

git stash takes your uncommitted changes, saves them somewhere safe and leaves your working folder clean. You reach for it when git pull or git switch refuses to run because your local edits would be overwritten. After that you bring the changes back with one more command.

I learn my automation tips in the AI Profit Boardroom. Have a look here. Affiliate link.

The command

git stash push -u -m "before pull"

git stash on its own does the same thing as git stash push. push saves the changes and resets your files to the last commit. -u (short for --include-untracked) also stashes new files that git is not tracking yet. Without it, those files stay where they are and can get in the way. -m "before pull" adds a label so you can tell stashes apart later. Run it from inside the repository. If the repository belongs to another user, run it as that user, not with sudo.

What you will see

$ git stash push -u -m "before pull"
Saved working directory and index state On main: before pull
$ git stash list
stash@{0}: On main: before pull
stash@{1}: WIP on main: 3f9c2a1 Update nginx config for example.com
$ git pull
Already up to date.
$ git stash pop
On branch main
Changes not staged for commit:
        modified:   config/app.php
Dropped refs/stash@{0} (9b1e4d7c2f...)

The first line confirms the save and shows which branch you were on. git stash list shows every stash, newest first, as stash@{0}, stash@{1} and so on. The second entry has no label, so git used the last commit message instead. git stash pop puts the newest stash back into your files and deletes it from the list. The "Dropped" line tells you it is gone from the list.

When to use it

Before you stash, it helps to see what has changed in the repository. If you just want one file's changes gone for good, throw them away instead of stashing.

Watch out for

If git stash pop hits a conflict with the code you just pulled, git does not delete the stash. Your files will contain conflict markers. Fix them, run git add on those files, then run git stash drop to remove the stash by hand. If you forget, the stash stays in the list and you may apply it again by mistake later. Use git stash apply instead of pop if you always want to keep a copy until you are sure.

Questions people ask

Does git stash save my .env file?

Not if it is in .gitignore. Ignored files are left alone unless you use -a (--all), which is rarely what you want.

How do I see what is inside a stash before I pop it?

Run git stash show -p stash@{0}. It prints the stash as a diff without changing any files.

Pulling code straight onto a live box and juggling local edits like this is the kind of thing you can only do on a server you control. You can get the same VPS with 20% off at https://asksteves.co.uk/vps. Affiliate link.

Want more automation tips? The AI Profit Boardroom on Skool is where I get mine. Have a look here. Affiliate link.

Everything on this blog runs on one Hostinger KVM 8. This link takes 20% off the same box.

GET A VPS, 20% OFF →

Affiliate link.

Related server notes: How do I throw away changes to one file in git? · How do I follow a log file on Linux and still scroll back? · How do I make PM2 apps start again after a reboot? · All daily notes