Week 7 · Lesson 9 of 10

Preserving Unfinished Work with Git Stash

0% Complete

Core Idea

Git stash temporarily sets aside uncommitted work so that you can switch tasks or branches without committing incomplete changes or discarding them.

It solves a specific interruption problem: the current work is valuable, but it is not ready to become a commit, and another branch needs immediate attention.

How It Works

Suppose you are editing 1.txt on main and the change is only half complete. You then try to switch to development:

git checkout development

Git may refuse because the local change would be overwritten by the checkout. The refusal protects the unfinished work.

Store the pending change temporarily with:

git stash

The modification disappears from the working directory, leaving a clean state that allows the branch switch. The work has not been deleted; it has been moved into the stash.

Now switch branches:

git checkout development

Perform the review or other task, then return:

git checkout main

Restore the most recent stash and remove it from the stash list with:

git stash pop

The unfinished changes reappear in the working directory.

Pop Versus Apply

Git stash pop performs two actions:

  1. Restore the selected stashed changes.
  2. Remove that stash entry from the stash list.

Git stash apply restores the changes but keeps the stash entry:

git stash apply

Apply is useful when the same stored change may need to be reused or retained as a temporary copy.

Listing and Removing Stashes

View saved stashes with:

git stash list

Git displays multiple stash entries in order, with the newest at the top and older entries below it.

Pop or apply a specific entry by supplying its stash identifier:

git stash pop <stash-identifier>

or:

git stash apply <stash-identifier>

Remove a stash entry without applying it with:

git stash drop

After drop, check the remaining entries with git stash list.

Why It Matters

A branch switch can be blocked when uncommitted work would be overwritten. The poor alternatives are to discard the work or create a premature commit that does not represent a finished unit.

Stash provides a third option: preserve the current work outside the working directory, handle the interruption, and resume later.

It separates task switching from project history. The unfinished state is held temporarily rather than presented as a completed commit.

Practical Application

A complete interruption workflow is:

  1. Confirm the current modifications.

git status

  1. Stash the unfinished work.

git stash

  1. Verify that the working directory is ready for switching.

git status

  1. Switch to the urgent branch.

git checkout development

  1. Complete the review or task.
  2. Return to the original branch.

git checkout main

  1. Restore the unfinished work.

git stash pop

  1. Continue editing, then stage and commit when the work is complete.

To preserve the stash after restoration, replace pop with apply:

git stash apply

Then Inspect

git stash list

The entry remains available.

Decision Logic

Use pop when the stash should be consumed and removed after restoration.

Use apply when the changes should return but the stash entry should remain.

Use list when multiple stashes exist and you need to identify the intended one.

Use drop when a stash is no longer needed and should be deleted without restoring it.

Trade-Offs and Limitations

A stash is temporary storage, not a normal commit in project history. It is useful for interrupted work, not as a replacement for meaningful commits.

Pop removes the stash entry after restoration. If you intend to keep a reusable stored copy, use apply instead.

Multiple stashes are ordered, and the most recent one is restored first by default. When the latest stash is not the intended one, select a specific identifier.

Stashing solves branch-switching pressure, but the work still needs to be completed, staged, and committed later if it should become part of the repository history.

Key Takeaway

Use git stash to pause incomplete work safely. Pop restores and removes a stash; apply restores and keeps it; list shows available entries; and drop deletes an entry that is no longer needed.

Back to top