Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 157Git · CI/CD

How to park unfinished work with git stash

You are in the middle of a change and an urgent bug comes in. Committing half-done work is ugly; losing it is worse. git stash puts your uncommitted changes aside, gives you a clean tree, and brings them back later. This how-to stashes with a name (also new files), lists and restores stashes, turns one into a branch, and avoids the classic "where did my stash go?".

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

157.1

The quick answer

git stash push -u -m "wip: login rate limit"
git switch main && git switch -c fix/urgent-bug
git switch feat/login-limit
git stash list
stash@{0}: On feat/login-limit: wip: login rate limit
git stash pop

-u = also stash untracked (new) files; -m = a name you will recognise later.

Commando's in deze les

157.2

Step by step

  1. Stash with a name (quick answer). Without -u, new files stay in the working tree and travel with you to the other branch.
  2. Only some files:
git stash push -m "wip: config only" -- app/config.py
git stash push --staged -m "staged part"
  1. Look before you restore:
git stash list
git stash show -p stash@{1}
  1. Restore:
CommandDoes
git stash popApply 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.
  1. 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
  1. A dropped stash by mistake can often be found again (it was a commit):
git fsck --no-reflog | awk '/dangling commit/ {print $3}' | head
git stash apply <that commit id>
  1. 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.

157.3

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.

Commando's in deze les

157.4

Common problems

ProblemCauseFix
New files still there after stashUntracked files are not stashed by default.git stash push -u.
pop gave conflictsCode 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 branchSwitched branches before pop.Undo (git checkout -- . / git stash again), switch, apply.
Ignored files (like .env) not stashedIgnored files are left alone.-a stashes ignored files too (careful with big folders).
Lost a stash after dropNot in any list.git fsck --no-reflog (step 6).

Commando's in deze les

157.5

Go further

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.

Commando's in deze les

Alle commando's in dit hoofdstuk