Applying Stashed Changes

When working on a Git project, there are situations where you may need to save uncommitted changes temporarily without committing them. Git’s stash feature allows you to "stash" these changes, temporarily saving your work and clearing the working directory. Later, you can reapply these stashed changes to continue where you left off.

Introduction to Git Stash

The git stash command allows users to temporarily save (or “stash”) uncommitted changes in their working directory, making it easy to switch contexts without losing ongoing work.

When to Use Git Stash:

    • When you want to switch branches but have uncommitted changes.
    • When you’re experimenting with code changes but don’t want to commit them yet.
    • When a task requires a clean working directory.

Basic Stashing Operations

Stashing Changes

To stash all modified and staged changes, use:

				
					git stash
				
			

This command takes the current state of your working directory and saves it in the stash list, while your working directory reverts to the latest commit state.

  • Explanation: This stores changes in the stash and resets your working directory to a clean state, as if no changes were made.

Naming a Stash (Optional)

By default, Git assigns a default name to each stash, but you can give it a custom name for easier reference

				
					git stash save "my-feature-work"

				
			

Explanation: save "my-feature-work" stores your changes with the specified name, making it easier to identify later.

Applying Stashed Changes

When you’re ready to bring back your stashed changes, Git provides two main commands: git stash apply and git stash pop.

Using git stash apply

				
					git stash apply

				
			

This command reapplies the most recent stash to your working directory without removing it from the stash list.

  • Explanation: It brings your most recent stash back into the working directory, allowing you to continue working with the changes but keeps the stash stored in the list.

Using git stash pop

				
					git stash pop

				
			

This command applies the most recent stash and removes it from the stash list.

  • Explanation: pop is often used when you want to reapply changes and clear the stash since you no longer need to keep it stored.

Applying a Specific Stash

If you have multiple stashes, you can specify which one to apply by referencing its position in the stash list:

				
					git stash apply stash@{1}

				
			

Explanation: stash@{1} refers to the second stash in the list, allowing you to apply a specific stash rather than the most recent one.

Managing Multiple Stashes

You can list all stashes in the stash stack using:

				
					git stash list

				
			

Explanation: This command shows all saved stashes, including their names or default descriptions, enabling you to identify which stash to apply.

Partial Stashing

Partial stashing allows you to stash specific files or even specific changes within files.

Stashing Specific Files

To stash changes in particular files only, use:

				
					git stash push -p file1.txt file2.txt

				
			

Explanation: This command stashes only changes in file1.txt and file2.txt, leaving the rest of the working directory untouched.

Interactive Stashing (-p flag)

The -p or --patch option lets you select specific changes to stash.

				
					git stash push -p

				
			

Explanation: When you use -p, Git will walk you through each change interactively, letting you choose which ones to stash.

Advanced Stash Options

Keeping Index Changes

If you only want to stash working directory changes but keep staged files in the index, use:

				
					git stash --keep-index

				
			

Explanation: This command stashes only the unstaged changes, leaving staged changes intact in the index, making it useful when you’re selectively stashing.

Stashing Untracked Files

If you have untracked files that you also want to stash, use:

				
					git stash -u

				
			
  • Explanation: The -u flag includes untracked files in the stash, storing all changes made to files not yet tracked by Git.

Deleting Stashes

Once you no longer need a stash, it’s a good idea to delete it to keep your stash list manageable.

Removing a Specific Stash

Use:

				
					git stash drop stash@{1}

				
			

Clearing All Stashes

To delete all stashes at once, use:

				
					git stash clear

				
			

Explanation: This command removes all stashes, which can be helpful for keeping your project clean after you’ve applied or no longer need certain stashes.

Best Practices with Stashing

  • Use Descriptive Stash Messages: When creating stashes, add a message describing the changes.
  • Clear Old Stashes: Regularly review and remove unnecessary stashes to keep your project organized.
  • Avoid Stashing Frequently: If you find yourself stashing often, consider if a more structured approach, like branching, would better suit your workflow.
  • Resolve Conflicts Promptly: When applying a stash leads to conflicts, resolve them right away to avoid confusion.

Stashing is a powerful feature in Git that allows you to save your work temporarily and return to it when you’re ready. From basic usage to advanced techniques, understanding how to effectively use git stash will make handling multiple tasks and managing changes smoother. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India