Git Stash - Instant Code Pause Button ⏸️ (Illustrated)
Tags: git, beginners
Published: 2024-02-24 12:47:31


## 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

```bash
git stash
# store with message
git stash push -m "⚠️ changes in file3.js"
```
Stash the changes in your working directory with an optional descriptive message.

<img src="https://i.imgur.com/l4AX6DI.gif" alt="Git stash" width="800" height="500"/>

- **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.
```bash
git stash pop
```
<img src="https://i.imgur.com/1nn7T5p.gif" alt="Git stash" width="800" height="500"/>

```bash
git stash pop stash@{3}
```
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

<img src="https://i.imgur.com/OVCvOWY.gif" alt="Git stash" width="800" height="500"/>


### 4. Listing Stashed Changes
Display a list of stashed changes along with their corresponding stash IDs and descriptions.
```bash
git stash list
```

<img src="https://i.imgur.com/XvVCfGg.gif" alt="Git stash" width="800" height="500"/>


### 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
```bash
git stash apply
```
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.
```bash
#git stash show -p stash@{n}

git stash show stash@{0}
```

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

```bash
git stash push -m "description" --patch
```
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.
```bash
git stash branch branch_name stash@{n}
```

### 8. Removing changes from stash
```bash
#git stash drop stash@{n}
git stash drop stash@{0}
```
### 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 👩‍💻***