git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[BUG] "git stash -- path" reports wrong unstaged changes

From
Jeff King <peff@peff.net>
Date
Mar 17, 2017, 14:50 UTC
Message-ID
<20170317145039.dmcb3qyqbzfvtmgz@sigill.intra.peff.net>

I used "git stash -- path" for the first time today and happened to notice an oddity. If I run:

	git init -q repo
	cd repo
	
	for i in one two; do
		echo content >$i
		git add $i
	done
	git commit -qm base
	
	for i in one two; do
		echo change >$i
	done
	git stash -- one
it says:
  Saved working directory and index state WIP on master: 20cfadf base
  Unstaged changes after reset:
  M	one
  M	two
Even though "one" no longer has unstaged changes.
If I run with GIT_TRACE=1, that message is generated by:
  git reset -- one

which makes sense. At that stage we've just reset the index, but the working tree file still has modifications. In the non-pathspec case we run "git reset --hard", which takes care of the index and the working tree.

It's really "checkout-index" that cleans the working tree, but it doesn't have porcelain finery like an "Unstaged changes" message. I think the patch below would fix it, but I wonder if we can do it in a way that doesn't involve calling diff-files twice.

-Peff
---
diff --git a/git-stash.sh b/git-stash.sh
index 9c70662cc..9a4bb503a 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -299,10 +299,15 @@ push_stash () {
 	then
 		if test $# != 0
 		then
-			git reset ${GIT_QUIET:+-q} -- "$@"
+			git reset -q -- "$@"
 			git ls-files -z --modified -- "$@" |
 			git checkout-index -z --force --stdin
 			git clean --force ${GIT_QUIET:+-q} -d -- "$@"
+			if test -z "$GIT_QUIET" && ! git diff-files --quiet
+			then
+				say "$(gettext "Unstaged changes after reset:")"
+				git diff-files --name-status
+			fi
 		else
 			git reset --hard ${GIT_QUIET:+-q}
 		fi
Next: Thomas Gummerer
Message 1 of 26 in “[BUG] "git stash -- path" reports wrong unstaged changes”
  1. Jeff KingMar 17, 2017
  2. Thomas GummererMar 18, 2017
  3. Thomas GummererMar 19, 2017
  4. 1/3 stash: show less information for stash push -- <pathspec>Thomas Gummerer, Mar 19, 2017
  5. Junio C HamanoMar 20, 2017
  6. Jeff KingMar 20, 2017
  7. Junio C HamanoMar 20, 2017
  8. Thomas GummererMar 21, 2017
  9. Jeff KingMar 20, 2017
  10. 3/3 stash: pass the pathspec argument to git resetThomas Gummerer, Mar 19, 2017
  11. Jeff KingMar 20, 2017
  12. Thomas GummererMar 21, 2017
  13. Jeff KingMar 21, 2017
  14. 2/3 stash: make push -p -q --no-keep-index quietThomas Gummerer, Mar 19, 2017
  15. Jeff KingMar 20, 2017
  16. 0/3 Re: [BUG] "git stash -- path" reports wrong unstaged changesThomas Gummerer, Mar 21, 2017
  17. 2/3 stash: pass the pathspec argument to git resetThomas Gummerer, Mar 21, 2017
  18. Jeff KingMar 21, 2017
  19. 1/3 stash: don't show internal implementation detailsThomas Gummerer, Mar 21, 2017
  20. Jeff KingMar 21, 2017
  21. Thomas GummererMar 22, 2017
  22. Jeff KingMar 22, 2017
  23. 3/3 stash: keep untracked files intact in stash -kThomas Gummerer, Mar 21, 2017
  24. Jeff KingMar 21, 2017
  25. Thomas GummererMar 22, 2017
  26. Jeff KingMar 20, 2017

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.