Stash
Git stash will take all changes tracked by git—i.e. changes to index and change to work tree—and store them into the "stash". It is a special commit not part of the repository history.
We can think of stash as a stack of temporary changes. We may use them later. We may not.
Command
git stash
We can provide a message to the stash:
git stash -m "a message"
Stashes can be listed:
git stash list
git stash show
We can pop the latest stash:
git stash pop
git stash pop --index <index>
Exercise: Stashing changes
- In the upstream repository
learn-git
, add and commit a change to a new file.- In the downstream repository
remote-git
, make a change to theREADME.md
file, but do not commit it.- In this example we pretend we need to put our temporary changes aside, because something came up, and we need to work on something else. So stash the changes.
Solution: Stashing changes
Navigate to the learn-git
repository.
echo "upstream change" >> upstream.md
git add upstream.md
git commit -m "upstream change"
Navigate to the remote-git
repository.
echo "downstream change" >> README.md
Stash the changes:
git stash
Output:
Saved working directory and index state WIP on feature: 58dab59 upstream change
Exercise: Playing around with stashes
- List the stashes.
- Show the latest stash.
- Pop the latest stash.
- Stash again with a custom message.
- List the stashes again.
Solution: Playing around with stashes
git stash list
git stash show
git stash pop
git stash -m "custom changes"
git stash list