Stashing Changes in Git

In Git, stashing lets you save uncommitted changes temporarily, clearing your working directory to focus on other tasks or switch contexts. This command is particularly helpful when you’re working on something and need to temporarily set aside your changes without committing them. Let’s explore how to use Git’s stashing feature effectively.

How to Create a Stash

To create a stash, use the following command:

				
					git stash

				
			

Explanation

The above command stashes all uncommitted changes (both staged and unstaged) in tracked files. Once the stash is created, your working directory returns to the last committed state, and all modified files are removed temporarily.

Example: If you have modified file1.txt and file2.txt, running git stash will save those changes and clear your working directory, allowing you to work with a clean state.

Adding a Custom Message

To label the stash with a message for easier identification later, use:

				
					git stash push -m "Message describing the changes"

				
			

Example: git stash push -m "Updated homepage layout and styles" creates a stash with a custom message. This helps in easily identifying the purpose of each stash.

Listing and Viewing Stashes

Listing All Stashes

To view all the stashes in your repository, use:

				
					git stash list

				
			

This command shows all stashes in a stack format. Each stash is labeled as stash@{index}, where index represents the position of the stash in the stack (starting from stash@{0} for the most recent).

Example Output:

				
					stash@{0}: WIP on main: 4567abc Updated homepage layout
stash@{1}: WIP on main: 1234def Add search functionality

				
			

Viewing Stash Details

To see what changes a stash contains, use:

				
					git stash show stash@{0}

				
			

For a detailed view of the exact modifications:

Applying and Popping Stashes

Applying a stash reintroduces the saved changes into your working directory.

Applying a Stash

Use apply to retrieve the changes from a stash without removing it from the stack:

				
					git stash apply stash@{0}

				
			

If you don’t specify an index, Git will apply the latest stash (stash@{0}) by default.

  • Explanation: apply is ideal when you want to keep a copy of the stash for future use, as it doesn’t delete the stash from the stack.

Popping a Stash

The pop command works like apply but also removes the stash from the stack after applying it:

				
					git stash pop stash@{0}

				
			

Explanation: Use pop when you no longer need the stash after retrieving it, helping to keep your stash stack tidy.

Advanced Stashing Techniques

Stashing Untracked Files

To stash untracked files (files not added to the Git index), use:

				
					git stash push -u

				
			
  • Explanation: This command is useful if you’ve created new files that you don’t want to commit but still want to save temporarily.

Stashing Ignored Files

To stash files ignored by Git (as per .gitignore), use:

				
					git stash push -a

				
			

Partial Stashing with Patch Mode

If you only want to stash specific changes, use interactive patch mode:

				
					git stash push -p

				
			

Explanation: In patch mode, Git will prompt you to select which changes to include in the stash. This is useful when you want to stash certain parts of a file while leaving other parts in your working directory.

Deleting and Clearing Stashes

When stashes are no longer needed, you can delete them individually or clear all at once.

Dropping a Specific Stash

To remove a particular stash, use:

				
					git stash drop stash@{1}

				
			

Clearing All Stashes

To delete all stashes at once:

				
					git stash clear

				
			

Explanation: This command is especially useful when you’ve applied or no longer need the changes stored in the stash stack and want to keep your repository clean.

Understanding Stash Conflicts

Sometimes, when applying a stash, you may encounter conflicts if there are changes in your working directory that clash with the stashed changes. Git will alert you to these conflicts, and you’ll need to resolve them just as you would with merge conflicts.

  • Example: If there’s a conflict while applying stash@{0}, Git will leave the conflicted files in your working directory, marking the areas of conflict. After resolving, stage the changes and continue with your workflow.

Best Practices with Stashing

  • Label Stashes with Descriptive Messages: Always add messages to stashes for easier identification, especially in collaborative projects.
  • Clear Outdated Stashes: Regularly remove stashes that are no longer needed to avoid a cluttered stack.
  • Use Stashing Judiciously: Rely on stashing as a temporary solution and avoid accumulating stashes over time.
  • Resolve Conflicts Promptly: When applying stashes that lead to conflicts, resolve them immediately to maintain a clean workflow.

Stashing changes in Git is a powerful tool for managing temporary changes, giving you the flexibility to save work-in-progress without committing. By understanding how to create, apply, delete, and manage stashes, you’ll be able to handle interruptions or multitask within a Git repository efficiently. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India