What does git Stash do ?

Mohan Sharma
2 min readMay 2, 2024

--

Git stash is a lifesaver when you’re working on a Git-managed project and you need to switch branches, but you have uncommitted changes in your working directory that you don’t want to commit yet. Essentially, it’s like a temporary storage area for your changes.

Here’s how it works:

  1. Stash Changes: You use the git stash command to stash your changes. This command will save your modified tracked files and staged changes onto a stack of unfinished changes that you can reapply at any time.
  2. Work on Something Else: Now that your changes are stashed away, you can switch branches, create a new branch, or do whatever you need to do without worrying about your uncommitted changes interfering.
  3. Retrieve Stashed Changes: When you’re ready to work on the changes again, you can apply the stashed changes back to your working directory using git stash apply or git stash pop.
  • git stash apply applies the changes but keeps the stash.
  • git stash pop applies the changes and removes the stash from the stack.

4. Resolve Conflicts (if any): If there are conflicts between your stashed changes and the current state of your working directory, you’ll need to resolve them just like you would with a regular merge or rebase operation.

5. Continue Working: Once the stashed changes are reapplied, you can continue working on them, commit them when you’re ready, or stash them again if needed.

Git stash is handy for situations like:

  • Switching branches to work on something urgent or unrelated.
  • Testing changes in a clean state without committing unfinished work.
  • Dealing with unexpected interruptions or emergencies without losing your progress.

It’s a versatile tool that helps maintain a clean and organized workflow, especially when you need to juggle multiple tasks simultaneously in a Git-managed project.

--

--

Mohan Sharma
Mohan Sharma

Written by Mohan Sharma

Software Developer | Writer | Ai Freak

No responses yet