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

Re: git stash doesn't always save work dir as-is: bug?

From
DLDan Lenski <dlenski@gmail.com>
Date
Aug 28, 2014, 02:54 UTC
Message-ID
<loom.20140828T044432-503@post.gmane.org>
In-Reply-To
<201309080040.r880ePlr094459@elf.torek.net>
Chris Torek <chris.torek <at> gmail.com> writes:
Show 5 quoted lines
> 
> When "git stash" does its work, if the index and the work
> directory are out of sync, but the work directory is in sync with
> the HEAD commit, the work directory commit does not contain the
> file in its work-directory state, but rather in its index-state.

I too encountered this bug while trying to design a custom pre-commit hook that would speculatively change the working tree and then "stash pop" its changes. Chris Torek alerted me to it on StackOverflow: http://stackoverflow.com/questions/25536034/modifying-working-directory-and- staging-area-temporarily-in-git-pre-commit-hook

Another small issue I had in solving the same problem: there's no way to tell if "git stash save" has created a new stash or not, so it's hard to figure out whether one should be popped later.

It would be helpful if there were a specific non-zero return code assigned when no stash was generated due to no changes requiring it. Here's a simple patch for that:

--- a/git-stash
+++ b/git-stash
@@ -220,8 +220,7 @@ save_stash () {
        git update-index -q --refresh
        if no_changes
        then
-               say "$(gettext "No local changes to save")"
-               exit 0
+               die_with_status 9 "$(gettext "No local changes to save")"
        fi
        test -f "$GIT_DIR/logs/$ref_stash" ||
                clear_stash || die "$(gettext "Cannot initialize stash")"

Thanks,
Dan
Previous: Chris Torek
Message 2 of 2 in “git stash doesn't always save work dir as-is: bug?”
  1. Chris TorekSep 8, 2013
  2. Dan LenskiAug 28, 2014

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.