Git Foundations / Branch and Merge

Set work aside with stash

Lesson 18 of 33. Open to read. No account needed.

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.

  1. 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
    
  2. Look at the unfinished work:

    git status
    

    contact.md is modified and form-fields.txt is untracked.

  3. Try a plain stash:

    git stash
    git status
    git stash list
    

    Git prints Saved working directory and index state WIP on contact-form: and the last commit. The list has one entry. But git status still lists form-fields.txt as untracked: the plain stash left the new file behind.

  4. 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 list
    

    git status now reports a clean working tree, and the list shows stash@{0}: On contact-form: Contact form in progress.

  5. 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
    
  6. Restore the unfinished work:

    git stash pop
    git status
    git stash list
    

    Both changes are back, and git stash list prints nothing.

  7. Finish the job by committing it:

    git add contact.md form-fields.txt
    git commit -m "Add opening hours and form fields"
    
  8. 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.md
    

    Git reports a CONFLICT in contact.md and says the stash entry is kept. The file shows the committed line after <<<<<<< Updated upstream and your stashed line before >>>>>>> Stashed changes.

  9. 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 list prints 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 seeLikely causeNext step
A new file is still there after stashingUntracked files need -ugit stash pop, then git stash push -u -m "<message>"
No local changes to saveNothing to stash, or only untracked files without -uCheck git status; add -u for new files
No stash entries found.The list is empty; the work was already restoredCheck git status and git log
CONFLICT after git stash popThe same lines changed since stashingResolve, git add, then git stash drop
An old entry is still in git stash listIt was applied with apply, or a pop conflictedCheck the work is back, then git stash drop
  1. 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

  1. You ran git stash, switched branches, and found your new file notes.txt still in the folder. Why was it not stashed, and what should you have run?
  2. git stash pop reported 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

  1. notes.txt was untracked, and a plain git stash saves only changes to tracked files. git stash push -u -m "<message>" would have included it.
  2. Yes. A conflicting pop keeps the entry, and Git says so. After resolving the conflict and staging the file, run git stash drop to remove the entry.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy