Demystifying Git Stash: Code Juggling Essentials

Demystifying Git Stash: Code Juggling Essentials

Unlock the secrets of seamless development with essential commands for simplifying workflow and code management

In the world of version control, Git is a developer's best friend, and within Git's arsenal lies a somewhat unsung hero – Git stash. If you've ever found yourself in the midst of coding chaos, needing to switch gears quickly without committing half-finished work, Git stash is your lifesaver. In this blog, we'll unravel the mysteries of Git stash, exploring its commands, understanding its importance, and showcasing real-world scenarios where it shines.

Quick Commands Reference:

  1. git stash: Stash your changes.

  2. git stash list: List all stashes.

  3. git stash show: Show changes in the most recent stash.

  4. git stash pop: Apply and remove the most recent stash.

  5. git stash apply: Apply the most recent stash (keep it in the stash list).

  6. git stash apply stash@{revision}: Apply a particular stash.

  7. git stash -u: To stash both tracked and untracked files.

  8. git stash clear: Delete all stashes.

  9. git stash drop stash@{revision}: Delete a particular stash.

  10. git stash branch <branch_name>: Create a new branch from the last stash.

  11. git stash branch <branch_name> stash@{revision}: Create a branch from a specific stash.

Why Git Stash?

Unfinished Work and Context Switching:

Imagine you're knee-deep in coding a new feature on the master branch when an urgent bug report comes in. Git stash allows you to stash your ongoing changes, switch to a different branch, fix the bug, and seamlessly return to your feature development.

Branch Switching and Clean Working Directory:

Git stash shines when you need to move between branches swiftly. Instead of committing changes that aren't ready, stash provides a clean slate for branch hopping. It ensures a tidy working directory, making it easier to focus on the task at hand.

Emergency Fixes and Safe Experimentation:

Stash becomes your ally in addressing critical issues without compromising your ongoing work. It's also a safety net for experimenting with changes. Stash lets you try out ideas, and if they don't work, you can effortlessly revert to your stashed state.

Detailed Exploration of Commands:

  1. Stashing Your Changes (git stash):

    Use git stash to temporarily store changes without committing.

     git stash
    
  2. Listing All Stashes (git stash list):

    See a list of all stashes and their reference numbers.

     git stash list
    
  3. Showing Changes in the Last Stash (git stash show):

    Display changes made in the last stash.

     git stash show
    
  4. Applying and Popping the Last Stash (git stash pop):

    Apply and remove the last stash.

     git stash pop
    
  5. Applying the Last Stash (git stash apply):

    Apply the last stash while keeping it in the stash list.

     git stash apply
    
  6. Applying a Specific Stash (git stash apply stash@{revision}):

    Apply a stash by referencing its number (revision).

     git stash apply stash@{3}
    
  7. Deleting All Stashes (git stash clear):

    Remove all stashes from the stash list.

     git stash clear
    
  8. Stashing untracked files (git stash -u):

    If you want to stash both changes to tracked files and untracked files simultaneously, you can use the -u or --include-untracked option with git stash. This option includes untracked files in the stash.

     git stash -u
    

    Note that untracked files stashed in this way won't be listed in the subsequent git stash list output, as they are not changes that can be applied or popped like changes to tracked files.

  9. Deleting a Specific Stash (git stash drop stash@{revision}):

    Discard a specific stash using its reference number.

     git stash drop stash@{2}
    
  10. Creating a New Branch from the Last Stash (git stash branch <branch_name>):

    Start a new branch based on the last stash.

    git stash branch <branch_name>
    
  11. Creating a Branch from a Specific Stash (git stash branch <branch_name> stash@{revision}):

    Begin a branch from a specific stash using its reference.

    git stash branch <branch_name> stash@{revision}
    

Git Stash in Action: Switching to the Right Branch

Let's say you've mistakenly started coding on the master branch, but your intended changes should be on the existing feature/live-chat branch. Here's how Git stash comes to your rescue:

  1. Stash Your Changes on Master:

     git stash
    
  2. Switch to the Target Branch:

     git checkout feature/live-chat
    
  3. Apply the Stashed Changes:

     git stash apply
    
    • Alternatively, use git stash pop if you want to drop the stash after applying.
  4. Commit on the Correct Branch:

     git commit -m "Your commit message"
    

By using Git stash, you've seamlessly switched branches, preserved your changes, and committed them to the correct branch. It's a simple yet powerful workflow that keeps your version control history organised.

Summary:

Git stash is not just a feature; it's a versatile tool that empowers developers to navigate the complexities of software development with finesse and efficiency. Whether you're juggling multiple tasks, experimenting with changes, or addressing urgent issues, Git stash has got your back. As you integrate these stash commands into your workflow, you'll discover a newfound agility in managing your codebase. Happy stashing!