Stash Your Changes
git stash push -m "half-done login form"
Git saves the changes to tracked files, staged and unstaged, and resets those files to the last commit. git stash on its own does the same with an automatic message. A message makes the stash easy to find later.
See What Is Stashed
git stash list
git stash show -p stash@{0}
The list shows entries such as stash@{0}, the newest, and stash@{1}, the one before. git stash show without -p prints only a summary of the changed files and line counts.
Apply or Pop
| Command | Changes restored | Stash entry |
|---|---|---|
git stash apply | Yes | Kept |
git stash pop | Yes | Removed |
Both use the newest stash unless you name one: git stash apply stash@{2}. Use apply when you are not sure the changes will fit, then drop the entry yourself once everything looks right.
If the stashed changes conflict with your files, Git marks the conflicts as in a merge. pop then keeps the entry, so nothing is lost. Fix the files, git add them, and drop the stash when you are done.
Include New Files
By default, files Git does not track yet stay where they are. Add them to the stash with -u:
git stash push -u -m "new upload handler"
-a also includes ignored files. Use it with care: ignored folders such as node_modules or vendor can be large. Which files count as ignored is set in .gitignore.
Stash One File
Name the paths after --:
git stash push -m "config experiment" -- config/app.php
Only that file is stashed; other changes stay in place. git stash push --keep-index stashes all changes but leaves the staged ones in your files as well, which lets you test exactly what you are about to commit.
Turn a Stash into a Branch
git stash branch login-form stash@{0}
Git creates a branch at the commit where you made the stash, applies the changes there, and drops the entry if that works. This avoids conflicts when the original branch has moved on.
Drop and Clear
Warning: a dropped stash no longer appears in git stash list or in any reflog. Check git stash list and git stash show -p before you drop.
git stash drop stash@{1}
git stash clear
drop removes one entry; clear removes all of them. When Git drops an entry it prints the entry's commit hash, in the form Dropped stash@{1} (HASH). While that hash is on screen, git stash apply HASH still restores it. Without it, a dropped stash can sometimes be found among unreachable commits with git fsck --unreachable, until garbage collection removes it.
A Typical Server Use
Someone edited a file directly on the server and a pull now fails:
git status
git stash push -m "edits found on server"
git pull --ff-only
git stash show -p
Read the stashed edits, then decide whether to apply them or drop them. Moving the useful ones into the repository keeps the checkout clean for the next deploy. For the difference between pull and fetch, see git pull vs git fetch; to undo a commit instead, see git reset explained.