← Back to list

Day 8: Mastering Git Stash & Local Cleanliness

As developers work on software projects, interruptions are inevitable. You might be halfway through implementing a feature when an urgent…

Tabspace · 2026-06-13 17:46 · 0 claps · 4.6 min read
#github #git
Open on Medium ↗
Wiki topics: 🔓 · Open Source

Day 8: Mastering Git Stash & Local Cleanliness

As developers work on software projects, interruptions are inevitable. You might be halfway through implementing a feature when an urgent production bug appears. You may need to switch branches to review a teammate’s code or pull the latest changes from the remote repository. In these situations, your current work may not be ready for a commit.

Git provides a powerful mechanism for handling these scenarios without forcing you to create incomplete commits: Git Stash.

Git Stash allows developers to temporarily save uncommitted work, clean their working directory, perform other tasks, and later restore the saved changes exactly where they left off.

In addition to stashing, maintaining a clean repository often requires removing temporary files, generated artifacts, and untracked files. Git provides the git clean command specifically for this purpose.

This lesson explores both Git Stash and Git Clean in depth.

Why Git Stash Exists

Imagine the following scenario:

You are working on a new feature.

feature-login

You have modified several files:

login.js
auth.js
styles.css

None of these changes are complete yet.

Suddenly, your manager asks you to fix a critical bug in production immediately.

Normally Git prevents branch switching if changes might be overwritten:

git switch main

You may receive:

error: Your local changes would be overwritten

You have several options:

  1. Commit incomplete work
  2. Copy files somewhere manually
  3. Use Git Stash

Git Stash is the professional solution.

What is Git Stash?

Git Stash temporarily stores:

  • Modified tracked files
  • Staged changes
  • Optionally untracked files

and restores your repository to a clean state.

Think of stash as a temporary shelf where Git stores unfinished work.

Before Stash
Working Directory
    ↓
Unfinished Changes
After Stash
Working Directory
    ↓
Clean State
Stash Stack
    ↓
Saved Changes

Understanding the Stash Stack

Git stores stashes in a stack structure.

A stack follows:

Last In
First Out
(LIFO)

Example:

stash@{0} ← Most Recent
stash@{1}
stash@{2}

When applying or popping a stash:

git stash pop

Git uses:

stash@{0}

by default.

Creating Your First Stash

Suppose:

git status

Output:

modified: app.js
modified: config.js

Save current work:

git stash

Output:

Saved working directory and index state

Verify:

git status

Output:

nothing to commit, working tree clean

Your changes are now stored safely inside Git’s stash stack.

Viewing Existing Stashes

To see all saved stashes:

git stash list

Example:

stash@{0}: WIP on feature-login
stash@{1}: WIP on feature-auth
stash@{2}: WIP on feature-payment

Each stash receives a unique identifier.

Creating Named Stashes

Instead of generic messages, provide meaningful descriptions.

git stash push -m "Login page validation work"

Check:

git stash list

Output:

stash@{0}: On feature-login: Login page validation work

Named stashes are easier to identify later.

What Gets Stored in a Stash?

By default Git stashes:

Tracked Modified Files

✓ Included

Staged Files

✓ Included

Untracked Files

✗ Not Included

Ignored Files

✗ Not Included

This behavior is important to remember.

Stashing Untracked Files

Consider:

new-feature.js

is untracked.

Running:

git stash

will not save it.

To include untracked files:

git stash -u

or

git stash --include-untracked

Now Git saves:

Tracked Files
+
Untracked Files

Stashing Everything Including Ignored Files

To include ignored files:

git stash -a

or

git stash --all

This stores:

Tracked Files
Untracked Files
Ignored Files

Useful when performing major environment switches.

Applying a Stash

To restore the most recent stash:

git stash apply

Git restores the changes.

However:

Stash remains in stack

Example:

stash@{0}

still exists.

Applying a Specific Stash

View available stashes:

git stash list

Output:

stash@{0}
stash@{1}
stash@{2}

Apply a specific one:

git stash apply stash@{1}

Git restores only that stash.

Pop vs Apply

This is one of the most important distinctions.

Apply

git stash apply

Result:

Restore Changes
Keep Stash

Pop

git stash pop

Result:

Restore Changes
Delete Stash

Visualization:

Apply:
Stack → remains

Pop:
Stack → removes top item

Use Apply when unsure.

Use Pop when confident.

Inspecting Stash Contents

Before restoring a stash, inspect its contents.

Show summary:

git stash show

Detailed changes:

git stash show -p

Output resembles:

+ Added login validation
- Removed old authentication

This helps determine which stash contains the desired work.

Creating Multiple Stashes

Developers often maintain several stashes.

Example:

git stash push -m "Login Feature"
git stash push -m "Payment Integration"
git stash push -m "UI Improvements"

List:

stash@{0} UI Improvements
stash@{1} Payment Integration
stash@{2} Login Feature

Remember:

Most recent = stash@{0}

Deleting a Stash

Remove a specific stash:

git stash drop stash@{1}

Output:

Dropped stash@{1}

Useful for cleaning outdated work.

Clearing All Stashes

Delete everything:

git stash clear

Result:

All stash entries removed

Use carefully because recovery becomes difficult.

Stash Conflicts

Applying a stash may produce merge conflicts.

Example:

  1. Stash changes
  2. Another developer modifies same lines
  3. Pull latest changes
  4. Apply stash

Git reports:

CONFLICT (content)

Resolve exactly like merge conflicts:

<<<<<<< HEAD
Current code
=======
Stashed code
>>>>>>> Stash

Edit manually.

Then:

git add .
git commit

Conflict resolved.

Creating a Branch from a Stash

Sometimes a stash evolves into a separate feature.

Create a branch directly from a stash:

git stash branch feature-ui

Git performs:

  1. Create branch
  2. Apply stash
  3. Remove stash

Automatically.

Very useful for exploratory work.

Understanding Git Clean

Git Stash manages tracked modifications.

Git Clean handles unwanted files.

Consider:

temp.log
output.txt
debug.tmp

These files are not tracked.

Git status:

Untracked files:
temp.log
output.txt
debug.tmp

Remove them:

git clean -f

Why Git Clean is Dangerous

Unlike stash:

Git Clean Deletes Files

No automatic recovery exists.

Always verify first.

Dry Run Mode

Preview files before deletion.

git clean -n

Output:

Would remove temp.log
Would remove output.txt

Nothing is deleted.

This should be your first step every time.

Removing Directories

Delete untracked folders:

git clean -fd

Where:

-f = force
-d = directories

Example:

build/
dist/
temp/

are removed.

Removing Ignored Files

To remove ignored files too:

git clean -fx

Useful for:

node_modules
build artifacts
generated files

Often used before a clean build.

Common Real-World Workflow

Scenario:

You are coding Feature A.

git status

Output:

modified: app.js
modified: login.js

Emergency bug arrives.

Step 1:

git stash push -m "Feature A progress"

Step 2:

git switch main

Step 3:

git pull

Step 4:

Fix bug and push.

Step 5:

git switch feature-a

Step 6:

git stash pop

Continue exactly where you left off.

This workflow is used daily by professional software engineers.

Best Practices

Use Meaningful Stash Messages

Good:

git stash push -m "Payment API integration"

Bad:

git stash

Prefer Apply Before Pop

Safer:

git stash apply

Verify first.

Delete later if desired.

Always Preview Clean Operations

Use:

git clean -n

before:

git clean -f

Do Not Keep Stashes Forever

Old stashes become confusing.

Regularly review:

git stash list

and remove unused entries.

Summary

Git Stash is one of the most valuable productivity tools in Git. It allows developers to pause work, switch contexts, address urgent tasks, and later resume exactly where they left off without polluting project history with incomplete commits.

Key commands learned:

git stash
git stash push -m
git stash list
git stash apply
git stash pop
git stash show
git stash drop
git stash clear
git stash branch

For repository cleanliness:

git clean -n
git clean -f
git clean -fd
git clean -fx

Mastering Git Stash and Git Clean enables developers to handle interruptions efficiently, maintain organized repositories, and work confidently in fast-paced collaborative environments.


메타데이터
post_id
cdef01a27cb2
slug
day-8-mastering-git-stash-local-cleanliness-cdef01a27cb2
url
https://medium.com/@tabspace_/day-8-mastering-git-stash-local-cleanliness-cdef01a27cb2
canonical_url
https://medium.com/@tabspace_/day-8-mastering-git-stash-local-cleanliness-cdef01a27cb2
author_url
https://medium.com/@tabspace_
status
ok
fetched_at
2026-06-15 20:49:13