How can I see the changes un-stashing will make to the current working tree? I would like to know what changes will be made before applying them!
See the most recent stash:
git stash show -p
See an arbitrary stash:
git stash show -p stash@{1}
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).
Answered 2023-09-21 08:10:29
stash@{0}
is the default; you only need an argument if you want to look at previous stashes. - anyone {0}
. - anyone -p
stand for? - anyone To see the most recent stash:
git stash show -p
To see an arbitrary stash:
git stash show -p stash@{1}
Also, I use git diff to compare the stash with any branch.
You can use:
git diff stash@{0} master
To see all changes compared to branch master.
Or You can use:
git diff --name-only stash@{0} master
To easy find only changed file names.
Answered 2023-09-21 08:10:29
git diff stash@{0} master
, you get a diff of your stash against the current master (which includes the work done on master after the stash was made), not the files/lines that the stash would change, which is what the question is about. - anyone git difftool --tool=... stash@{0} HEAD
- anyone git diff stash@{0}^ stash@{0}
- anyone git diff stash@{0} master -- filename
to get the changes to a specific file. - anyone If the branch that your stashed changes are based on has changed in the meantime, this command may be useful:
git diff stash@{0}^!
This compares the stash against the commit it is based on.
Answered 2023-09-21 08:10:29
~/.gitconfig
: laststash = diff stash@{0}^!
- anyone git difftool stash^!
for diff of last stash against commit it was based on, git difftool stash HEAD
for diff of last stash against current commit (stash@{n} for earlier stashes) - anyone git diff 'stash@{0}^!'
- anyone Depending on what you want to compare the stash with (local working tree / parent commit / head commit), there are actually several commands available, amongst which the good old git diff
, and the more specific git stash show
:
Compare stash with ↓ | git diff | git stash show |
---|---|---|
Local working tree | git diff stash@{0} |
git stash show -l |
Parent commit | git diff stash@{0}^ stash@{0} |
git stash show -p |
HEAD commit | git diff stash@{0} HEAD |
/ |
While git stash show
looks more user friendly on first sight, git diff
is actually more powerful in that it allows specifying filenames for a more focused diff. I've personally set up aliases for all of these commands in my zsh git plugin.
Answered 2023-09-21 08:10:29
error: switch
l' requires a value` when running git stash show -l
- anyone -l
is no longer a valid option in newer versions of git. - anyone git diff "stash@{0}"
) - anyone If your working tree is dirty, you can compare it to a stash by first committing the dirty working tree, and then comparing it to the stash. Afterwards, you may undo the commit with the dirty working tree (since you might not want to have that dirty commit in your commit log).
You can also use the following approach to compare two stashes with each other (in which case you just pop one of the stashes at first).
Commit your dirty working tree:
git add .
git commit -m "Dirty commit"
Diff the stash with that commit:
git diff HEAD stash@{0}
Then, afterwards, you may revert the commit, and put it back in the working dir:
git reset --soft HEAD~1
git reset .
Now you've diffed the dirty working tree with your stash, and are back to where you were initially.
Answered 2023-09-21 08:10:29
git stash show -l
. Does it diff the latest stash against the working (dirty) copy? How do you use it without getting error: switch l requires a value
? - anyone git stash show -l
. As to why it doesn't work for you, I can only guess you might be on an older version of git? I'm on git v2.20.1, and it works flawlessly without errors. - anyone @Magne's answer is the only one to (very late) date that answers the most flexible/useful interpretation of the question, but its a fair bit more complicated than necessary. Rather than committing and resetting, just stash your working copy, compare, then unstash.
git stash save "temp"
git diff stash@{0} stash@{1}
git stash pop
That shows you the differences between the top of the stash stack and your working folder by temporarily making your working folder changes become the top of the stash stack (stash@{0}), moving the original top down one (stash@{1}) then comparing using the original top in the 'new set' position so you see the changes that would result from applying it on top of your current work.
"But what if I don't have any current work?" Then you are in the normal boring case. Just use @Amber's answer
git stash show
or @czerasz's answer
git diff stash@{0}
or admit that stashing and unstashing is fast and easy anyway, just unstash the changes and inspect them. If you don't want them at the moment throw them (the current index/working folder changes) away. In full that's
git stash apply
git diff
git reset
git checkout
Answered 2023-09-21 08:10:29
git stash save -u
- anyone Just in case, to compare a file in the working tree and in the stash, use the below command
git diff stash@{0} -- fileName (with path)
Answered 2023-09-21 08:10:29
This works for me on git version 1.8.5.2:
git diff stash HEAD
Answered 2023-09-21 08:10:29
git stash apply
. - anyone If you have tools for diff (like beyond compare)
git difftool stash HEAD
Answered 2023-09-21 08:10:29
git stash apply
. - anyone HEAD
. I could modify @yerlilbilgin's answer to remove HEAD but I think anyone who uses git can figure that part out and me lengthening the answer would make it less readable. No blame on @yerlibilgin. - anyone I believe git diff <current-branchname>..stash@{0}
is the most intuitive way to compare the changes between the local working tree and the most recent stash. Replace stash@{0}
with the applicable stash number as needed.
Beware that git diff stash@{0}
may produce misleading results. If the two histories of your stash and current branch have diverged, the diff will look like you’re adding all the new stuff in your stash and removing everything unique to the current branch.
answer based on the git book
Also, note that double dot ..
and triple dot ...
specify different commit comparisons, and I am referring to the double dot for this answer. See the git book for details
Answered 2023-09-21 08:10:29
One way to do this without moving anything is to take advantage of the fact that patch
can read git diff's (unified diffs basically)
git stash show -p | patch -p1 --verbose --dry-run
This will show you a step-by-step preview of what patch would ordinarily do. The added benefit to this is that patch won't prevent itself from writing the patch to the working tree either, if for some reason you just really need git to shut up about commiting-before-modifying, go ahead and remove --dry-run and follow the verbose instructions.
Answered 2023-09-21 08:10:29
She the list of stash
git stash list
stash@{0}: WIP on feature/blabla: 830335224fa Name Commit
stash@{1}: WIP on feature/blabla2: 830335224fa Name Commit 2
So get the stash number and do:
You can do:
git stash show -p stash@{1}
But if you want a diff (this is different to show the stash, that's why I write this answer. Diff
consider the current code in your branch and show
just show what you will apply)
You can use:
git diff stash@{0}
or
git diff stash@{0} <branch name>
Another interesting thing to do is:
git stash apply
git stash apply stash@{10}
This applies the stash without removing it from the list, you can git checkout .
to remove those change or if you are happy git stash drop stash@{10}
to remove a stash from the list.
From here I never recommend to use git stash pop
and use a combination of git stash apply
and git stash drop
If you apply a stash in the wrong branch... well sometimes is difficult to recover your code.
Answered 2023-09-21 08:10:29
Combining what I learned in this thread and in this one, when I want to see "what is inside the stash", I first run:
git stash show stash@{0}
That will show what files were modified. Then, to get a nice visual diff in a difftool, I do:
git difftool --dir-diff stash@{0} stash@{0}^
This will display all the differences at once of the given stash against its parent.
You can configure the diff tool in ~/.gitconfig
, e.g. with Meld:
...
[diff]
tool = meld
Answered 2023-09-21 08:10:29
I found that none of the answers here gave me quite what I needed for my use case:
Made some changes, stashed them, wanted to check what had changed without applying the stash.
The best I found for a diff between the working tree and the stash was:
git diff stash@{0}
or simply:
git diff stash
But this showed the diff the wrong way round for how I expected it: Lines added (usually in green) were shown as removed (usually red), and vice versa.
Reversing the diff got me what I needed:
git diff stash -R
At this point you get identical results running this command after stashing as you would running git diff
before the stash.
Answered 2023-09-21 08:10:29
FWIW This may be a bit redundant to all the other answers and is very similar to the accepted answer which is spot on; but maybe it will help someone out.
git stash show --help
will give you all you should need; including stash show info.
show [<stash>]
Show the changes recorded in the stash as a diff between the stashed state and its original parent. When no is given, shows the latest one. 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). You can use stash.showStat and/or stash.showPatch config variables to change the default behavior.
Answered 2023-09-21 08:10:29