Apply a stash

GitGitBeginner
Practice Now

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

Introduction

In Git, a stash is a way to save changes that are not ready to be committed yet. It allows you to temporarily save your changes and switch to another branch or work on something else. Once you are ready to continue working on your changes, you can apply the stash and continue where you left off.


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-12699{{"`Apply a stash`"}} end

Apply a stash

You are working on a feature branch in the git-playground repository and you need to switch to another branch to work on a bug fix. However, you have some changes that are not ready to be committed yet. You want to save these changes and switch to the other branch. Once you are done with the bug fix, you want to apply the stash and continue working on your feature branch.

The changes have been stashed on the feature-branch branch, and the stash message is "my changes".

  1. Change to the git-playground directory:
cd git-playground
  1. Switch to the master branch and stash it after fixing the bug, the stash message is "fix the bug". Fix the bug by updating the contents of the file1.txt file to "hello,world":
git checkout master
echo "hello,world" > file1.txt
git stash save "fix the bug"
  1. Switch to the feature-branch branch, look at the list of stashes, and apply the stash whose information is "my changes":
git checkout feature-branch
git stash apply stash@{1}

This is the contents of the README.md file:

## git-playground
Git Playground
some changes

You should see that the changes you made before stashing are now applied.

Summary

In this lab, you learned how to apply a stash in Git. Applying a stash allows you to temporarily save your changes and switch to another branch or work on something else. Once you are ready to continue working on your changes, you can apply the stash and continue where you left off.

Other Git Tutorials you may like