Introduction
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
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.
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".
- Change to the
git-playgrounddirectory:
cd git-playground
- Switch to the
masterbranch and stash it after fixing the bug, the stash message is "fix the bug". Fix the bug by updating the contents of thefile1.txtfile to "hello,world":
git checkout master
echo "hello,world" > file1.txt
git stash save "fix the bug"
- Switch to the
feature-branchbranch, 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.