Create a Git Stash

GitGitBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL git(("`Git`")) -.-> git/DataManagementGroup(["`Data Management`"]) git/DataManagementGroup -.-> git/stash("`Save Changes Temporarily`") subgraph Lab Skills git/stash -.-> lab-12757{{"`Create a Git Stash`"}} end

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
  1. Switch to a branch named feature:
git checkout -b feature
  1. Make some changes to the files in the directory:
echo "Some changes" >> README.md
  1. Save your changes to a stash:
git stash save "My changes"
  1. Switch to a different branch:
git checkout master
  1. When done making changes on the other branch, switch back to the feature branch and apply your stash:
git stash apply

This is the finished result:

stash@{0}: On feature: My changes

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.

Other Git Tutorials you may like