Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
Step by step
- Stash with a name (quick answer). Without
-u, new files stay in the working tree and travel with you to the other branch. - Only some files:
- Look before you restore:
- Restore:
| Command | Does |
|---|---|
git stash pop | Apply the newest stash and delete it (if no conflict). |
git stash apply stash@{1} | Apply a stash and keep it. |
git stash drop stash@{1} | Delete one stash. |
git stash branch NAME stash@{1} | Make a new branch from the commit where you stashed, apply the stash there. |
- Conflicts on pop — the stash is kept; fix the files,
git add, and drop the stash yourself:
git stash pop
CONFLICT (content): Merge conflict in app/config.py
git add app/config.py
git stash drop- A dropped stash by mistake can often be found again (it was a commit):
- Do not use stash as long-term storage — after a few days a WIP commit on a branch (
git commit -m "WIP", later squashed) is safer and pushed.
Check that it worked
After push, git status is clean; after pop, your changes are back (git diff shows them) and git stash list no longer lists that stash.
Common problems
| Problem | Cause | Fix |
|---|---|---|
| New files still there after stash | Untracked files are not stashed by default. | git stash push -u. |
pop gave conflicts | Code changed since you stashed. | Resolve, git add, then git stash drop. |
| Many unnamed stashes, which is which? | Stashed without -m. | git stash show -p stash@{N}; name them next time. |
| Stash applied on the wrong branch | Switched branches before pop. | Undo (git checkout -- . / git stash again), switch, apply. |
Ignored files (like .env) not stashed | Ignored files are left alone. | -a stashes ignored files too (careful with big folders). |
Lost a stash after drop | Not in any list. | git fsck --no-reflog (step 6). |
Go further
- Command pages: git.
- Next how-tos: Work with branches and pull requests, Undo the last Git commit, Rebase a branch on main.
- Learn it: Git — stash, Git — worktree.
- Practise: Lab: bisect, stash and a release tag.
1Which option also stashes new (untracked) files?
-u (--include-untracked).
2What is the difference between git stash pop and git stash apply?
pop applies and deletes the stash; apply applies and keeps it.