Git Stash: Complete Guide to Temporary Code Storage for Beginners

Master Git stash functionality from scratch. Learn how to temporarily save work, manage multiple stashes, and understand why stashing is essential for clean collaboration in development teams.

18 min read

Have you ever been in the middle of working on a feature when suddenly you need to switch branches or pull the latest changes? You're not ready to commit your incomplete work, but you also can't switch branches with uncommitted changes. This is where Git stash becomes your lifesaver.

๐Ÿ’ก

๐ŸŽฏ What You'll Learn: In this comprehensive guide, you'll discover:

  • What Git stash is and why it's essential for development workflow
  • How stashing compares to committing incomplete code
  • Managing multiple stashes with names and references
  • Advanced stash operations: apply vs pop, selective stashing
  • Best practices for using stash in team collaboration
  • Common pitfalls and how to avoid them

๐Ÿค” Understanding Git Stash: The Problem it Solves

Git stash is a powerful feature that temporarily stores your uncommitted changes, allowing you to switch contexts without losing your work or polluting your commit history with incomplete code.

Prerequisites

Before we dive into stashing, make sure you have:

  • Git installed on your system
  • Basic understanding of git add, git commit, and git status
  • A Git repository to practice with
  • Understanding of branches and working directory concepts

Why Stash Instead of Commit?

Many beginners wonder: "Why not just commit the incomplete work?" Here's why stashing is the better choice:

AspectGit StashIncomplete Commit
History ImpactNo impact on commit historyClutters commit history
Code QualityEncourages complete, tested commitsMay break builds for other team members
Team CollaborationKeeps shared branches cleanCan cause issues when others pull changes
FlexibilityEasy to discard if not neededRequires commit history rewriting

๐Ÿ“‹ Setting Up Our Practice Scenario

Let's start with a practical scenario to understand Git stash. We'll begin by examining our repository state and making some changes.

Checking Repository Status

ls -ahl

Example Output:

total 20K
drwxr-xr-x. 3 centos9 centos9 105 Sep 25 19:08 .
drwxr-xr-x. 3 centos9 centos9  23 Sep 24 13:21 ..
-rw-r--r--. 1 centos9 centos9  15 Sep 24 13:50 fifth.txt
-rw-r--r--. 1 centos9 centos9  23 Sep 24 13:21 first.txt
-rw-r--r--. 1 centos9 centos9  12 Sep 25 19:08 fourth.txt
drwxr-xr-x. 8 centos9 centos9 183 Sep 25 19:08 .git
-rw-r--r--. 1 centos9 centos9  36 Sep 24 13:36 second.txt
-rw-r--r--. 1 centos9 centos9  17 Sep 24 13:33 third.txt

Understanding the listing:

  • Five text files in our repository
  • .git directory contains all Git metadata
  • File timestamps show when they were last modified

Examining File Contents

cat fourth.txt

Example Output:

new changes

This shows our current file content before we make modifications.

๐Ÿš€ Your First Stash: Basic Operations

Now let's create a scenario where stashing becomes necessary by making some changes and then needing to switch contexts.

Making Uncommitted Changes

Let's add some content to our file:

cat <<EOF >> fourth.txt
second line
third line
EOF

What this command does:

  • cat <<EOF >> fourth.txt appends multiple lines to the file
  • >> means append (don't overwrite)
  • Everything between the two EOF markers gets added to the file

Verifying Our Changes

cat fourth.txt

Example Output:

new changes
second line
third line

File status: We've successfully added two new lines to the file.

Checking Git Status

git status

Example Output:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fourth.txt

no changes added to commit (use "git add" and/or "git commit -a")

Understanding Git's message:

  • On branch master: We're currently on the master branch
  • Changes not staged for commit: Git detected file modifications
  • modified: fourth.txt: The specific file that changed
  • Git provides helpful suggestions for next steps

๐Ÿ’พ Creating Your First Stash

Now comes the magic moment - let's stash our changes temporarily.

Basic Stash Command

git stash

Example Output:

Saved working directory and index state WIP on master: e3bd41e Merge branch 'dev'

Understanding the stash output:

  • Saved working directory and index state: Git saved all uncommitted changes
  • WIP on master: "Work In Progress" on the master branch
  • e3bd41e: The commit hash where the stash was created
  • Merge branch 'dev': The commit message of the current HEAD

Verifying the Stash Worked

git status

Example Output:

On branch master
nothing to commit, working tree clean

What happened:

  • Working directory is now clean (no uncommitted changes)
  • All modifications were temporarily saved to the stash
  • We can now safely switch branches or pull updates

Checking File Contents After Stash

cat fourth.txt

Example Output:

new changes

Important observation: The file content reverted to the last committed state. Our added lines are safely stored in the stash, not lost!

๐Ÿ“š Managing Your Stash Stack

Git stash works like a stack (Last In, First Out). Let's learn how to view and manage multiple stashes.

Viewing Your Stash List

git stash list

Example Output:

stash@{0}: WIP on master: e3bd41e Merge branch 'dev'

Understanding stash references:

  • stash@{0}: The most recent stash (index 0)
  • WIP on master: Automatic description of the stash
  • e3bd41e: The commit hash where stash was created

Retrieving Your Stashed Changes

Using git stash pop

git stash pop

Example Output:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fourth.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (33e8ffb8286507344df010dd576bab6bced763a0)

Key behaviors of git stash pop:

  • Restores your changes to the working directory
  • Removes the stash from the stash stack (it's deleted)
  • Shows the stash reference that was dropped

Verifying Changes Were Restored

cat fourth.txt

Example Output:

new changes
second line
third line

Perfect! Our changes are back exactly as we left them.

Confirming Stash Was Removed

git stash list

Expected Output: (empty - no output)

This confirms: The stash stack is now empty because pop removed the stash after applying it.

๐Ÿท๏ธ Working with Named Stashes

For better organization, especially when working with multiple stashes, you can give them descriptive names.

Creating a Named Stash

Let's create another scenario with multiple files:

git status

Current Status:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fourth.txt

no changes added to commit (use "git add" and/or "git commit -a")

We still have our changes from before. Let's stash them with a name:

git stash save "first"

Example Output:

Saved working directory and index state On master: first

Benefits of named stashes:

  • save "first": Creates a stash with a custom message
  • Makes it easier to identify specific stashes later
  • Essential when managing multiple stashes simultaneously

Creating Multiple Stashes

Let's make changes to another file:

cat third.txt

Current Content:

this is new file

Let's add some content:

cat <<EOF >> third.txt
2nd line
3rd line
EOF

Checking the New Changes

cat third.txt

Example Output:

this is new file
2nd line
3rd line
git status

Example Output:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   third.txt

no changes added to commit (use "git add" and/or "git commit -a")

Now let's stash these changes too:

git stash save "second"

Example Output:

Saved working directory and index state On master: second

Viewing Multiple Stashes

git stash list

Example Output:

stash@{0}: On master: second
stash@{1}: On master: first

Understanding the stack order:

  • stash@{0}: Most recent stash ("second")
  • stash@{1}: Previous stash ("first")
  • The numbers increase as stashes get older

๐ŸŽฏ Advanced Stash Operations

Let's explore more sophisticated stash operations that give you greater control over your saved work.

Using git stash apply vs git stash pop

Applying a Specific Stash (Keep in Stack)

git stash apply stash@{1}

Example Output:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fourth.txt

no changes added to commit (use "git add" and/or "git commit -a")

Key difference with apply:

  • Restores the changes to your working directory
  • Keeps the stash in the stack (doesn't delete it)
  • Allows you to reuse the same stash multiple times

Verifying Changes Were Applied

cat fourth.txt

Example Output:

new changes
second line
third line

Checking Stash List After Apply

git stash list

Example Output:

stash@{0}: On master: second
stash@{1}: On master: first

Important: Both stashes are still there! apply doesn't remove stashes from the stack.

Stashing Current Changes Again

Since we now have uncommitted changes again, let's stash them:

git stash

Example Output:

Saved working directory and index state WIP on master: e3bd41e Merge branch 'dev'

Now we have three stashes:

git stash list

Example Output:

stash@{0}: WIP on master: e3bd41e Merge branch 'dev'
stash@{1}: On master: second
stash@{2}: On master: first

Notice: All previous stashes shifted down by one index number.

๐Ÿ”„ Working with Multiple Stashes Simultaneously

Let's explore a real-world scenario where you might need to combine changes from multiple stashes.

Applying Multiple Stashes

Apply the Oldest Stash First

git stash apply stash@{2}

Example Output:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fourth.txt

no changes added to commit (use "git add" and/or "git commit -a")
cat fourth.txt

Example Output:

new changes
second line
third line

Apply the Second Stash (Different File)

git stash apply stash@{1}

Example Output:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fourth.txt
	modified:   third.txt

no changes added to commit (use "git add" and/or "git commit -a")

Checking Combined Changes

git status

Example Output:

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   fourth.txt
	modified:   third.txt

no changes added to commit (use "git add" and/or "git commit -a")

Verification of both files:

cat fourth.txt
new changes
second line
third line
cat third.txt
this is new file
2nd line
3rd line

Success! We now have changes from multiple stashes active simultaneously in our working directory.

๐Ÿงน Cleaning Up Your Stash Stack

After working with stashes, it's important to clean up to avoid confusion.

Viewing All Stashes Before Cleanup

git stash list

Example Output:

stash@{0}: WIP on master: e3bd41e Merge branch 'dev'
stash@{1}: On master: second
stash@{2}: On master: first

Clearing All Stashes at Once

git stash clear

What this command does:

  • Removes all stashes from the stack
  • Cannot be undone - use with caution
  • Useful for cleaning up when stashes are no longer needed

Verifying Stashes Were Cleared

git stash list

Expected Output: (empty - no output)

Confirmation: All stashes have been removed from the stack.

๐Ÿ“Š Complete Git Stash Commands Reference

Here's a comprehensive table of all essential Git stash commands:

CommandPurposeImpact on Stash Stack
git stashSave current changes with auto-generated messageAdds new stash to stack
git stash save "message"Save current changes with custom messageAdds new stash to stack
git stash listShow all saved stashesNo change to stack
git stash popApply most recent stash and remove from stackRemoves stash from stack
git stash pop stash@{n}Apply specific stash and remove from stackRemoves specified stash
git stash applyApply most recent stash, keep in stackStash remains in stack
git stash apply stash@{n}Apply specific stash, keep in stackStash remains in stack
git stash drop stash@{n}Delete specific stash without applyingRemoves specified stash
git stash clearDelete all stashesEmpties entire stack
git stash showShow changes in most recent stashNo change to stack
git stash show stash@{n}Show changes in specific stashNo change to stack

๐Ÿšจ Common Scenarios and Best Practices

Team Collaboration Scenarios

๐Ÿ’ก

๐Ÿ’ก Team Workflow: Stashing is essential when working in teams to maintain clean, stable branches.

Scenario 1: Urgent Bug Fix Needed

# You're working on a feature
echo "incomplete feature code" >> feature.txt
git status  # Shows uncommitted changes

# Urgent bug fix needed - stash your work
git stash save "incomplete feature work"

# Switch to main branch for bug fix
git checkout main
# Fix the bug, commit, and push

# Return to your feature branch
git checkout feature-branch
git stash pop  # Resume your work

Scenario 2: Need to Pull Latest Changes

# You have local changes
git status  # Shows modified files

# Can't pull with uncommitted changes
git pull origin main  # This might fail

# Solution: stash first
git stash
git pull origin main  # Now this works
git stash pop  # Apply your changes back

Advanced Stashing Techniques

Stashing Only Specific Files

# Stash only tracked files (ignore untracked)
git stash --keep-index

# Stash everything including untracked files
git stash --include-untracked

# Interactive stashing (choose what to stash)
git stash --patch

Stashing with Better Messages

# Descriptive messages for team clarity
git stash save "WIP: user authentication module - password validation pending"
git stash save "TEMP: debugging payment gateway integration"
git stash save "FEATURE: half-implemented search functionality"

โš ๏ธ Common Pitfalls and How to Avoid Them

Pitfall 1: Forgetting About Stashed Work

โš ๏ธ

โš ๏ธ Problem: Developers forget they have stashed work and it gets lost in the stack.

Solution:

  • Use descriptive stash messages
  • Regularly check git stash list
  • Clean up old stashes that are no longer needed

Pitfall 2: Stash Conflicts

When applying a stash, you might encounter conflicts if the base code has changed:

git stash pop
# Auto-merging file.txt
# CONFLICT (content): Merge conflict in file.txt

Resolution steps:

  1. Edit the conflicted files to resolve conflicts
  2. Add the resolved files: git add file.txt
  3. The stash is automatically dropped after conflict resolution

Pitfall 3: Accidentally Clearing All Stashes

๐Ÿšจ

๐Ÿšจ Important: git stash clear cannot be undone. Always verify what stashes you have before clearing:

# Safe approach
git stash list  # Review all stashes first
# If you see important work, apply it first
git stash clear  # Only after confirming

๐ŸŽฏ Key Takeaways

โœ… Remember These Points

  1. Stash Before Context Switching: Always stash uncommitted work before switching branches or pulling updates
  2. Use Descriptive Messages: Named stashes (git stash save "message") are easier to manage than default ones
  3. Understand Apply vs Pop: Use apply when you might need the stash again, pop when you're done with it
  4. Stack Management: Stashes work as a stack (LIFO) - newest stash is stash@{0}
  5. Team Collaboration: Stashing keeps shared branches clean by avoiding incomplete commits

๐Ÿ› ๏ธ Git Stash Quick Reference Cheat Sheet

Essential Commands for Daily Use

TaskCommandUse When
Save current workgit stashQuick save before switching context
Save with descriptiongit stash save "message"When you need to remember what you stashed
View all stashesgit stash listCheck what work you have saved
Restore and removegit stash popResume work and clean up stash
Restore but keepgit stash applyApply changes but keep stash for later
Use specific stashgit stash apply stash@{n}When you have multiple stashes
Delete specific stashgit stash drop stash@{n}Remove stash you no longer need
Clean up all stashesgit stash clearStart fresh (use with caution!)
See stash contentsgit stash showPreview what's in a stash before applying
Include untracked filesgit stash -uStash new files that aren't tracked yet

โœ…

๐ŸŽ‰ Congratulations! You've mastered Git stash fundamentals. You now understand how to temporarily save work without polluting your commit history, manage multiple stashes effectively, and use stashing as part of a clean development workflow.

What's your next step? Practice these stash commands in your daily development work and experience how they improve your Git workflow and team collaboration.

๐Ÿ’ฌ Discussion

I'd love to hear about your Git stashing experience:

  • How has Git stash improved your development workflow?
  • Have you encountered situations where stashing saved your project from conflicts?
  • What stashing strategies work best for your team collaboration?
  • Are there specific Git workflows or advanced stashing techniques you'd like me to cover?

Connect with me:

  • ๐Ÿ™ GitHub - Git examples and workflow configurations
  • ๐Ÿ“ง Contact - Git questions and workflow discussions
Owais

Written by Owais

I'm an AIOps Engineer with a passion for AI, Operating Systems, Cloud, and Securityโ€”sharing insights that matter in today's tech world.

I completed the UK's Eduqual Level 6 Diploma in AIOps from Al Nafi International College, a globally recognized program that's changing careers worldwide. This diploma is:

  • โœ… Available online in 17+ languages
  • โœ… Includes free student visa guidance for Master's programs in Computer Science fields across the UK, USA, Canada, and more
  • โœ… Comes with job placement support and a 90-day success plan once you land a role
  • โœ… Offers a 1-year internship experience letter while you studyโ€”all with no hidden costs

It's not just a diplomaโ€”it's a career accelerator.

๐Ÿ‘‰ Start your journey today with a 7-day free trial

Related Articles

Continue exploring with these handpicked articles that complement what you just read

More Reading

One more article you might find interesting