Git Stash Git

Jan 22nd, 2021 - written by Kimserey with .

The stash is the place where we can save work in progress from working tree and index while reverting to the HEAD commit, a command useful to pull latest on the checkout branch or simply inspect other branches. In this post we will look at how to use git stash.

What’s The Stash

The stash is a place where we can save work in progress and revert our working tree to HEAD. We can see the stash as being a bag where we gather all our changes including those in index, and put them inside the bag in order to get them back later.

The stash acts as a stack, meaning that changes stashed are pushed onto the stack and changes taken out of the stack are popped.

Using git stash

We can stash changes with stash, equivalent to stash push:

1
❯ git stash

This will create a new entry on our stash which we can list with

1
2
3
4
❯ git stash list

stash@{0}: WIP on my-branch-2: dc84cfa Commit message
stash@{1}: WIP on my-branch-2: dc84cfa Commit message

We can see that we have two stash here. By default the name of the stash indicates WIP on [branch]: [last commit on the branch] [last commit message on the branch].

If we want to modify the message, we can use save so that we can add a meaning to our stash if we want to easily find it:

1
❯ git stash save "Comments on stash"

This will then save the message:

1
2
3
4
5
❯ git stash list

stash@{0}: On my-branch-2: Comments on stash
stash@{1}: WIP on my-branch-2: dc84cfa Commit message
stash@{2}: WIP on my-branch-2: dc84cfa Commit message

We can check the content of each stash with show:

1
2
3
4
❯ git stash show
❯ git stash show -p
❯ git stash show 1
❯ git stash show -p 1

show will show the diffstat, if we want to see the actual changes we can use show -p. By default, the top of the stash will be shown (0), if we want to see a specific stash, we can specify the index at the end.

The stash behaving like a stack, we pushed the stash and we can pop it. Popping the stash will take the stash out and apply it.

1
2
❯ git stash pop
❯ git stash pop 2

Without any index, it will take the top of the stash and apply it. If we want to pop a specific index we can specify it at the end. It’s also possible to only apply, without popping out the stash with apply:

1
2
❯ git stash apply
❯ git stash apply 1

Similarly the index can be provided to target a specific stash to apply.

If we want to get rid of all our stash, we can use clear:

1
❯ git stash clear

This will delete all our stash without applying them. If we want to delete a single stash, we can use drop:

1
2
❯ git stash drop
❯ git stash drop 1

And similarly as all commands targeting a single stash, it’s possible to provide an index. Keep in mind that the stash is a stack, hence the indexes are not permanently attached to each stash. For example if we have [0, 1, 2] and drop 1, the stash state will be [0, 1] where stash@{2} became stash@{1}.

And that concludes today’s post! Hope you liked this post and I see you on the next one!

External Sources

Designed, built and maintained by Kimserey Lam.