DEV Community

Cover image for How to use Git Stash
João Ricardo Mattedi Cetto for LEDS

Posted on • Edited on

How to use Git Stash

While you work on a project in Git, you may encounter situations where you need to temporarily set aside your current work to switch branches or handle other tasks. In these cases, committing the changes of an uncompleted work isn’t always ideal. This is where the command “git stash” becomes useful. With this feature, you can save your uncommitted changes and clean up your workspace, it allows you to return to them later without losing progress. In this post, we’ll explore how to effectively use this feature and some key commands to manage your stashes.

STASH

In Git, the git stash command allows you to temporarily save changes made in your working directory without committing them. This is particularly useful when you’re working on a project but need to switch branches or handle another task without losing your uncommitted changes, which may not yet be ready to commit.

How does git stash work?

When you use git stash, it stores changes from both the working directory and the index (staging area), cleaning up your workspace and allowing you to switch focus to another part of the project. These changes are saved in a "stash stack," which allows you to retrieve them later when you’re ready to continue.

Basic Commands

  • git stash: Saves uncommitted changes in a temporary stash and cleans up the working directory.
  • git stash apply: Reapplies the stashed changes without removing them - from the stash stack.
  • git stash pop: Restores the stashed changes and removes them from the stash stack.
  • git stash list: Lists all saved stashes.
  • git stash drop [stash_id]: Removes a specific stash from the list.
  • git stash clear: Removes all stashes from the stack.

Using stash

Scenario:
You're working on a new feature in your project, but a bug appears and you need to switch branches to fix it. You don't want to commit your incomplete work yet, but also don't want to lose your changes.

Steps to stash:

1. Current Situation: You've made several changes to files while working on the feature-branch. Use the command git add . to add your work in progress (WIP) to the stage and, with git status you'll see that your WIP is in the stage.

$ git status
On branch feature-branch
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        modified:   teste/testando.txt
Enter fullscreen mode Exit fullscreen mode

2. Save the Changes to temporarily save your work without committing it, use git stash.

$ git stash
Saved working directory and index state WIP on feature-branch: 7e9ca1a first commit
Enter fullscreen mode Exit fullscreen mode

3. Fix the bug on main, commit the changes, and push them.

4. Return to Your Feature Branch once the bug fix is done with git checkout feature-branch

5. Reapply Your Stashed Changes using git stash pop.

$ git stash pop
On branch feature-branch
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   teste/testando.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (8e60d99fc7d822c5c0c67b7c3f709d2085026eb2)
Enter fullscreen mode Exit fullscreen mode

Obs: This command applies your saved changes back to the working directory and removes them from the stash list.

Did you enjoy the post? If you’d like to explore more Git and GitHub features, check out: Forks in GitHub

Top comments (0)