Lesson 8
Git Stashing
In this lesson, we will explore the concept of Git stashing, which allows you to temporarily save changes that you do not want to commit yet. This can be useful when you are in the middle of working on a feature or bugfix and need to switch to a different branch or task.
Objectives
- Understand the purpose of Git stashing
- Learn how to create and apply stashes
- Practice stashing and applying changes
Stashing changes
To stash changes, use the git stash
command. This will save your current changes in a new stash and revert your working directory to the last commit. You can also provide a message to describe the changes using git stash save "your message"
.
Applying stashed changes
To apply stashed changes, use the git stash apply
command followed by the stash name (e.g., stash@{0}
). This will apply the changes from the specified stash to your working directory. If you want to apply the changes and remove the stash, use git stash pop
instead.
Creating and applying patches
You can also create a patch from a stash using git stash branch <branchname>
command. This will create a new branch and apply the stash to it. To apply a patch to your current branch, use the git apply
command followed by the patch file.
Exercises
Stash changes
- Create a new file or modify an existing file in your local repository
- Use the
git stash
command to temporarily save your changes - Optionally, provide a message to describe the changes.
Apply stashed changes
- Use the
git stash list
command to view your stashes - Apply the changes from a specific stash to your working directory using the
git stash apply
command followed by the stash name - Alternatively, use
git stash pop
to apply the changes and remove the stash.
Create and apply a patch
- Create a new branch and apply a stash to it using the
git stash branch <branchname>
command - Create a patch file from the changes in the new branch using the
git diff
command - Apply the patch to your current branch using the
git apply
command followed by the patch file.