Hamburger Icon
  • Labs icon Lab
  • Core Tech
Labs

Guided: Advanced Stashing Techniques

Git stashing is an incredibly useful concept that can be applied to almost every developer workflow. This hands-on lab will help you get up to speed with advanced stashing techniques that will help you alleviate the cost of context switching, boost your feature development workflow, and give you a means of easily cleaning your working directory without losing changes! By the end of the lab, you will have used git stashing techniques to create and compose multiple different features in an application.

Labs

Path Info

Level
Clock icon Advanced
Duration
Clock icon 26m
Published
Clock icon Feb 13, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    ### Introduction

    Stashing is an important and very useful concept that will boost your development workflow. You may find yourself working on a feature or bugfix only having to shift focus because of an urgently needed hotfix or due to changing scope, etc.

    So, what then should you do with the changes in your current working directory? Enter git stash and its subcommands! Stashing lets you quickly store off unstaged changes in your current branch so that you don’t lose all of your precious work! It facilitates a feature development workflow that is simple, easy to reason about, and flexible.

    Please feel free to reference the git stash documentation as you complete this guided code lab.

    Note: Changes made to this filesystem using Git will not automatically be conveyed in the filetree. If you want to visualize changes in affected files, you must exit out of the file and reopen it.

  2. Challenge

    ### Creating, Viewing and Applying Simple Stashes

    At a high level, you can think of git stashing as a simple stack of dishes where each stash is like a dish. You can git stash push stashes on to the top of the stack. You can also git stash pop stashes off the top of the stack. If you’d like to quickly create a stash because you know that you will use it soon, you can simply use the command git stash. This is most useful when you need to quickly clean your working directory of unstaged changes without losing work! Git provides the subcommand git stash list so that you can easily list all of the current stashes along with their name and index. This is very helpful when you have a lot of saved stashes and you want to apply them selectively. Going a bit deeper, you can also take the name of a stash and get a description of the changes with that stash via git stash show . The git stash pop command is great for when you are working with simple, unnamed stashes and you know that you just want the last created stash that is sitting at the top of the stack.

  3. Challenge

    ### Stashing Techniques for Feature Development

    In the last step you focused on simple, unnamed stashes. In this step you’ll learn how to create named stashes and use them within the context of feature development on different branches. Named stashes become very important when you are working on multiple different features in tandem. Context switching from feature to bugfix to feature is often a harsh reality in software development today, but stashing can help alleviate a lot of the complexity involved! First, you need to create a new branch and make a change to simulate some feature development. Uh oh, you just got word that you need to create another feature with a higher priority than the one you are currently working on. This is where git stash push –m comes in handy. The -m flag is much akin to the git commit –m flag in that it lets you attach a custom message to a stash!

    Note: You have been introduced to the concept of unnamed and named stashes, but in reality, every stash is named behind the scenes. By default, when you create an “unnamed” stash, git attaches the stash index (its position on the stack) and the branch name that the stash was created from. The -m flag will add an additional custom message onto the end of this name so that you can easily identify your changes! It turns out that the new feature is going to incorporate some of your stashed change. Now that you’ve stashed your previous changes, you can create a new branch and automatically apply your saved stash on to the branch using git stash branch . With this feature development done, now you can stage your changes and commit them.

  4. Challenge

    ### Selectively Applying and Cleaning Up Stashes

    It is true that stashes are stored as a stack and that you can pop stashes from the top of the stack or push stashes onto the top of the stack. But what if you have a stash that exists near the bottom of the stack and you want to apply that stash to your current branch... Well that is where git stash apply comes in. The git stash apply subcommand let’s you pass a stash name to it and git will handle applying the stash.

    An added benefit to git stash apply is that executing this command will not pop off a "dish" from the stack. You'll still have that stash available to use later on which makes git stash apply great for testing the application of different changes introduced from stashes.

    Note: You will need the stash index of the stash you want to apply – this is really where git stash list and git stash push –m come in handy! Since they both let you easily find saved stashes on the stack. This working directory has a few stashes that have been created prior to you beginning this lab. You need to find the “Todo list” stash using git stash list and apply it using git stash apply. It’s quite easy to apply one stash on top of another stash. Now you need to find the “Header” stash using git stash list and apply it using git stash apply

    Note: You cannot git stash pop or git stash apply changes onto your working directory if you have unsaved changes there. You need a clean, working directory. Now what about stashes that you no longer need? You have a few options here. Firstly, you can use git stash drop to remove a given stash from the cache. If you want to cleanup all of your stashes in a git repo, you can simply run git stash clear. This is helpful if you have a lot of stashes which themselves contain many changes as you can reclaim disk space this way.

  5. Challenge

    ### Conclusion

    Well done! You have learned the ins and outs of advanced stashing techniques. Simple stashes are the most common way of using stashes during development. But if you are working in a fast-paced development environment, the more advanced stashing techniques given to you via the git stash push –m, git stash apply, and git stash branch commands are highly useful and can save you a lot of time while providing a smooth development experience.

    Throughout this lab we learned that stashing provides the following benefits:

    • Alleviates the cost of context switching (AKA interrupted development workflow)
    • Provides an easy-to-use place to store of prospective changes while testing out new ideas
    • Let's you easily clean your working directory of unstaged changes without losing work or committing work unnecessarily

    From here, it's recommend that you continue your journey of practical learning by checking out more of the git Guided Code Labs, here, at Pluralsight.

Zach is currently a Senior Software Engineer at VMware where he uses tools such as Python, Docker, Node, and Angular along with various Machine Learning and Data Science techniques/principles. Prior to his current role, Zach worked on submarine software and has a passion for GIS programming along with open-source software.

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.