DEV Community

nia
nia

Posted on

[Git] How to Temporarily Save Changes with `git stash` While Switching Branches

1st March 2025

While working on a team project, there might be times when you're in the middle of writing code on your feat branch but need to switch to the dev branch to check something. In this case, you can use the git stash command to temporarily save your work without committing unfinished changes.

Steps

  1. Save your current changes:
   git stash push -m "Description of your work in progress"
Enter fullscreen mode Exit fullscreen mode
  1. Switch to the dev branch:
   git switch dev
Enter fullscreen mode Exit fullscreen mode
  1. After checking what you need on dev, switch back to your feat branch:
   git switch feat
Enter fullscreen mode Exit fullscreen mode
  1. Restore your stashed changes:
   git stash pop
Enter fullscreen mode Exit fullscreen mode

The git stash pop command applies the latest stash and automatically removes it from the stash list. If you want to apply the stash without deleting it, use:

git stash apply
Enter fullscreen mode Exit fullscreen mode

What If You Have Multiple Stashes?

If you've stashed your work multiple times, you can see the list of saved stashes:

git stash list
Enter fullscreen mode Exit fullscreen mode

Example output:

stash@{0}: On branch_name: Work in progress 2
stash@{1}: On branch_name: Work in progress 1
Enter fullscreen mode Exit fullscreen mode

To apply a specific stash, use:

git stash pop stash@{0}
Enter fullscreen mode Exit fullscreen mode

I haven't used multiple stashes much yet, but if I do, I'll come back and explain the process in more detail.

Top comments (0)