threads / discuss / 45396

Viewing untracked+stashed files in git stash show

Subject: Viewing untracked+stashed files in git stash show

## tl;dr

6 messages between Mar 16, 2017 and Mar 17, 2017.

replies: 5people: 3as markdown or json

Okash Khawaja· Mar 16, 2017, 16:34 UTC · lore
Hi,

If you have some untracked files and your run `git stash -u`. Then `git stash show` doesn't show the untracked files. Is there a flag that can be passed to git stash show to report untracked files?

Thanks, Okash

stefan.naewe@atlas-elektronik.com· Mar 17, 2017, 08:20 UTC · re: Okash Khawaja · lore

Re: Viewing untracked+stashed files in git stash show

Am 16.03.2017 um 17:34 schrieb Okash Khawaja:
Show 5 quoted lines
> Hi,
> 
> If you have some untracked files and your run `git stash -u`. Then
> `git stash show` doesn't show the untracked files. Is there a flag
> that can be passed to git stash show to report untracked files?
Not for 'git stash' but you can use 'git show stash@{0}^3
(Or, of course, 'gitk stash@{0}' )
HTH
Stefan
-- 
----------------------------------------------------------------
/dev/random says: I keep my .BAT files in D:\BELFRY
python -c "print '73746566616e2e6e616577654061746c61732d656c656b74726f6e696b2e636f6d'.decode('hex')" 
GPG Key fingerprint = 2DF5 E01B 09C3 7501 BCA9  9666 829B 49C5 9221 27AF
Okash Khawaja· Mar 17, 2017, 09:50 UTC · re: stefan.naewe@atlas-elektronik.com · lore

Re: Viewing untracked+stashed files in git stash show

Hi,
On Fri, Mar 17, 2017 at 8:20 AM,  <stefan.naewe@atlas-elektronik.com> wrote:
Show 8 quoted lines
> Am 16.03.2017 um 17:34 schrieb Okash Khawaja:
>> Hi,
>>
>> If you have some untracked files and your run `git stash -u`. Then
>> `git stash show` doesn't show the untracked files. Is there a flag
>> that can be passed to git stash show to report untracked files?
>
> Not for 'git stash' but you can use 'git show stash@{0}^3

Okay that's fine as long as you know. But the first thing that comes to your mind is that you've lost those untracked files. Is there a reason why git stash show doesn't show those files?

Okash Khawaja· Mar 17, 2017, 12:08 UTC · re: Okash Khawaja · lore

Re: Viewing untracked+stashed files in git stash show

Hi,
On Fri, Mar 17, 2017 at 9:50 AM, Okash Khawaja <okash.khawaja@gmail.com> wrote:
Show 15 quoted lines
> Hi,
>
> On Fri, Mar 17, 2017 at 8:20 AM,  <stefan.naewe@atlas-elektronik.com> wrote:
>> Am 16.03.2017 um 17:34 schrieb Okash Khawaja:
>>> Hi,
>>>
>>> If you have some untracked files and your run `git stash -u`. Then
>>> `git stash show` doesn't show the untracked files. Is there a flag
>>> that can be passed to git stash show to report untracked files?
>>
>> Not for 'git stash' but you can use 'git show stash@{0}^3
>
> Okay that's fine as long as you know. But the first thing that comes
> to your mind is that you've lost those untracked files. Is there a
> reason why git stash show doesn't show those files?
Here's some more interesting behaviour when stashing untracked files.

Add new files (untracked) + modify some tracked files -> `git stash -u` -> modify the stashed files in a way that `git stash pop` will fail -> `git stash pop` -> it fails so now you would expect none of the stashed files restored but instead the untracked stashed files reappear.

This combined with the fact that `git stash show` doesn't show untracked files suggest that untracked files go into an undefined (or at least undocumented) state when it comes to git stash. Is that something that needs to be looked at?

Jeff King· Mar 17, 2017, 13:51 UTC · re: Okash Khawaja · lore

Re: Viewing untracked+stashed files in git stash show

On Fri, Mar 17, 2017 at 09:50:29AM +0000, Okash Khawaja wrote:
Show 15 quoted lines
> Hi,
> 
> On Fri, Mar 17, 2017 at 8:20 AM,  <stefan.naewe@atlas-elektronik.com> wrote:
> > Am 16.03.2017 um 17:34 schrieb Okash Khawaja:
> >> Hi,
> >>
> >> If you have some untracked files and your run `git stash -u`. Then
> >> `git stash show` doesn't show the untracked files. Is there a flag
> >> that can be passed to git stash show to report untracked files?
> >
> > Not for 'git stash' but you can use 'git show stash@{0}^3
> 
> Okay that's fine as long as you know. But the first thing that comes
> to your mind is that you've lost those untracked files. Is there a
> reason why git stash show doesn't show those files?

No, I don't think there is a good reason. The "untracked" option was added to git-stash much later, and nobody considered how it interacted with "stash show".

Changes from the index have the same problem. Probably "stash show" should have options "--index" and "--untracked" to show:

  # index
  git diff $stash^1 $stash^2
  # untracked
  git diff $stash^1 $stash^3
Possibly it should even show those by default when they are non-empty.
-Peff
Jeff King· Mar 17, 2017, 14:14 UTC · re: Jeff King · lore

Re: Viewing untracked+stashed files in git stash show

On Fri, Mar 17, 2017 at 09:51:44AM -0400, Jeff King wrote:
Show 18 quoted lines
> > Okay that's fine as long as you know. But the first thing that comes
> > to your mind is that you've lost those untracked files. Is there a
> > reason why git stash show doesn't show those files?
> 
> No, I don't think there is a good reason. The "untracked" option was
> added to git-stash much later, and nobody considered how it interacted
> with "stash show".
> 
> Changes from the index have the same problem. Probably "stash show"
> should have options "--index" and "--untracked" to show:
> 
>   # index
>   git diff $stash^1 $stash^2
> 
>   # untracked
>   git diff $stash^1 $stash^3
> 
> Possibly it should even show those by default when they are non-empty.

Actually, $stash^1 is already the parent of the other commits, so we can just show them as-is. Doing this:

diff --git a/git-stash.sh b/git-stash.sh
index 9c70662cc..868d9027b 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -388,7 +388,16 @@ show_stash () {
 		fi
 	fi
 
-	git diff ${FLAGS} $b_commit $w_commit
+	{
+		# use --pretty=format instead of tformat, because it omits the
+		# blank line between the subject and the diff; use "."
+		# to enable history simplification and drop empty commits.
+		git show --first-parent -m --pretty=format:'==> %s <==' \
+			${FLAGS} $w_commit -- .
+		git show --pretty=format:'%n==> %s <==' ${FLAGS} $i_commit -- .
+		test -n "$u_commit" &&
+			git show --pretty=format:'%n==> %s <==' ${FLAGS} $u_commit -- .
+	} | git_pager
 }
 
 show_help () {
actually looks OK to me, though I don't know if people really like the
existing format. With a setup like:

  git init
  echo base >file
  git add file
  git commit -m base
  echo index >file
  git add file
  echo working tree >file
  echo untracked >other
  git stash -u

it looks like:

  $ git stash show
  ==> WIP on master: 8aa1ec9 base <==
   file | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
  
  ==> index on master: 8aa1ec9 base <==
   file | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)
  
  ==> untracked files on master: 8aa1ec9 base <==
   other | 1 +
   1 file changed, 1 insertion(+)

or:

  $ git stash show -p
  ==> WIP on master: 8aa1ec9 base <==
  diff --git a/file b/file
  index df967b9..ace4bb6 100644
  --- a/file
  +++ b/file
  @@ -1 +1 @@
  -base
  +working tree
  
  ==> index on master: 8aa1ec9 base <==
  diff --git a/file b/file
  index df967b9..9015a7a 100644
  --- a/file
  +++ b/file
  @@ -1 +1 @@
  -base
  +index
  
  ==> untracked files on master: 8aa1ec9 base <==
  diff --git a/other b/other
  new file mode 100644
  index 0000000..5a72eb2
  --- /dev/null
  +++ b/other
  @@ -0,0 +1 @@
  +untracked

And if you have only a single change, it omits the others:

  $ echo working tree >file
  $ git stash
  $ git stash show
  ==> WIP on master: 8aa1ec9 base <==
   file | 2 +-
   1 file changed, 1 insertion(+), 1 deletion(-)


If I were designing "stash show" from scratch today, I'd probably do
something like that. But like I said, I don't know if people are
invested in the current output (which _can_ be piped to "git apply" if
need be, though my output could, too, if you do not have index or
untracked files).

-Peff

← back to recent threads