Thoughts about React,Web Development,Performance

Published on August 3, 2022

Git Stash Tips

Show Stash Diff

To see the difference between the current code and some stash's entry, you can run the following:

git stash show -p

You can specify a stash index too:

// git stash list
stash@{0}: First entry
stash@{1}: Second entry
stash@{2}: Third entry
stash@{3}: Fourth entry
stash@{4}: Fifth entry

git stash show -p 1 // Shows diff for stash@{1} // Second entry

git stash push

You can run this command to save code into the stash with a specific message so it's easier to differentiate multiple stash entries later (and even apply a specific one by using git stash apply):

git stash push -m "Custom Message"

Note: git stash save "Custom Message" is deprecated, so I'd suggest to stick with git stash push.

git stash apply

Let's suppose that you have the following entries in the stash:

// git stash list
stash@{0}: First entry
stash@{1}: Second entry
stash@{2}: Third entry
stash@{3}: Fourth entry
stash@{4}: Fifth entry

You can run git stash apply with the index of the stash list as a parameter (starting with 0) to apply the entry "Third entry" from the stash:

git stash apply 2

This means that git stash pop is equivalent to git stash apply 0.

Apply a Stash Without Removing it From the List

If you may want to apply some changes but keep them in the stash, you can run the following:

git stash apply

According to git docs:

Like pop, but do not remove the state from the stash list

Disable Pagination for git stash list

There's an issue that prevents to add a global configuration to disable pagination for git stash list, so you can only do this on a per command basis:

git --no-pager stash list

This maybe be a bit cumbersome to repeat, so you can always create an alias for git stash list as I did:

alias gstl='git --no-pager stash list'

Now you can run gstl to list your stash entries.

Stash a Specific File

If you want to stash a specific file(s), you can append them on the git stash -m message syntax:

git stash push -m "Custom Message" index.js package.json yarn.lock