What is `git stash`, and when would you use it?
Show answer — try answering out loud first
git stash temporarily saves your uncommitted changes (both working directory and staging) and leaves your directory clean, as it was at the last commit. You use it when you need to switch context suddenly (for example, fixing an urgent bug on another branch) but your current work is not yet ready for a commit.
You're halfway through a feature, with unfinished changes, and suddenly something urgent needs fixing on another branch. You don't want to commit incomplete code, but Git won't let you switch branches with conflicting changes. Solution: git stash.
git stash takes your pending changes, saves them on a separate stack, and cleans your working directory. Then you can switch branches, do the urgent work, come back, and recover your changes with git stash pop.
git stash: save and clean.git stash list: see what you have saved.git stash pop: recover the last stash and remove it from the stack.git stash apply: recover it but keep it on the stack.
# You have half-finished changes and need to switch branches urgently
git stash
# Output: Saved working directory and index state WIP on feature: ...
# Your working directory is now clean
# You switch branches, do the urgent work, come back
git switch main
# ...urgent fix, commit, push...
git switch feature
# Recover your saved changes
git stash pop
# Your changes come back and the stash is removed from the stack
# View the list of saved stashes
git stash list
# stash@{0}: WIP on feature: 1a2b3c4 ...
# Save with a descriptive message
git stash push -m "formulario a medias"
# Also include untracked (new) files
git stash -u
Forgetting that a normal git stash does not include untracked files (new ones, without a prior git add); to include them you must use git stash -u. Another mistake is piling up many unnamed stashes and then not knowing which is which (that's why it's good to use -m with a message). Also, confusing pop (recover and delete) with apply (recover and keep).
"
git stashtemporarily saves my uncommitted changes and leaves the working directory clean. I use it when I'm in the middle of something and an urgent bug lands on another branch: I don't want to commit incomplete code, so I stash, switch branches, resolve the urgent issue, come back, and rungit stash popto recover my work. I usepopto recover and delete, orapplyto keep it, and-uif I want to include new files."
You ran git stash, but you have a new file notas.txt that you never added to Git. After stashing, you notice that notas.txt is still there. Why?
See answer
Because by default git stash does not include untracked (new, unmonitored) files. It only saves changes to files that are already tracked. For notas.txt to also be saved in the stash, you would have to use git stash -u (or --include-untracked).