Setting Work Aside
You are halfway through changing the sightings layout when a club member messages: the meeting time on the home page is wrong again, and the meeting is tomorrow.
Your working directory is full of half-finished work. You do not want to commit it, because it does not work yet, and you do not want to throw it away either. git stash is the drawer: it takes your uncommitted changes off the desk, hands you a clean working directory, and gives them back whenever you ask.
Stashing
git stash push -m "half-finished sightings layout" git status
Status comes back clean: no modified files, nothing staged. The project looks exactly as it did at your last commit, and the urgent fix can be made against a known state rather than on top of a mess.
The -m and its label are optional. They are also the difference between a drawer you can use and a drawer full of unlabelled envelopes, so use them.
Doing the urgent thing
git add index.html git commit -m "Correct the meeting time to Thursday"
Getting it back
git stash pop
Your half-finished layout comes back into the working directory, and the stash is removed from the drawer. Carry on where you left off.
If there is more than one thing in there:
git stash list
stash@{0}: On main: half-finished sightings layout
stash@{1}: On main: experiment with the header colours
A plain git stash pop takes the newest. You can name a specific one — git stash pop stash@{1} — when the newest is not the one you want.
When a commit is the better tool
Stash is for minutes and hours. It is the wrong tool for days.
A stash is invisible in git log, unlabelled unless you labelled it, and easy to forget completely. Work that is coherent enough to describe should be committed instead — and once Chapter 6 gives you branches, "commit it on a branch and come back later" becomes the better answer to almost every version of this problem.
The honest rule: if you can write a commit message for it, commit it. If you cannot, and you need the desk clear for an hour, stash it.
The warning
Stashes are local. They are never pushed, never shared, and never part of the history. A forgotten stash is the most common way beginners "lose" work that is technically still there.
So when something you definitely wrote seems to have vanished, git stash list is worth running alongside git reflog from the last chapter. Between them they find nearly everything.
Pop and apply
One more word you will meet. git stash apply brings the work back and keeps the stash in the drawer; pop brings it back and removes it. Use pop unless you have a specific reason to want the copy kept, in which case you already know why.
- "Stashing commits my work." Nothing enters the history. A stash is a private drawer on your machine — not a commit, not a branch, and not visible in
git log. - "A stash belongs to the branch I made it on." The drawer is per-repository. You can stash on one branch and pop on another, which is occasionally useful and occasionally surprising.
- "
popandapplyare the same."popreapplies and empties that drawer;applyreapplies and leaves the stash where it is. - "A stash is a good place to keep work for a few weeks." It is a good place for an hour. Unlabelled, invisible in the log, never pushed, and — because a stash is stored as a reflog entry — eventually eligible for the same routine cleanup that prunes old reflog entries. Anything longer belongs on a branch.
- Chapter 6 switches branches constantly, and a working directory full of unfinished changes is the most common thing that blocks a switch. Stash is the standard answer to that block.
- Preferring a commit for real work is what keeps things recoverable, because commits are in the log and reachable by the reflog while stashes are neither.
- Knowing that
git stash listexists rescues the specific and demoralizing case of work that is not lost, just filed somewhere you forgot about.
Knowledge Check
What does git stash push do to your working directory?
- It clears the uncommitted changes and stores them where you can get them back
- It commits the changes to the current branch with an automatically generated message
- It copies the changes to a second folder on disk and deletes the originals
- It marks the changes as unfinished so that they cannot be committed by mistake
You stash some work and then forget about it for a month. What has happened to it?
- It is still sitting there in the drawer, and
git stash listwill show it - It was discarded automatically once the working directory was clean again
- It was applied automatically the next time you switched branches in the project
- It was pushed to GitHub with your next push and is stored with the project
When is committing the better choice than stashing?
- Whenever the work is coherent enough that you could write a message for it
- Whenever the changes touch more than one file at a time in the project
- Whenever you plan to come back to the work later in the same working day
- Whenever the branch you are on has already been pushed to a remote copy
What is the difference between git stash pop and git stash apply?
popreapplies the work and removes the stash;applykeeps it in the drawerpoptakes the oldest stash andapplytakes the newest one availablepopworks only on the branch where the stash was made,applyanywherepopdiscards conflicting changes whileapplystops and reports them
You got correct