DEV Community

Cover image for Git Stash - Instant Code Pause Button ⏸️ (Illustrated)
Priya jain
Priya jain

Posted on • Originally published at priya.today

Git Stash - Instant Code Pause Button ⏸️ (Illustrated)

Introduction

Imagine you're in the middle of working on a new feature for your project when suddenly, you need to fix a critical bug. This is where git stash comes in handy.

git stash lets you temporarily set aside the changes you've made so you can deal with urgent matters without losing your progress or making messy commits. It's like hitting a pause button on your code changes, allowing you to switch tasks smoothly.

In this article, we'll walk you through how to use Git Stash in simple terms. Whether you're new to Git or just looking to streamline your workflow, mastering Git Stash will help you work smarter and more efficiently.

Understanding Git Stash

git stash comes with few key concepts:

1.Stash Stack:

Git Stash operates with a stack-based model, where stashed changes are stored in a stack-like structure. This means that you can stash changes multiple times, creating a stack of stashed states that can be popped or applied in a last-in, first-out (LIFO) manner.

2.Stashed Changes:

When you stash changes using Git, Git takes the modified tracked files in your working directory and saves them to a special area called the stash. This action reverts the tracked files to their clean state, allowing you to perform other operations or switch branches without carrying the unfinished changes.

Basic Commands

1. Store Uncommitted Changes

git stash
# store with message
git stash push -m "⚠️ changes in file3.js"
Enter fullscreen mode Exit fullscreen mode

Stash the changes in your working directory with an optional descriptive message.

Git stash

  • WIP On priya – priya is just a branch name like any other branch and WIP stands for Work In Progress. "WIP on priya" means that stash@{0} was created on the branch "priya".
  • fb7804 add head line – fb7804 is a Commit hash and add head line is a commit message. At that time of stash creation, fb7804 add head line is the latest commit. ### 2. Retrieving Stashed Changes Apply the most recent stashed changes to your working directory and remove them from the stash stack.
git stash pop
Enter fullscreen mode Exit fullscreen mode

Git stash

git stash pop stash@{3}
Enter fullscreen mode Exit fullscreen mode

If we want to re-apply the changes from a different stash, we should pass its identifier as the last argument to the respective commands.

3. Stash multiple changes

Git stash

4. Listing Stashed Changes

Display a list of stashed changes along with their corresponding stash IDs and descriptions.

git stash list
Enter fullscreen mode Exit fullscreen mode

Git stash

6. Don't remove changes from stash

If we don't want our changes to be removed from the stash after applying it on the branch, we can use the following command

git stash apply
Enter fullscreen mode Exit fullscreen mode

This command can be used to apply the same changes on multiple branches.

5. Viewing Stashed Changes

Display a list of stashed changes along with their corresponding stash IDs and descriptions.

#git stash show -p stash@{n}

git stash show stash@{0}
Enter fullscreen mode Exit fullscreen mode

6. Stash specific file

We can make use of the --patch flag while running the git stash command that instructs Git not to stash everything that is modified instead it will iterate through all the changes and ask whether we want to stash it or not.

git stash push -m "description" --patch
Enter fullscreen mode Exit fullscreen mode

This command provides granularity in choosing which changes to stash, enabling developers to selectively stash specific modifications while keeping others intact.

7. Create a branch from the stash

Sometimes, the changes stashed might be substantial enough to warrant further development on a dedicated branch. Git facilitates this process by allowing the creation of a new branch directly from a stash.

git stash branch branch_name stash@{n}
Enter fullscreen mode Exit fullscreen mode

8. Removing changes from stash

#git stash drop stash@{n}
git stash drop stash@{0}
Enter fullscreen mode Exit fullscreen mode

Conclusion

By leveraging Git Stash, developers can seamlessly switch tasks, tackle urgent issues, and experiment with code variations without cluttering their commit history or sacrificing efficiency.
Embrace Git Stash as your go-to code pause button, empowering you to navigate the complexities of software development with ease and precision.

Happy Coding 👩‍💻

Top comments (0)