13 Stashing (git stash)

There are times when you may want to stash (set aside) your current work rather than commit.

Examples:

  • Realized that you were working on the wrong branch
  • Realized that you and your collaborator are working on the same lines and resolving conflicts may be difficult
  • Need to immediately switch to another branch and do not want to commit the change
  • Partial commit
git branch
touch temp_script.sh
git add temp_script.sh
git status
## * master
## On branch master
## Changes to be committed:
##  new file:   temp_script.sh
## 
## Untracked files:
##  .gitignore


13.1 Stash

git stash
git stash list
## Saved working directory and index state WIP on master: 196a84c Adding loss.txt while in detached HEAD
## stash@{0}: WIP on master: 196a84c Adding loss.txt while in detached HEAD


13.2 Unstash

git stash apply
git stash list
## On branch master
## Changes to be committed:
##  new file:   temp_script.sh
## 
## Untracked files:
##  .gitignore
## 
## stash@{0}: WIP on master: 196a84c Adding loss.txt while in detached HEAD
git stash apply --index
git stash list


13.3 Delete Stash

git stash drop
git stash list
## Dropped refs/stash@{0} (8671050f90b16604533352792efdf7fe2163fa21)


Note: You can perform both git stash apply and git stash drop at the same time by entering git stash pop.


13.4 Branch a Stash

Probably the best use of stash: git stash branch <branch_name>

git stash branch <branch_name>