Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Git stashing: save and restore your code
     

Git stashing: save and restore your code

Have you ever been in the middle of coding when suddenly you needed to switch branches, but your work wasn't ready for a commit?
I think it happens to me like 200 times a week. 😀
This is whereGit stash comes in handy. Stashing allows you to temporarily save your work without committing it, letting you switch contexts seamlessly.


What is Git Stashing?

Git stashing temporarily saves your uncommitted changes without adding them to a commit. This is useful when:

  • You need to switch branches but don’t want to commit unfinished work.
  • You want to test a different feature without losing progress.
  • You’re working in a dirty working directory and need a clean state.

A stash includes modified and staged files but doesn’t store untracked or ignored files unless explicitly specified.


Basic Git Stash Commands

git stash

This command stashes your current changes, leaving your working directory clean.

Example

# Work on a feature but need to switch branches$git stashSaved working directory and index state WIP on main: abc1234 Add new feature
Enter fullscreen modeExit fullscreen mode

Now your working directory is clean, and you can safely switch branches.

git stash list

Displays a list of all saved stashes.

Example

$git stash liststash@{0}: WIP on main: abc1234 Add new featurestash@{1}: WIP on dev: def5678 Refactor component
Enter fullscreen modeExit fullscreen mode

This allows you to see all your stored work-in-progress changes.

git stash show

Shows a summary of what’s inside a stash.

Example

$git stash show stash@{0} src/main.js | 5 +++--
Enter fullscreen modeExit fullscreen mode

Use-p to see detailed changes:

$git stash show-p stash@{0}
Enter fullscreen modeExit fullscreen mode

Working With Multiple Stashes

git stash pop

Applies the most recent stash and removes it from the stash list.

Example

$git stash pop
Enter fullscreen modeExit fullscreen mode

If there are merge conflicts, Git will notify you.

I don't use this command very often to be honest because I am afraid to lose some commits, even if I know that it's safe.

git stash apply

Applies a stash butkeeps it in the stash list.

Example

$git stash apply stash@{1}
Enter fullscreen modeExit fullscreen mode

Useful when you want to reuse a stash multiple times.

git stash drop

Deletes a specific stash after applying changes.

Example

$git stash drop stash@{1}
Enter fullscreen modeExit fullscreen mode

Removes stash@{1} from the list.


Advanced Git Stash Commands

git stash push -m "message"

Stores a stash with a custom message for better organization.

Example

$git stash push-m"Fixing bug in login form"
Enter fullscreen modeExit fullscreen mode

This makes it easier to find later ingit stash list.

git stash branch

Creates a new branch from a stash.

Example

$git stash branch feature-branch stash@{0}
Enter fullscreen modeExit fullscreen mode

Useful when you want to resume work from a stash as a separate branch.

git stash clear

Removes all stored stashes.

Example

$git stash clear
Enter fullscreen modeExit fullscreen mode

Use this command carefully because it's irreversible!


Thanks for reading this post, I hope you found it interesting!

Feel free to follow me to get notified when new articles are out 🙂

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
tamilselvam_48 profile image
Tamil Selvam M
  • Joined

This feature is very useful as it helps avoid unwanted commits.

CollapseExpand
 
kasuken profile image
Emanuele Bartolesi
🏆 Microsoft MVP & GitHub Star ⭐👨‍🎓 LinkedIn Technical Instructor 🎓
  • Email
  • Location
    Zurich, Switzerland
  • Pronouns
    he/him
  • Work
    Cloud Architect
  • Joined

Yes, I love it!

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Free, open and honest software education.

Read our welcome letter which is an open invitation for you to join.

More fromThis is Learning

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp