git stash: Save, List, Apply and Pop Work in Progress

git stash lets you put unfinished work aside, switch tasks and come back later without making messy commits. In this lesson you will save, inspect and restore stashes, handle untracked files and conflicts, and learn when a WIP commit is better.

Advertisement

๐Ÿ“– What You’ll Learn: what git stash is, basic stash and pop, stashing with a message, listing and inspecting stashes, pop vs apply, drop and clear, untracked and partial stashes, git stash branch, conflicts when popping, and common real-world situations.


๐Ÿ“ฆ What Is git stash?

You are halfway through a change when an urgent bug report arrives, or you need to pull, or switch branches, but your work is not ready to commit. git stash saves your uncommitted changes on a stack, cleans your working directory, and lets you bring the changes back later.

Think of it as a clipboard for unfinished work.


โ–ถ๏ธ Basic Usage

git status                 # modified: tests/login.spec.ts
git stash                  # save the changes and clean the working directory
git status                 # nothing to commit, working tree clean

# ...switch branch, pull, fix the urgent bug...

git stash pop              # bring the changes back and remove them from the stash
Saved working directory and index state WIP on main: 8b1d4e2 Add learning goals

๐Ÿท๏ธ Stash with a Message

With several stashes, messages tell them apart:

git stash push -m "WIP: login tests for locked-out user"

๐Ÿ“‹ List, Inspect and Choose Stashes

git stash list
stash@{0}: On feature/login: WIP: login tests for locked-out user
stash@{1}: WIP on main: 8b1d4e2 Add learning goals

stash@{0} is always the most recent.

git stash show stash@{1}        # files changed in a stash
git stash show -p stash@{1}     # the full diff

๐Ÿ” pop vs apply

CommandRestores changesRemoves the stash from the list
git stash popโœ…โœ… (only if it applied without conflicts)
git stash applyโœ…โŒ Keeps it, so you can apply it again elsewhere
git stash apply stash@{1}       # apply a specific stash and keep it
git stash drop stash@{1}        # delete one stash
git stash clear                 # delete ALL stashes (cannot be undone)

โš ๏ธ Common mistake: git stash clear and git stash drop permanently delete stashed work. List with git stash list before cleaning up.


๐Ÿงฉ Untracked and Ignored Files

By default, git stash saves only tracked files (modified and staged). New files stay in the working directory.

CommandAlso stashes
git stash -u (--include-untracked)New, untracked files
git stash -a (--all)Untracked and ignored files
git stash push -- tests/login.spec.tsOnly the listed file(s)
git stash push --stagedOnly staged changes (Git 2.35 and newer)
git stash --keep-indexStash, but leave staged changes in place

๐ŸŒฟ Turn a Stash into a Branch

If the code changed a lot since you stashed and pop would conflict, create a branch from the commit where you stashed and apply it there:

git stash branch feature/login-wip stash@{0}

๐Ÿงฏ Conflicts When Popping

If the stashed changes clash with changes made since, git stash pop reports conflicts like a merge. Resolve them as in Resolve Merge Conflicts, then git add the files. The stash is not dropped automatically when there were conflicts; drop it yourself once you are happy: git stash drop.


๐Ÿงช Typical Situations

SituationCommands
Pull when you have local editsgit stash, git pull, git stash pop
Urgent fix on another branchgit stash -u, git switch main, fix and commit, git switch -, git stash pop
Started work on the wrong branchgit stash, git switch correct-branch, git stash pop
Try an idea quickly and discard itgit stash to save current work, experiment, git restore ., git stash pop

๐Ÿ’ก Tip: Stashes are local and easy to forget. For anything longer than a quick interruption, a WIP commit on a feature branch is often safer: you can tidy it later with interactive rebase.


๐Ÿ“ Assignment

  1. Modify two tracked files, stash them, confirm a clean git status, and restore them with pop.
  2. Create three stashes with messages and list them.
  3. Inspect the second stash with git stash show -p, apply it with apply, and drop it.
  4. Create a new untracked file and show that git stash ignores it but git stash -u saves it.
  5. Simulate “started on the wrong branch”: edit on main, stash, switch to a new branch, pop.
  6. Bonus: create a conflict when popping a stash and resolve it.

โ“ FAQ

๐Ÿค” What does git stash do?

It saves your uncommitted changes in a temporary stack and restores a clean working directory, so you can switch tasks and reapply the changes later.

๐Ÿค” What is the difference between git stash pop and git stash apply?

Both restore the changes. pop also removes the stash from the list; apply keeps it.

๐Ÿค” How do I stash untracked files?

Use git stash -u (or --include-untracked).

๐Ÿค” How do I name a stash?

git stash push -m "your message".

๐Ÿค” Can I recover a dropped stash?

Sometimes. Dropped stashes become unreachable commits; git fsck --unreachable can list them while they still exist. It is much easier not to drop stashes you might need.


๐ŸŽฏ Next Steps

๐ŸŽฌ Learn Git and GitHub step by step: watch the series on YouTube!

Subscribe to SqaToolsByDY • Practice on sqatools.in

3 thoughts on “git stash: Save, List, Apply and Pop Work in Progress”

Leave a Comment