DEV Community

Cover image for Mastering Git Stash: Seamless Workflow Management
Labby for LabEx

Posted on

Mastering Git Stash: Seamless Workflow Management

Introduction

This article covers the following tech skills:

Skills Graph

🧑‍💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.

Git is a powerful version control system that allows developers to keep track of changes made to their codebase. One of the useful features of Git is the ability to create a stash. A stash allows you to save the current state of your working directory and index, so you can switch to a different branch or work on a different feature without losing your changes.

Create a Git Stash

As a developer, you may find yourself in a situation where you need to switch to a different branch or work on a different feature, but you're not ready to commit your changes yet. You don't want to lose your progress, but you also don't want to commit incomplete or buggy code. This is where a stash comes in handy.

A stash allows you to save your changes without committing them, so you can switch to a different branch or work on a different feature. You can then apply your stash later when you're ready to continue working on your changes.

To create a stash, you can use the git stash save command. Let's say you're working on a branch named feature in the git-playground repository and you want to save your changes before switching to a different branch:

  1. First, navigate to the git-playground directory:
cd git-playground
Enter fullscreen mode Exit fullscreen mode
  1. Switch to a branch named feature:
git checkout -b feature
Enter fullscreen mode Exit fullscreen mode
  1. Make some changes to the files in the directory:
echo "Some changes" >> README.md
Enter fullscreen mode Exit fullscreen mode
  1. Save your changes to a stash:
git stash save "My changes"
Enter fullscreen mode Exit fullscreen mode
  1. Switch to a different branch:
git checkout master
Enter fullscreen mode Exit fullscreen mode
  1. When done making changes on the other branch, switch back to the feature branch and apply your stash:
git stash apply
Enter fullscreen mode Exit fullscreen mode

This is the finished result:

stash@{0}: On feature: My changes
Enter fullscreen mode Exit fullscreen mode

Summary

Creating a stash in Git allows you to save your changes without committing them, so you can switch to a different branch or work on a different feature. You can then apply your stash later when you're ready to continue working on your changes. Use the git stash save command to create a stash, and the git stash apply command to apply your stash.


🚀 Practice Now: Create a Git Stash


Want to Learn More?

Top comments (0)