{"thread":{"id":"39010","subject":"Why can't I stash submodule changes?","startedAt":"2015-04-06T02:15:26Z","lastAt":"2015-04-07T05:19:02Z","messageCount":4,"participants":["Shane da Silva","Phillip Susi","Jens Lehmann"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"259043","messageId":"CAAKwDd8SVA=ynzH_U1M3bb+mLZMnF79-imBLrU+x9qcbXfWO3g@mail.gmail.com","threadId":"39010","inReplyTo":null,"subject":"Why can't I stash submodule changes?","fromName":"Shane da Silva","fromEmail":"shane@dasilva.io","sentAt":"2015-04-06T02:15:26Z","receivedAt":"2015-04-06T02:15:26Z","isPatch":false,"sender":{"key":"shane@dasilva.io","avatar":null},"body":"I’m having trouble understanding why I cannot stash changes to a submodule.\n\nWhen adding a submodule to a repository (`git submodule add\n./sub-repo`), I can then run `git stash` and `git stash pop` with\nexpected results—the submodule disappears and reappears in the working\ntree.\n\nHowever, when I try stashing an update to a submodule, `git stash`\nreports “No local changes to save”. The following shell script\nillustrates this behavior:\n\n\n# Create repo\nmkdir test-repo\ncd test-repo\ngit init\ngit commit --allow-empty -m \"Initial commit\"\n\n# Create submodule\nmkdir sub-repo\ncd sub-repo\ngit init\ngit commit --allow-empty -m \"Initial commit\"\ncd -\n\n# Add submodule\ngit submodule add ./sub-repo\ngit commit -m \"Add submodule\"\n\n# Modify submodule\ncd sub-repo\ntouch foo\ngit add foo\ngit commit -m \"Submodule changed\"\ncd -\n\n# Stash submodule change\ngit stash # <---------------------------Displays \"No local changes to save”\n\n\nI’m trying to wrap my head around why this is the current behavior, as\nI suspect this is intentional but it seems unexpected. If anyone can\nshed any light on this, I would really appreciate it!\n\nThanks,\n\nShane\n"},{"id":"259089","messageId":"5522A2EC.9050100@ubuntu.com","threadId":"39010","inReplyTo":"CAAKwDd8SVA=ynzH_U1M3bb+mLZMnF79-imBLrU+x9qcbXfWO3g@mail.gmail.com","subject":"Re: Why can't I stash submodule changes?","fromName":"Phillip Susi","fromEmail":"psusi@ubuntu.com","sentAt":"2015-04-06T15:14:52Z","receivedAt":"2015-04-06T15:14:52Z","isPatch":false,"sender":{"key":"psusi@ubuntu.com","avatar":null},"body":"-----BEGIN PGP SIGNED MESSAGE-----\nHash: SHA1\n\nOn 4/5/2015 10:15 PM, Shane da Silva wrote:\n> I’m trying to wrap my head around why this is the current behavior,\n> as I suspect this is intentional but it seems unexpected. If anyone\n> can shed any light on this, I would really appreciate it!\n\nWhy would you expect anything else?  You have no local changes ( since\nyou committed them already ).  In other words, git status shows no\nuncommitted changes, so there is nothing to stash.\n\n\n-----BEGIN PGP SIGNATURE-----\nVersion: GnuPG v2.0.17 (MingW32)\n\niQEcBAEBAgAGBQJVIqLsAAoJENRVrw2cjl5RFGsIAIC38/iZTQsYWfeS8mOt3DVY\njrRCrbfHcjQyKWsEk2seupEV1K1OO0lPhocRE4+3T+vAz3n9Wdc+ATuXNv41vmkY\nr2R3VaTXimLw6NfaSxMfqEb4xL/9M0UhUS7SdEALVEApS4AySxYKWKL+RoqF0LWD\nJgP6DHCzOLBy8cttaQppZdfRHa34FUmeH1k7m6r/14tarwcc+a3glVqW7i3gue7z\ns3zhEkd+dqgab79TNj1gh86UE016UmG7yjbBTWKnNrYdTCW5IBCDqsp0We2PH1Jy\n2QPckedCisroZjq0I4uAbuUCm94obiEJKclbY+Wl4sVdYb9rralBOJnPkKwRsiw=\n=4ekO\n-----END PGP SIGNATURE-----\n"},{"id":"259090","messageId":"5522C92B.6090700@web.de","threadId":"39010","inReplyTo":"CAAKwDd8SVA=ynzH_U1M3bb+mLZMnF79-imBLrU+x9qcbXfWO3g@mail.gmail.com","subject":"Re: Why can't I stash submodule changes?","fromName":"Jens Lehmann","fromEmail":"jens.lehmann@web.de","sentAt":"2015-04-06T17:58:03Z","receivedAt":"2015-04-06T17:58:03Z","isPatch":false,"sender":{"key":"jens.lehmann@web.de","avatar":"https://avatars.githubusercontent.com/u/135220?v=4"},"body":"Am 06.04.2015 um 04:15 schrieb Shane da Silva:\n> I’m having trouble understanding why I cannot stash changes to a submodule.\n>\n> When adding a submodule to a repository (`git submodule add\n> ./sub-repo`), I can then run `git stash` and `git stash pop` with\n> expected results—the submodule disappears and reappears in the working\n> tree.\n\nJust to be sure: Only the index of the superproject and the .gitmodules\nfile are updated by stash to either contain the submodule or not. But\nthe subdirectory \"sub-repo\" stays unchanged and won't be removed or\nreappear, right?\n\n> However, when I try stashing an update to a submodule, `git stash`\n> reports “No local changes to save”. The following shell script\n> illustrates this behavior:\n>\n>\n> # Create repo\n> mkdir test-repo\n> cd test-repo\n> git init\n> git commit --allow-empty -m \"Initial commit\"\n>\n> # Create submodule\n> mkdir sub-repo\n> cd sub-repo\n> git init\n> git commit --allow-empty -m \"Initial commit\"\n> cd -\n>\n> # Add submodule\n> git submodule add ./sub-repo\n> git commit -m \"Add submodule\"\n>\n> # Modify submodule\n> cd sub-repo\n> touch foo\n> git add foo\n> git commit -m \"Submodule changed\"\n> cd -\n>\n> # Stash submodule change\n> git stash # <---------------------------Displays \"No local changes to save”\n\nThanks for providing a recipe to reproduce this!\n\n> I’m trying to wrap my head around why this is the current behavior, as\n> I suspect this is intentional but it seems unexpected. If anyone can\n> shed any light on this, I would really appreciate it!\n\nThe current behavior of git is that submodule contents aren't updated\nwhen the superproject changes. Running \"git submodule update\" later\nwill then update their content to most submodule changes (but e.g. it\nwon't remove a deleted submodule from the work tree). So yes, this is\nexpected until recursive submodule update materializes (and even then\nI'm not sure how to handle untracked but not ignored files inside a\nsubmodule when stashing will result in the submodule directory to be\nremoved).\n"},{"id":"259115","messageId":"CAAKwDd8oNdCWew7AfTg9+7z7gox3n2PYf01DUcCNbdGAWPBjqw@mail.gmail.com","threadId":"39010","inReplyTo":"5522C92B.6090700@web.de","subject":"Re: Why can't I stash submodule changes?","fromName":"Shane da Silva","fromEmail":"shane@dasilva.io","sentAt":"2015-04-07T05:19:02Z","receivedAt":"2015-04-07T05:19:02Z","isPatch":false,"sender":{"key":"shane@dasilva.io","avatar":null},"body":"Thanks for your responses, all.\n\nJens: yes, only the index is updated by the stash. The subdirectory\nremains and won't be removed (you will actually receive a warning\nabout `git` not being able to remove it).\n\nI think the core of my misunderstanding is I was used to the idea that\nwhen `git status` showed modifications to tracked files, `git stash`\nwould make those modifications disappear. Submodules are fundamentally\ndifferent, and it makes sense to me now why it wouldn't necessarily be\na good idea for `git stash` to be a mutating operation on submodules.\n\nI ran into this behavior while debugging an issue with a git-hook\nmanager I maintain, called Overcommit\n(https://github.com/brigade/overcommit). It was discovered that if you\nonly add submodule changes to the index and then run `git stash save\n--keep-index`, an exit status of zero is returned even though no stash\nwas created. I had assumed that if the return code was zero, a stash\ncommit was created, but it makes sense why this is not the case.\n\nThanks again for your time in helping clarify this behavior.\n\nOn Mon, Apr 6, 2015 at 10:58 AM, Jens Lehmann <Jens.Lehmann@web.de> wrote:\n> Am 06.04.2015 um 04:15 schrieb Shane da Silva:\n>>\n>> I’m having trouble understanding why I cannot stash changes to a\n>> submodule.\n>>\n>> When adding a submodule to a repository (`git submodule add\n>> ./sub-repo`), I can then run `git stash` and `git stash pop` with\n>> expected results—the submodule disappears and reappears in the working\n>> tree.\n>\n>\n> Just to be sure: Only the index of the superproject and the .gitmodules\n> file are updated by stash to either contain the submodule or not. But\n> the subdirectory \"sub-repo\" stays unchanged and won't be removed or\n> reappear, right?\n>\n>\n>> However, when I try stashing an update to a submodule, `git stash`\n>> reports “No local changes to save”. The following shell script\n>> illustrates this behavior:\n>>\n>>\n>> # Create repo\n>> mkdir test-repo\n>> cd test-repo\n>> git init\n>> git commit --allow-empty -m \"Initial commit\"\n>>\n>> # Create submodule\n>> mkdir sub-repo\n>> cd sub-repo\n>> git init\n>> git commit --allow-empty -m \"Initial commit\"\n>> cd -\n>>\n>> # Add submodule\n>> git submodule add ./sub-repo\n>> git commit -m \"Add submodule\"\n>>\n>> # Modify submodule\n>> cd sub-repo\n>> touch foo\n>> git add foo\n>> git commit -m \"Submodule changed\"\n>> cd -\n>>\n>> # Stash submodule change\n>> git stash # <---------------------------Displays \"No local changes to\n>> save”\n>\n>\n> Thanks for providing a recipe to reproduce this!\n>\n>> I’m trying to wrap my head around why this is the current behavior, as\n>> I suspect this is intentional but it seems unexpected. If anyone can\n>> shed any light on this, I would really appreciate it!\n>\n>\n> The current behavior of git is that submodule contents aren't updated\n> when the superproject changes. Running \"git submodule update\" later\n> will then update their content to most submodule changes (but e.g. it\n> won't remove a deleted submodule from the work tree). So yes, this is\n> expected until recursive submodule update materializes (and even then\n> I'm not sure how to handle untracked but not ignored files inside a\n> submodule when stashing will result in the submodule directory to be\n> removed).\n"}]}