What's Git Stash? Git Stash Explained Unwrapping Git Stash

By Thomas

Ever found yourself in the middle of a complex commit, only to realize that you need to switch to a different branch for an urgent fix or new feature? The git stash command is your savior in such situations, allowing you to temporarily store your changes, switch to another branch, and then come back to your work later. But what exactly is 'git stash', and how does it work? Let's delve into the details.

a sign that says, 15 stash commands you should know
a sign that says, 15 stash commands you should know

In essence, git stash is a built-in Git command that helps to temporarily save local changes that haven't been committed yet. It's like creating a snapshot of your current changes, storing them away for later use, and then resetting your working directory to a clean state. Now, let's explore this command's capabilities under two primary aspects.

25 GIT COMMANDS WITH EXAMPLES
25 GIT COMMANDS WITH EXAMPLES

Stashing Changes: The Basics

Before we dive into advanced uses, let's understand the basic syntax and functionality of the git stash command.

What is 𝐆𝐒𝐭 𝐬𝐭𝐚𝐬𝐑
What is 𝐆𝐒𝐭 𝐬𝐭𝐚𝐬𝐑

The most common usage of git stash is to store all modified files in your working directory:

git stash

25 GIT COMMANDS EVERY DEVELOPER SHOULD MASTER
25 GIT COMMANDS EVERY DEVELOPER SHOULD MASTER

Stashing with a Message

You can add a message to your stash to help organize and apply the stashed changes later:

git stash save "my_stash_message"

Now, you can easily identify and apply this specific stash using the message 'my_stash_message'.

What is Git? (Day 1 Beginner Guide)
What is Git? (Day 1 Beginner Guide)

Applying Stashed Changes

To apply stashed changes, use the git stash apply command. You can apply a specific stash using its index number or the message you provided:

git stash apply stash@{0} or git stash apply "my_stash_message"

After applying, Git will keep the stash in the stash list. To discard the stash after applying its changes, use the -u or --keep-index option:

Git & GitHub Commands Cheat Sheet πŸ’» | Essential Developer Guide
Git & GitHub Commands Cheat Sheet πŸ’» | Essential Developer Guide

git stash apply --u

Advanced Stashing: Cherry-Picking and Cleanup

Sometimes, you might want to stash only specific files or stages. Let's explore these advanced scenarios.

Git
Git
πŸš€ Git Commands Every Beginner Should Memorize
πŸš€ Git Commands Every Beginner Should Memorize
Git Stash
Git Stash
130+ Git Commands Every Developer Should Know
130+ Git Commands Every Developer Should Know
7 Git Commands Every Beginner Should Know | Git & GitHub Basics
7 Git Commands Every Beginner Should Know | Git & GitHub Basics

You can stash only specific files by adding their names to the git stash command:

git stash -- file1.txt file2.py

Stashing Specific Stages

The git stash command also allows you to stash only specific stages (like 'RM' for removed files, 'AM' for modified files):

git stash -p

This command will show you all the changes that can be stashed, and you can choose which stages you want to stash.

Cleaning Up Stashes

After applying stashes, you may want to clean up the stash list to keep things organized:

git stash drop stash@{0}

This command drops stash number 0 from the stash list. Alternatively, you can use the git stash clear command to remove all stashes at once:

git stash clear

With this command, you can maintain a clean, well-organized stash list.

In a nutshell, the git stash command empowers you to manage local changes efficiently, ensuring you're never stuck mid-task due to switching branches or working on multiple features simultaneously. So, embrace the power of git stash and enhance your Git workflow today!