Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git stash: How to see if there are stashed changes in a branch

Tags:

git

git-stash

Let's say i'm in a branch off of master, called "my_new_stuff". I have a feeling i might have stashed something in there. I'm worried that if I do git stash pop and i didn't stash anything it's going to shove a load of unwanted crap into my working folder.

Can i see if there are stashed changes without unstashing them?

thanks, max

like image 856
Max Williams Avatar asked Jun 17 '13 16:06

Max Williams


People also ask

How do you check if there stashed changes in git?

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 list stashed changes?

git stash show -p stash@{0} --name-only shows just the names of the files (not the contents) in your first stash. @mrgloom If you want to see the stashed changes for a single file, then something like git diff stash@{0}^! -- file. txt will do it.

How do I see diff in stash?

From the git stash manpages: By default, the command shows the diffstat, but it will accept any format known to git diff (e.g., git stash show -p stash@{1} to view the second most recent stash in patch form). stash@{0} is the default; you only need an argument if you want to look at previous stashes.


2 Answers

The stash stores snapshots in the same way that commits do. You can see the contents of the stash with

git stash list

You can reference those snapshots with the stash@{N} notation or use the hashes shown. You can use any of Git's commands that work on commits on stashes. For example

git diff master stash@{0}

will show you what the most recent stash would add/remove to the master branch if you applied it there.

like image 187
Peter Lundgren Avatar answered Sep 28 '22 23:09

Peter Lundgren


Not quite an answer as such, but a little script I made using Peter Lundgren's answer, above, which i find very useful: when I switch branches it tells me if I have stashed changes.

in .git/hooks/post-checkout

#!/bin/sh
branch=$(git rev-parse --abbrev-ref HEAD)
stashes=`git stash list | grep "WIP on $branch"`
if [ "$stashes" ]
then
  echo "You have the following stashes for this branch:"
  echo $stashes
fi
like image 29
Max Williams Avatar answered Sep 28 '22 22:09

Max Williams