When You Need Your Hands Free
You are halfway through a change when something urgent comes up on another
branch. The work is not ready to commit, and switching branches would either
carry the unfinished edits along or be refused. git stash puts the
unfinished work on a shelf and gives you a clean working tree; later you take
it back down and carry on.
Stashing and Restoring
git stash push -m "<what you were doing>" # shelve the work
git stash list # see what is on the shelf
git stash pop # put the newest entry back and remove it
git stash on its own does the same as git stash push without a message.
After stashing, git status reports a clean working tree, and your files match
the last commit.
Each stash is an entry, listed newest first as stash@{0}, stash@{1} and
so on. An entry with a message reads On <branch>: <message>; without one, Git
writes WIP on <branch>: followed by the last commit's ID and summary. A clear
message matters when you come back days later.
To take an entry back without removing it from the list, use apply, then
drop once you are happy:
git stash apply # restore the newest entry; keep it on the list
git stash drop # remove the newest entry from the list
New Files Are Not Stashed by Default
A plain git stash saves changes to tracked files, staged or not. A new
file you have never added is untracked, so it stays in your folder, where it
can get mixed up with the other branch's work. Include untracked files with
-u:
git stash push -u -m "<what you were doing>"
When Restoring Conflicts
If the files changed while the work was on the shelf, for example because you
committed a change to the same lines, putting it back can conflict. Git marks
the file as in a merge: <<<<<<< Updated upstream introduces the version in
your files now, and >>>>>>> Stashed changes ends the stashed version. It also
says The stash entry is kept in case you need it again.
Resolve it as you would a merge conflict: write the right content, remove the
markers, check with git diff --check and stage the file with git add. Then
remove the entry yourself with git stash drop, because a conflicting pop
does not remove it.
Stash or Commit?
A stash is a short-term shelf. It is not shared when you share a branch, and
entries are easy to forget. For a quick interruption, stash. For anything
longer, commit the unfinished work on its branch with a clear message, such as
"Work in progress: contact form", and tidy the commit later, for example with
git commit --amend.
Exercise: Switch Tasks Without Losing Work
You are adding details to a bakery's contact page on the contact-form branch
when someone reports a typo on the home page, which lives on main.
Starting state: Git configured as in the configuration lesson.
-
Run the setup. It ends with an edited file and a new one, uncommitted:
mkdir -p ~/git-practice/stash-practice cd ~/git-practice/stash-practice git init -b main cat > index.md <<'EOF' # Welcom to the bakery EOF cat > contact.md <<'EOF' # Contact us Email: hello@example.com EOF git add index.md contact.md git commit -m "Add home and contact pages" git switch -c contact-form cat >> contact.md <<'EOF' Phone: 555-0100 EOF git commit -am "Add phone number" cat >> contact.md <<'EOF' Opening hours: 8:00 to 16:00 EOF cat > form-fields.txt <<'EOF' name email message EOF -
Look at the unfinished work:
git statuscontact.mdis modified andform-fields.txtis untracked. -
Try a plain stash:
git stash git status git stash listGit prints
Saved working directory and index state WIP on contact-form:and the last commit. The list has one entry. Butgit statusstill listsform-fields.txtas untracked: the plain stash left the new file behind. -
Put the work back, then stash everything, new file included, with a message:
git stash pop git stash push -u -m "Contact form in progress" git status git stash listgit statusnow reports a clean working tree, and the list showsstash@{0}: On contact-form: Contact form in progress. -
Make the urgent fix on
main, then come back:git switch main cat > index.md <<'EOF' # Welcome to the bakery EOF git commit -am "Fix welcome typo" git switch contact-form -
Restore the unfinished work:
git stash pop git status git stash listBoth changes are back, and
git stash listprints nothing. -
Finish the job by committing it:
git add contact.md form-fields.txt git commit -m "Add opening hours and form fields" -
Now a stash that conflicts. Stash an edit to the email line, then commit a different change to the same line, and pop:
cat > contact.md <<'EOF' # Contact us Email: hello@example.com (we reply within a day) Phone: 555-0100 Opening hours: 8:00 to 16:00 EOF git stash push -m "Promise a reply time" cat > contact.md <<'EOF' # Contact us Email: orders@example.com Phone: 555-0100 Opening hours: 8:00 to 16:00 EOF git commit -am "Use the orders address" git stash pop git status cat contact.mdGit reports a
CONFLICTincontact.mdand says the stash entry is kept. The file shows the committed line after<<<<<<< Updated upstreamand your stashed line before>>>>>>> Stashed changes. -
Both changes are wanted: the new address and the reply promise. Resolve, check, stage, then drop the entry and commit:
cat > contact.md <<'EOF' # Contact us Email: orders@example.com (we reply within a day) Phone: 555-0100 Opening hours: 8:00 to 16:00 EOF git diff --check git add contact.md git stash drop git stash list git status git commit -m "Promise a reply time"git stash listprints nothing, and the combined line is committed.
Success: the home page fix is on main, the contact page work was restored
on contact-form with its new file, and git stash list is empty.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| A new file is still there after stashing | Untracked files need -u | git stash pop, then git stash push -u -m "<message>" |
No local changes to save | Nothing to stash, or only untracked files without -u | Check git status; add -u for new files |
No stash entries found. | The list is empty; the work was already restored | Check git status and git log |
CONFLICT after git stash pop | The same lines changed since stashing | Resolve, git add, then git stash drop |
An old entry is still in git stash list | It was applied with apply, or a pop conflicted | Check the work is back, then git stash drop |
-
Clean up:
cd ~/git-practice ls rm -rf stash-practice ls
Independent task: before cleaning up, add a line to contact.md:
cat >> contact.md <<'EOF'
Closed on public holidays.
EOF
Shelve it with the message "Holiday note", show the list, restore it in a way that keeps the entry on the list, show the list again, and finally remove the entry so the list is empty.
Check Your Understanding
- You ran
git stash, switched branches, and found your new filenotes.txtstill in the folder. Why was it not stashed, and what should you have run? git stash popreported a conflict. Is the entry still on the list, and what must you do after resolving?
Answer and Reasoning
Independent task
git stash push -m "Holiday note"
git stash list
git stash apply
git stash list
git stash drop
git stash list
The first list shows stash@{0}: On contact-form: Holiday note. After
apply, the line is back in contact.md and the entry is still listed. After
drop, the list is empty and the line remains in the file.
Understanding check
notes.txtwas untracked, and a plaingit stashsaves only changes to tracked files.git stash push -u -m "<message>"would have included it.- Yes. A conflicting pop keeps the entry, and Git says so. After resolving the
conflict and staging the file, run
git stash dropto remove the entry.