Git Stash

Git stash is a powerful feature that allows you to temporarily save changes in a Git repository without committing them. This is particularly useful when you’re in the middle of working on a feature and need to quickly switch to another branch or task without losing your current changes. Stashing saves the current state of the working directory and index (the staging area), allowing you to restore them later.

Understanding the Basics of Git Stash

What is Git Stash?

    • Git stash allows you to “shelve” or “stash away” uncommitted changes in your working directory so you can work on something else and then come back to your changes later.

How Does It Work?

    • When you use Git stash, it saves your modified tracked files, staged changes, and any new files to a stack of changes that you can apply later.

Basic Stashing Commands

    • git stash: Stashes all modified tracked files, staged changes, and new files.
    • git stash list: Shows all stashes you’ve created, each with an identifier like stash@{0}, stash@{1}, and so on.
    • Example

How to Stash Changes

To stash your changes, you can use the basic command git stash. Let’s say you have modified two files, file1.txt and file2.txt, but you aren’t ready to commit them. Running git stash will save both files to the stash stack and revert your working directory to the last committed state. You can confirm that the changes were stashed by checking with git stash list, which will show a list of all saved stashes with unique identifiers, like stash@{0}, and a short description for each.

				
					git stash
git stash list

customElements.define('my-element', MyElement);

				
			

In the output of git stash list, you’ll see each stash with its identifier and timestamp, making it easy to locate specific changes later.

Applying Stashes with git stash apply and git stash pop

Once you’ve stashed changes, there are two main ways to bring them back to your working directory: git stash apply and git stash pop. When you use git stash apply, Git reapplies the stash to your working directory but keeps the stashed changes in the list. This is useful if you plan to reuse the stash later.

				
					git stash apply stash@{0}

				
			

The git stash pop command works similarly but removes the stash from the list after reapplying it. This approach is helpful when you’re confident you won’t need the stashed changes anymore. By default, git stash apply applies the most recent stash, but you can specify any stash by its identifier.

For example:

Using Specific Flags with Stash

Git stash provides several useful flags for more targeted stashing. The --keep-index flag allows you to stash only the unstaged changes, preserving staged ones. For example, if you want to keep the staged changes intact but stash the rest, this command is invaluable.

Another useful flag is -u (or --include-untracked), which includes untracked files in the stash. Typically, Git stash does not include untracked files by default, so this option lets you handle new files created in the working directory along with your modified files. For cases where you want to include both untracked and ignored files, the -a flag (or --all) will stash everything, providing the cleanest state possible.

Examples:

				
					git stash --keep-index
git stash -u
git stash -a

				
			

Naming Stashes for Better Organization

When working on multiple stashes, giving each a descriptive name makes it easier to manage and locate them later. This is especially beneficial in larger projects. You can name a stash by using the save command with a descriptive label. For example:

				
					git stash save "Work on Feature X - WIP"

				
			

Accessing and Applying Specific Stashe

If you have several stashes, applying a specific one is easy. Simply refer to it by its identifier, as in git stash apply stash@{1}, and Git will bring those changes back to the working directory. This flexibility lets you manage different stashes individually and work with any stashed state you need at any given time.

For example:

				
					git stash apply stash@{1}

				
			

Managing Stashes with Deletion Commands

When you no longer need a stash, removing it from the stack helps keep your stash list manageable. You can delete a specific stash with git stash drop by specifying its identifier, or clear the entire stack with git stash clear. Clearing stashes is an irreversible action, so it’s best to be certain before using it.

For example:

				
					git stash drop stash@{0}
git stash clear

				
			

Advanced Techniques with Stash

One advanced feature of Git stash is the ability to create a branch directly from a stash, which can be especially useful if you decide mid-way through that the stashed changes deserve their own branch. To do this, use git stash branch followed by the new branch name and stash identifier.

For example:

				
					git stash branch new-feature-branch stash@{0}

				
			

Git will create a new branch, apply the stash to it, and leave your current branch unchanged.

Resolving Conflicts in Stashes

Occasionally, conflicts arise when applying or popping a stash, particularly if the files in the stash differ from those in the current working directory. Git will alert you to these conflicts, allowing you to review and resolve them just as you would with merge conflicts. Once resolved, you can continue working with the stash as usual.

Practical Use Cases for Git Stash

In real-world workflows, Git stash proves invaluable. For instance, if you’re halfway through implementing a new feature and need to urgently fix a bug in another branch, stashing lets you switch branches without committing incomplete work. Similarly, if you’re experimenting with code changes and want to “shelve” them temporarily to test a different approach, Git stash lets you save your progress without committing.

Git stash is an essential Git feature that allows developers to manage temporary changes seamlessly. By using stashing commands, custom naming, targeted stashing, and advanced techniques, developers can create a workflow that maintains a clean working environment and maximizes productivity. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India