DEV Community

Kitco's Today I Learned
Kitco's Today I Learned

Posted on

1

[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.

AWS Q Developer image

Your AI Code Assistant

Ask anything about your entire project, code and get answers and even architecture diagrams. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Start free in your IDE

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More