What I've Learned About Git from Senior Colleagues (Part 1 - git stash)
Tags: webdev, git, productivity, tip
Published: 2024-03-12 06:40:38


## Welcome to the Git Essentials Series!

If you're an intern or fresher eager to bridge the gap between Git theory and real-world application, you're in the right place. In this series, I'll be sharing firsthand stories and insights gleaned from my journey, learning from seasoned colleagues since the beginning of my career. From navigating complex merge conflicts to mastering the art of efficient collaboration, each post will offer practical wisdom and valuable lessons learned along the way. Get ready to embark on this Git adventure with me, as we explore the depths of version control and unlock the secrets to seamless project management. Stay tuned for the first installment, where we'll get acquainted with `git stash`.

![Senior helps Junior](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yx8o8yhkf5firorcu3s0.jpeg)

# 1. My Story
When I first started learning Git, I found it easy to apply basic commands like git add, git commit, git pull, git push, and so on. Perhaps you did too. I thought that knowing just these commands would be enough for working professionally, but reality proved to be more challenging. Once, my mentor wanted me to fix some comments on my pull request for feature A that I had submitted earlier. However, I was working on the branch for feature B. I couldn't commit these messy code changes. That's when he enlightened me about git stash.
Let's get started! Remember that the examples in this blog use `git version 2.39.3`
# 2. What is git stash? And how to use?
When you run the command `git stash --help`, the git documentation says:
>Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

Still a bit hard to understand, huh? Uh huh? So, imagine your code changes are looking like this.

![Before git stash save](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l40j53vvtnp8ei6lfc1v.png)

And after running git stash in the terminal, it looks like this.

![After git stash save](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gimpws3qfy37knii9y3e.png)

WTF??? Did your code just disappear??? No! It's been saved in the stash, and your code has reverted to a clean state as if you haven't made any changes. So now, where can you find that piece of code you just had? Try running `git stash list` in the terminal.

```bash
# output
stash@{0}: WIP on main: 8a278dc :+1: Refactor, add shortcut, README.md
```
In the above output, `stash@{0}`, where `0` is the index of the stash. The closer the stash's time to the current time, the smaller the index. `WIP on main: 8a278dc :+1: Refactor, add shortcut, README.md` is the message of the stash created by default, following the branch name and the message of the most recent commit.

You can customize the stash message using the following syntax:
```bash
git stash save {custom message}
# Example
git stash save 'Stash with message'
# Then the stash list will look like...
stash@{0}: On main: Stash with message
stash@{1}: WIP on main: 8a278dc :+1: Refactor, add shortcut, README.md
```

Yeah, now I can stash my messy code changes on the feature B branch, then switch back to fixing feature A, and later return to feature B. Now, if I want to retrieve the stashed code changes, what should I do?
```bash
git stash apply
```

Now your code has been restored
![After git stash apply](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5rplimnosmzco6qiiz56.png)

You can pass the parameter as the index of the stash to specify the stashed code portion you want to restore.

```bash
git stash apply 1
# or
git stash apply 0
```
There's a note when you save a stash that untracked files won't be stashed. To stash them, you have two options: one is to add them to staged changes before stashing, and the other is to run `git stash save --include-untracked`.

By now, you've acquired a comprehensive understanding of the basic concepts and functionalities of git stash, equipping you to tackle a wide array of practical scenarios with confidence. As we transition to Chapter 3: "Some Other Useful Commands" rest assured that you've solidified your foundation in utilizing git stash effectively.
# 3. Some Other Useful Commands
1. `git stash pop`: This command is similar to git stash apply but also removes the most recent stash from the stash list after applying it.
2. `git stash drop`: Use this command to remove a specific stash from the stash list.
3. `git stash clear`: This command removes all stashes from the stash list, providing a clean slate.

If you want to learn more, use the git stash --help command to see additional information. Always remember that help is the sacred command that helps you learn everything.

In conclusion, mastering git stash is crucial for efficient version control management. With the basics covered, stay tuned for upcoming series where we'll delve into topics like git rebase, git merge, writing effective commit messages, and resolving conflicts seamlessly. These skills will further enhance your Git proficiency and streamline your development workflow. Keep exploring and happy coding!

Reference: [Git Documentation](https://git-scm.com/doc)