Git

Last Updated: 8/24/2023

Stashing

  • When we switch branches, git resets our working directory to the snapshot stored in the last commit of the target branch. - - If you have local changes in the working directory that are not committed yet, these changes could get lost in these situations. So git doesn't allow us to switch branches.
  • To switch branches whenever there is changes, you can either commit or stash the changes.
  • Stashing means storing somewhere (safe place) in our Git repository.
git stash push -m "New changes". 
  • Stashing is not part of the history.
  • By default, new untracked files are not included in your stash, to include it, we have to use the all option.
git stash push --all "My new stash"
git stash -am "My new stash"

List stash

git stash list

View stash

Before applying the changes from stash, we want to look at those changes to see what lines of code have been modified.

git stash show stash@{1}
or
git stash show 1

Apply stash

  • To apply the changes to working directory
git stash apply 1

Remove stash

git stash drop 1
git stash drop 0

Remove all stashes

git stash clear