Using Git Stash to Temporarily Store Uncommitted Changes

Stashing Work in Git

In Git, the git stash command allows you to temporarily shelve changes you've made to your working directory and staging area. This is useful when you need to switch contexts or branches but aren't ready to commit your current work. The command saves your modifications, restores your working directory to the state of the last HEAD commit, and lets you reapply the changes later.

Core Stashing Commands

Stashing Current Modifications

To save both tracked and staged changes, use:

git stash push -m "Descriptive message"

This creates a new stash entry with a custom message and cleans your working directory.

Stashing Unstaged Changes Only

If you want to keep staged changes while stashing unstaged modifications:

git stash push --keep-index

Listing Stash Entries

To view all stashed changes:

git stash list

Each entry appears with an identifier like stash@{0}.

Inspecting Stash Contents

To examine a specific stash's changes:

git stash show -p stash@{1}

The -p flag displays the full diff instead of just a summary.

Reapplying Stashed Changes

To restore the most recent stash without removing it:

git stash apply

To apply a specific stash:

git stash apply stash@{2}

Applying and Removing Stash

To restore and immediately remove the latest stash:

git stash pop

Removing Stash Entries

To delete a specific stash:

git stash drop stash@{0}

To clear all stashes:

git stash clear

Practical Workflow Example

Consider this scenario: You're working on feature-x when an urgent bug fix is neeeded on main:

  1. Save current work:
git stash push -m "Work in progress on feature-x"
  1. Switch to main branch:
git checkout main
  1. After fixing the bug:
git commit -am "Fix critical bug"
git checkout feature-x
  1. Restore your work:
git stash pop

Common Use Cases

  • Context Switching: Quickly move between branches without committing incomplete work
  • Emergency Fixes: Park feature development to address high-priority issues
  • Experimentation: Test different approaches without affecting the main work
  • Cleen Pulls: Stash changes before pulling to avoid merge conflicts

Advanced Stashing Techniques

To stash only specific files:

git stash push -m "Partial stash" -- path/to/file.js

To create a branch from a stash:

git stash branch new-branch stash@{1}

This applies the stash to a new branch, which can help when resolving conflicts.

Tags: Git version-control stash git-workflow

Posted on Sat, 31 Oct 2026 16:33:14 +0000 by sanstenarios