Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you tell if a git stash is no longer required?

Tags:

git

git-stash

Is it possible to tell whether a stash has already been applied, and therefore is no longer required, without doing git stash apply? Assume that I'm only using one branch.

This could be prevented by using pop rather than apply when applying a stash, and therefore get rid of the stash each time it gets applied. However, I sometimes use git stash to keep a snapshot of work in progress, rather than only using it to switch from one task to another. Using pop would defeat that somewhat.

like image 859
Andrew Grimm Avatar asked Nov 01 '11 05:11

Andrew Grimm


People also ask

How do I check my git stash status?

Git Stash List. The Git stash list command will pull up a list of your repository's stashes. Git will display all of your stashes and a corresponding stash index. Now, if you wish to view the contents of a specific stash, you can run the Git stash show command followed by stash@ and the desired index.

How do I know if my git stash is empty?

You can git diff and inspect the output. If there is none, then there is nothing to stash . If there is output, then you can grep to parse it for specific things you need.

Does git stash expire?

Git stashes are saved until your hard disk dies (unlike commits, which are usually transmitted to some other computer via git push so will outlive a hard disk failure). You can have as many stashes as you want.

How do I see changes after git stash?

Retrieving stashed changes You can reapply stashed changes with the commands git stash apply and git stash pop . Both commands reapply the changes stashed in the latest stash (that is, stash@{0} ). A stash reapplies the changes while pop removes the changes from the stash and reapplies them to the working copy.


2 Answers

You can use the following shell script to get git stash list prefixed with checkmarks if they have already been applied or there is no need to apply them as there is no difference.

git stash list | while read line; do \
  ref=${line%%:*}; \
  prefix=$(test $(git diff $ref | wc -l) = "0" && echo "✔  " || echo "   "); \
  echo "$prefix$line"; \
done

This will give you a list like:

✔  stash@{0}: WIP on develop: 77a1a66 send 'social.share' message via 'view-req-relay'...
   stash@{1}: WIP on bigcouch: 4bfa3af added couchdb filters...

And if you like it you can add it as a git alias like that:

git config --global --add alias.stash-list '!git stash list | while read line; do   ref=${line%%:*};   prefix=$(test $(git diff $ref | wc -l) = "0" && echo "✔  " || echo "   ");   echo "$prefix$line"; done'
git stash-list

(tested with bash and zsh)

like image 33
muhqu Avatar answered Sep 18 '22 13:09

muhqu


Just make a diff and you will see.

git diff HEAD stash@{0}

like image 155
the.malkolm Avatar answered Sep 18 '22 13:09

the.malkolm