Simple Guide on Git Stash command

Tanya Bond 
2 min readMar 19, 2024

--

As you know, Git is a developer’s essential tool for tracking changes in any set of files. The Git Stash command gives you the ability to save changes that you have made to your working directory but don’t want to commit yet, allowing you to reapply them later.

Today I’ll give you a quick overview of how to utilize the Git Stash command effectively.

Understanding Git Stash

Git Stash works like a clipboard for your changes; it allows you to switch to a different branch while saving your current work elsewhere. It provides a parking spot for your changes, enabling you to reapply them later when you’re ready.

Using Git Stash

✨ Saving Changes

To save your changes, you use the git stash command. This command takes your modified tracked files and staged changes, saves them for later use, and then reverts them to their state at the last commit:

git stash save "message"

The “message” is an optional description for the stash. If you don’t provide a message, Git automatically creates one for you. Sometimes I use just command “git stash”, and my changes are saved.

✨ Listing Stashes

You can have multiple stashes, and to list them in your terminal, you use the ‘git stash list’ command. This command displays all the stashes that you currently have:

git stash list

✨ Applying Stashes

When you’re ready to apply your stashed changes, you can use the ‘git stash apply’ command. This command does not remove the changes from your stash list — it merely applies them to your working directory:

git stash apply

If you have multiple stashes, you can specify the stash to apply like this:

git stash apply stash@{2}

To apply a stash by name use:

git stash apply stash^{/my_stash_name}

✨ Dropping Stashes

If you want to delete a stash after applying it, you can use this command:

git stash drop stash@{2}

✨ Popping Stashes

The pop command applies the stash and then immediately removes it from your stash list:

git stash pop stash@{2}

To apply and remove a stash by name:

git stash pop stash^{/my_stash_name}

✨ Creating a Branch from a Stash

If the changes in your stash are too significant and you want to isolate them, you can create a new branch:

git stash branch new_branch_name stash@{2}

This command creates a new branch, checks it out, applies the stash, and drops it if applied successfully.

Conclusion and resources

The Git Stash command is a very simple but powerful tool. It allows you to switch between branches without losing your work and enables you to apply, drop, and even create new branches from your stashes. It can enhance your productivity and ease in handling code modifications.

Documentation: https://git-scm.com/docs/git-stash

--

--

Tanya Bond 

iOS Developer with experience in Web Development and Engineering Management. Based in Cali 🌴🇺🇸