Git -

Stashing


Introduction

Git stashing is a powerful tool that allows you to temporarily store modified, tracked files and staged changes, enabling you to clean your working directory without committing. This tutorial dives deep into how to effectively use stashing in your Git workflow.


1. Creating Stashes

To stash your changes and keep your working directory clean:

        
            git stash
        
    
Or, to create a stash with a specific message:
        
            git stash save "Your descriptive message"
        
    
Or, using the modern syntax with -m to provide a message:
        
            git stash push -m "Your descriptive message"
        
    

Output
        
            stash@{0}: On dev: Your descriptive message

        
    

dev - branch name. Your descriptive message - stash message


2. Viewing Stashes

To see a list of all your stashed changes, which are stored in a stack:

        
            git stash list
        
    

3. Applying Stashes

To apply a specific stashed change without removing it from the stash stack:

        
            git stash apply
        
    

4. Applying Specific Stashes

You can apply a specific stash from your stash list without removing it from the stash stack, allowing you to revisit or apply other stashes later. Here’s how:

This approach is useful when you need to apply older stashes without popping the most recent one, maintaining the order and integrity of your stashed changes.


5. Removing Stashes

To apply the most recent stash and remove it from the stack:

        
            git stash pop
        
    

6. Removing Specific Stashes

To remove a specifically named or indexed stash, use the `git stash drop` command followed by the stash identifier. Stashes are identified by their stack index, such as `stash@{0}`, which refers to the most recent stash.

Here's how to drop a specific stash by its index:

git stash drop stash@{0}

If you have named your stash and need to find it, you can list all stashes and locate it by description:

        
            git stash list
        
    

Output
        
            stash@{0}: On dev: stash message 1
stash@{1}: On dev: stash message 2

        
    

Once you've identified the correct stash index from the list, you can drop it using the drop command with the specific index:

git stash drop stash@{n}

Replace `n` with the index number of the stash you wish to remove, based on its position in the stash list shown by git stash list.


7. Clearing All Stashes

To remove all stashed entries from your stack:

        
            git stash clear
        
    

8. Stash Best Practices

Effective stash management involves more than just storing changes. Here are some best practices:


9. Conclusion

Mastering the use of Git stashing can significantly enhance your flexibility when managing changes across different branches. With the detailed strategies and commands outlined in this tutorial, you can maintain a clean and organized workflow while handling multiple tasks simultaneously.