Stash Changes – Git Stash
git stash
is used to separately manage work-in-progress (WIP) codes. When you want to switch the current branch in the middle of editing the Working Tree and INDEX, the edits can prevent you from switching the current branch (refer to the git checkout
page). In that case, the git stash
command is useful. The stashed lines of code are parked somewhere temporarily and make the Working Tree and INDEX clean (the Working Tree and INDEX statuses become the same as the HEAD status) so that you can switch branches.
Key Git Stash Commands
git stash
: stash the Working Tree and INDEXgit stash save "[stash message]"
: stash the Working Tree and INDEX with a stash messagegit stash list
: list up stashed itemsgit stash apply [stash number]
: restore a stashed itemgit stash pop [stash number]
: restore a stashed item and delete it from the stash listgit stash drop [stash number]
: delete a stashed itemgit stash clear
: delete all the stashed items
For a better understanding, please go through the following practice section.
Practice
Developer A (Project Owner Role)
Objective:
Learn how to use the git stash commands
1. Prepare a practice directory and file
Create a new practice directory and file
For this practice, we create a new directory and file.
- Practice project directory: git_branch_stash_practice
- Practice file: git_branch_stash_practice.html
We create the practice directory under the main project directory (e.g., Dev_A_bloovee). Run the commands below to create the directory and file.
cd ~/Dev_A_bloovee
mkdir git_branch_stash_practice
cd git_branch_stash_practice
touch git_branch_stash_practice.html
Edit the practice HTML file (master and Branch_A)
For the master branch
Open the git_branch_stash_practice.html file with a text editor and make the following edits.
Subscribe now for
uninterrupted access.