Stashing Changes

Stashing Changes

Git Stash

Introduction

Stashing temporarily saves changes that are not ready to commit. This allows you to switch branches without committing.

Basic Stash

Save Stash

git stash

Saves all uncommitted changes and cleans the working directory.

With Message

git stash save "Work in progress on login"

Or in newer Git:

git stash push -m "Work in progress on login"

Include Untracked Files

git stash -u

Or:

git stash --include-untracked

Include Ignored Files

git stash -a

Viewing Stashes

List All Stashes

git stash list

Output:

stash@{0}: WIP on main: abc1234 Previous work
stash@{1}: On feature: def5678 Feature changes

Show Stash Contents

git stash show

Shows file changes in last stash.

Detailed View

git stash show -p

Shows actual diff of stashed changes.

Show Specific Stash

git stash show stash@{1}

Applying Stashes

Apply Latest Stash

git stash apply

Applies changes but keeps stash.

Apply and Remove Stash

git stash pop

Applies changes and removes from stash list.

Apply Specific Stash

git stash apply stash@{2}

Apply to Different Branch

git stash branch new-branch stash@{0}

Creates new branch and applies stash.

Dropping Stashes

Remove Latest Stash

git stash drop

Remove Specific Stash

git stash drop stash@{2}

Clear All Stashes

git stash clear

⚠️ This permanently deletes all stashes!

Advanced Stashing

Stash Specific Files

git stash push -m "Message" -- file1.txt file2.txt

Stash Including Index

git stash --index

Preserves staged changes separately.

Create Stash Branch

git stash branch branch-name stash@{0}

Creates branch, applies stash, then drops it.

Practical Examples

Example 1: Switch Branches with Uncommitted Work

# Working on feature
git checkout -b feature-login

Make changes...

git add .

Need to switch but not ready to commit

git stash

Switch to main

git checkout main

Do other work...

git checkout main

Return to feature

git checkout feature-login git stash pop

Example 2: Pull with Uncommitted Changes

# Want to pull but have changes
git stash
git pull origin main
git stash pop

Example 3: Multiple Work in Progress

# First task
git stash push -m "Task 1: login form"

Make changes for task 2

git add . git stash push -m "Task 2: validation"

Work on task 3

...

See all stashes

git stash list

Work on task 1

git stash apply stash@{1}

Example 4: Compare Stash with Current

git stash show -p stash@{0}

Shows what was in stash vs current.

Stash and Merge Conflicts

If conflicts occur during apply:

# Conflict files are staged

Resolve conflicts manually

git add resolved-files git stash drop # Or continue with git stash pop

When to Use Stash

  • Switching branches with uncommitted work
  • Pulling changes when you have local modifications
  • Temporarily setting aside work
  • Working on something quickly before finishing current task

Summary

Essential stash commands:

  • git stash - Save uncommitted changes
  • git stash pop - Apply and remove latest stash
  • git stash apply - Apply changes, keep stash
  • git stash list - Show all stashes
  • git stash drop - Delete a stash
  • git stash clear - Delete all stashes
Stash is essential for flexible workflow management.

Next Lesson

Learn about popular Git workflows used in teams.

Quiz - Quiz - Stashing Changes

1. What does 'git stash' do?

2. What is the difference between 'git stash apply' and 'git stash pop'?

3. What command shows all stashes?

4. How do you include untracked files in a stash?

5. What does 'git stash clear' do?

Reset, Revert, and Restore