Skip to main content

Stashing: save your changes without committing or discarding

Sometimes you're working on something, but you're not ready to commit the changes. This usually happens because:

  • Maybe the changes you've made so far are incomplete, and wouldn't make sense on their own.
  • Or maybe you want to pull some changes from the remote repository that someone else has made, but you don't want to make a commit just in case there are merge conflicts.

You can, at any point, save all the changes you've made so far and store them in the stash1.

Later on your can apply the changes from the stash if you want.

As an example, look at this git status:

$ git status

Changes to be committed:
(use "git reset HEAD <file>..." to unstage)

modified: README.md

Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)

modified: app.py

If you run this:

$ git stash

Saved working directory and index state \
"WIP on master: 049d078 added app.py and a readme file"
HEAD is now at 049d078 added app.py and a readme file
(To restore them type "git stash apply")

Then run git status again:

$ git status

# On branch master
nothing to commit, working directory clean

When we ran git stash itself, Git told us how to bring back the changes: just run git stash apply.

However before running git stash apply we could run git pull for example, to bring some changes from the remote repository.

There's quite a lot you can do with the stash, including things like git stash list to see previously-stashed sets of changes.

Footnotes

  1. 7.3 Git Tools - Stashing and Cleaning (git-scm.com)