Ifty's BlogThoughts & Ideas
cover

How to Save Current Progress and Switch Projects with Git Stash

9/9/2024, 12:00:00 AM

GitVersion ControlWorkflow

Every developer faces this scenario: you're deep into coding a feature when suddenly you need to switch to another branch to fix a critical bug or work on a different project. But your current work isn't ready for a commit yet. This is where Git stash becomes your best friend! Git stash allows you to temporarily save your uncommitted changes and switch contexts seamlessly.

What is Git Stash?

Git stash is a powerful feature that temporarily saves your uncommitted changes (both staged and unstaged) and reverts your working directory to match the HEAD commit. Think of it as a temporary clipboard for your work-in-progress code. You can then switch branches, pull updates, or work on something else, and later restore your stashed changes.

When to Use Git Stash

  • Emergency bug fixes: When you need to quickly switch to fix a critical issue
  • Branch switching: When you want to switch branches but aren't ready to commit
  • Pulling updates: When you need to pull changes but have uncommitted work
  • Experimenting: When you want to try something without losing your current progress

Basic Git Stash Commands

1. Save Your Current Work to Stash

The most basic command to stash your changes:

git stash

Or with a descriptive message (recommended):

git stash push -m "Working on user authentication feature"

2. List All Stashes

To see all your saved stashes:

git stash list

Output example:

stash@{0}: On main: Working on user authentication feature
stash@{1}: WIP on feature-branch: 1234567 Add login form

3. Apply Stashed Changes

To restore your most recent stash:

git stash apply

To apply a specific stash:

git stash apply stash@{1}

4. Pop Stashed Changes

To apply and remove the most recent stash from the stash list:

git stash pop

Advanced Stash Operations

Stash Specific Files

To stash only specific files:

git stash push -m "Stashing only config files" config.js package.json

Include Untracked Files

By default, Git stash doesn't include untracked files. To include them:

git stash -u
# or
git stash --include-untracked

Stash Everything (Including Ignored Files)

To stash everything, including files listed in .gitignore:

git stash -a
# or
git stash --all

Practical Workflow Example

Here's a real-world scenario showing how to use Git stash effectively:

# You're working on a feature
git add .
# Oops! Emergency bug report came in

# 1. Stash your current work
git stash push -m "Half-done user profile feature"

# 2. Switch to main branch
git checkout main

# 3. Create and switch to bug fix branch
git checkout -b hotfix/critical-bug

# 4. Fix the bug and commit
git add .
git commit -m "Fix critical login bug"

# 5. Push and merge the fix
git push origin hotfix/critical-bug

# 6. Switch back to your feature branch
git checkout feature/user-profile

# 7. Restore your stashed work
git stash pop

# 8. Continue working on your feature

Managing Multiple Stashes

View Stash Details

To see what changes are in a specific stash:

git stash show -p stash@{0}

Drop a Stash

To delete a specific stash without applying it:

git stash drop stash@{1}

Clear All Stashes

To remove all stashes:

git stash clear

Best Practices

  1. Always add descriptive messages to your stashes for easy identification
  2. Don't let stashes pile up - apply or drop them regularly
  3. Use git stash pop instead of apply when you're sure you want to remove the stash
  4. Test your code after applying a stash to ensure everything works correctly
  5. Consider committing instead if your changes are in a good state

Difference Between Pop and Apply

  • git stash pop: Applies the stash and removes it from the stash list
  • git stash apply: Applies the stash but keeps it in the stash list

Use pop when you're confident the stash applied successfully. Use apply when you want to keep the stash as a backup.

Conclusion

Git stash is an essential tool for maintaining a clean and efficient development workflow. It allows you to context-switch seamlessly without losing your progress or creating unnecessary commits. Master these commands, and you'll find yourself much more productive when juggling multiple tasks and projects.

Remember: Git stash is temporary storage, not a replacement for proper version control. Once you're ready, always commit your changes properly to maintain a clean project history.