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 swtich to another branch and do not want to commit the change
  • Partial commit
## * master
## On branch master
## Changes to be committed:
##   (use "git reset HEAD <file>..." to unstage)
## 
##  new file:   temp_script.sh
## 
## Untracked files:
##   (use "git add <file>..." to include in what will be committed)
## 
##  .gitignore


13.1 Stash

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


13.2 Unstash

## On branch master
## Changes to be committed:
##   (use "git reset HEAD <file>..." to unstage)
## 
##  new file:   temp_script.sh
## 
## Untracked files:
##   (use "git add <file>..." to include in what will be committed)
## 
##  .gitignore
## 
## stash@{0}: WIP on master: 74bd563 Adding loss.txt while in detached HEAD


13.3 Delete Stash

## Dropped refs/stash@{0} (9332cd2d349b059bee6aa7b04f4f713089b37512)


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


13.4 Branch a Stash

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