Introduction
This article covers the following tech skills:
🧑💻 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:
- First, navigate to the
git-playground
directory:
cd git-playground
- Switch to a branch named
feature
:
git checkout -b feature
- Make some changes to the files in the directory:
echo "Some changes" >> README.md
- Save your changes to a stash:
git stash save "My changes"
- Switch to a different branch:
git checkout master
- 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.
🚀 Practice Now: Create a Git Stash
Want to Learn More?
- 🌳 Learn the latest Git Skill Trees
- 📖 Read More Git Tutorials
- 💬 Join our Discord or tweet us @WeAreLabEx
Top comments (0)