Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Git

Git: Use git stash to Save Work Without Committing

- 22.03.26 - ErcanOPAK

💾 Save Work, Switch Branch, Come Back

Halfway through feature. Emergency bug fix needed. Don’t want to commit messy code. git stash saves work temporarily.

The Problem

# Working on feature branch
git status
# modified:   feature.js
# modified:   styles.css

# Boss: "Fix production bug NOW!"
git checkout main
# Error: Your local changes would be overwritten
# Please commit or stash them

# Options:
# 1. Commit messy half-done work ❌
# 2. Create throwaway commit ❌
# 3. Copy files somewhere else ❌
# 4. Use git stash ✅

Basic Stash Usage

# Save current changes
git stash
# Saved working directory and index state

# Working directory now clean
git status
# nothing to commit, working tree clean

# Switch to other branch
git checkout main

# Fix bug, commit, push

# Go back to feature branch
git checkout feature-branch

# Restore stashed changes
git stash pop
# Changes restored, stash removed

# Continue working!

🎯 Essential Stash Commands

# Save with descriptive message
git stash save "WIP: user authentication form"

# List all stashes
git stash list
# stash@{0}: WIP: user authentication form
# stash@{1}: WIP: homepage redesign
# stash@{2}: On main: quick fix

# Apply stash without removing it
git stash apply stash@{0}

# Apply and remove (like pop)
git stash pop stash@{0}

# Show what's in a stash
git stash show stash@{0}

# Show full diff
git stash show -p stash@{0}

# Delete specific stash
git stash drop stash@{0}

# Delete all stashes
git stash clear

Advanced Stash Tricks

# Stash only staged changes
git stash --staged

# Stash including untracked files
git stash -u

# Stash everything (including ignored files)
git stash -a

# Stash specific files only
git stash push -m "message" path/to/file.js

# Create branch from stash
git stash branch new-feature-branch stash@{0}
# Creates branch, applies stash, drops stash
# Perfect when stash has conflicts with current branch

# Stash interactively (choose what to stash)
git stash -p
# Goes through each change, asks: stash this hunk?

🔄 Real Workflow

# Working on feature
$ vim feature.js

# Emergency: fix production bug
$ git stash save "WIP: user login form validation"
$ git checkout main
$ git pull

# Create hotfix branch
$ git checkout -b hotfix-payment-bug
$ vim payment.js
$ git add payment.js
$ git commit -m "Fix: payment timeout issue"
$ git push origin hotfix-payment-bug

# Back to feature
$ git checkout feature-branch
$ git stash pop

# Continue where you left off!

Stash vs Commit

✅ When to Stash

  • Temporary context switch
  • Work in progress, not ready
  • Experimenting, might discard
  • Quick bug fix interruption

✅ When to Commit

  • Logical unit of work done
  • Want to keep in history
  • Ready for code review
  • Stable checkpoint

💡 Pro Tips

  • Use descriptive messages: Future you will thank you
  • Don’t hoard stashes: Clean up old ones regularly
  • Stash before pull: Avoid merge conflicts with uncommitted work
  • Create alias: git config --global alias.save 'stash save'

⚠️ Common Mistakes

  • Forgetting about stashes: Check git stash list regularly
  • Stashing too much: Multiple stashes get confusing
  • Not using messages: stash@{3} tells you nothing
  • Conflicts on pop: Resolve like merge conflicts, then drop stash manually

“Before git stash, interruptions meant messy commits or lost work. Now I stash, fix bugs, come back instantly. Saved me dozens of times during on-call weeks.”

— Senior Developer

Related posts:

Git: Managing Multiple GitHub Accounts with SSH Config

Git Pull Breaks Local Changes Silently

Git Pull Suddenly Breaks Local Changes

Post Views: 6

Post navigation

Ajax: Use AbortController to Cancel Pending Requests
Git: Use git cherry-pick to Copy Specific Commits Between Branches

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

April 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
27282930  
« Mar    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (751)
  • Add Constraint to SQL Table to ensure email contains @ (578)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (564)
  • Average of all values in a column that are not zero in SQL (531)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (489)
  • Find numbers with more than two decimal places in SQL (447)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries
  • SQL: Use Window Functions for Advanced Analytical Queries
  • .NET Core: Use Background Services for Long-Running Tasks
  • .NET Core: Use Minimal APIs for Lightweight HTTP Services
  • Git: Use Cherry-Pick to Apply Specific Commits Across Branches
  • Git: Use Interactive Rebase to Clean Up Commit History Before Merge

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (950)
  • How to add default value for Entity Framework migrations for DateTime and Bool (858)
  • Get the First and Last Word from a String or Sentence in SQL (836)
  • How to select distinct rows in a datatable in C# (805)
  • How to make theater mode the default for Youtube (751)

Recent Posts

  • C#: Use Init-Only Setters for Immutable Objects After Construction
  • C#: Use Expression-Bodied Members for Concise Single-Line Methods
  • C#: Enable Nullable Reference Types to Eliminate Null Reference Exceptions
  • C#: Use Record Types for Immutable Data Objects
  • SQL: Use CTEs for Readable Complex Queries

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com