Skip to main content
  1. Learn
  2. Software Development
  3. Guides
  4. Git tutorial
  5. Git collaboration
  6. Switching branches
  7. Stashing branches
GuidesSoftware DevelopmentBacklog
Git

Project and code management together.

Stashing branches

In Git, "stashing" refers to temporarily shelving changes that you've made to your working directory so that you can work on something else, typically without committing those changes to your branch. However, stashing itself doesn't directly relate to branches; rather, it's a mechanism to save your changes temporarily.

If you have uncommitted changes (or new files added) in your working tree and you want to switch to a new branch, those uncommitted changes will also carry to the new branch. Changes that you commit will be committed to the new branch.

However, if Git finds a conflict between the uncommitted changes in your current branch and the files in the new branch you’re switching to, you will not be allowed to switch. You must commit or stash those changes before switching branches.

You can think of stash as a drawer for temporarily storing uncommitted changes. Stashing allows you to put aside any “messy” changes in your working tree and continue working on a new branch with a clean slate.

You can always take out uncommitted changes stored in the stash and apply them to the original branch and other branches later.

Understanding stashing

  1. Stashing changes:
    • When you stash changes, Git saves the current state of modified files and staged changes, but reverts them back to the last commit state.
    • This action helps you switch branches or perform other operations without carrying over unfinished work.
  2. Stash stack:
    • Git maintains a stack of stashes, allowing you to store multiple stashes.
    • You can apply or remove stashes from the stack as needed.
  3. Applying stashes:
    • After stashing changes, you can switch branches and then apply the stash to reapply your changes.
    • Optionally, you can apply and drop the stash in one command, removing it from the stash stack after applying.

When to use stashing

  • Switching branches: Stash changes when you need to switch branches to work on a different feature or fix, but you're not ready to commit your current changes.
  • Temporary interruptions: Use stashing for temporary interruptions such as hotfixes or urgent tasks that require you to temporarily set aside your current work.
  • Partial commits: Stashing allows you to save changes in progress without committing them fully, useful for when changes aren't yet ready for a commit but you need to switch tasks.

Subscribe to our newsletter

Learn with Nulab to bring your best ideas to life