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

·

1 min read

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"
    
  2. Switch to the dev branch:

     git switch dev
    
  3. After checking what you need on dev, switch back to your feat branch:

     git switch feat
    
  4. Restore your stashed changes:

     git stash pop
    

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

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

Example output:

stash@{0}: On branch_name: Work in progress 2
stash@{1}: On branch_name: Work in progress 1

To apply a specific stash, use:

git stash pop stash@{0}

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