{"thread":{"id":"26179","subject":"Stashing subset of changed file plus a new file","startedAt":"2011-01-03T14:05:02Z","lastAt":"2011-01-03T14:05:02Z","messageCount":1,"participants":["Zivkov, Sasa"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"158846","messageId":"B221C4384AE5104EB414A877DFD0372C0BD779C260@DEWDFECCR04.wdf.sap.corp","threadId":"26179","inReplyTo":null,"subject":"Stashing subset of changed file plus a new file","fromName":"Zivkov, Sasa","fromEmail":"sasa.zivkov@sap.com","sentAt":"2011-01-03T14:05:02Z","receivedAt":"2011-01-03T14:05:02Z","isPatch":false,"sender":{"key":"sasa.zivkov@sap.com","avatar":null},"body":"Let's suppose I changed a file e.txt and introduced two changes to it: C1 and\nC2. Additionally, I have a new file n.txt in my working tree. My goal is to\nstash the change C1 plus the complete file n.txt while keeping the change C2 in\nthe working tree.\n\nFor Git experts who don't want to read the complete post: what is the best\n(most intuitive) way to achieve that?\n\n\nI don't know if I somehow missed a trivial and obvious solution but here is\nwhat I went through:\n\n1) First, I tried:\n        > git stash --patch\nand answered 'y' for C1 and 'n' for C2. For the e.txt this worked as expected\nbut git stash didn't ask me about the (changes of the) file n.txt. n.txt\nremained in the working tree.  OK, reading through this list I found out that\ngit stash never looks at the non-tracked files.\n\n2) Second trial:\n        > git add n.txt\n        > git stash --patch\nand again answered the same for C1 and C2 as in 1). However, this again didn't\nask me about the n.txt and n.txt remained in the working tree.\n\n3) Thinking how to make git stash --patch not ignore the n.txt. Obviously, it\nlooks only at the hunks produced by the diff between the working tree and the\nindex... let me add the n.txt to the index but not its content (thus producing\nthe desired diff):\n        > git add -N n.txt\n        > git stash --patch\n        n.txt: not added yet\n        fatal: git-write-tree: error building trees\n        Cannot save the current index state\n\n4) Similar like 3) trying to make diff between the index and working tree for\nn.txt:\n        > git add n.txt\n        > rm n.txt\n        > git stash --patch\nthis went through but again didn't ask me for n.txt and n.txt wasn't part of\nthe stash commit:\n        > git stash show\n        ... no n.txt ...\n\n\n5) Giving up git stash --patch, using git add --patch:\n        > git add --patch\n        ... skipped C1, added C2 ...\n        > git add n.txt\n        > git stash --keep-index\nfinally, produced the stash that includes the creation of the n.txt! However,\nthe n.txt is still in both the working tree and the index and it has to be\nremoved:\n        > git rm -f n.txt\nusing the -f option in order to remove it both from the index and the working\ntree.\n\nThe 5) is a solution but it has its drawbacks:\n- one has to remove the new files after git stash\n- when using git add --patch one has to select hunks that have to stay in the\n  working tree which is exactly opposite as when using git stash --patch where\n  one has to select the hunks to be stashed.\n- unintuitive:\n        new files are added to the index in order to be stashed\n        hunks of diff of an existing file are added to the index in order to stay\n        in the working tree\n\nQuestions:\nIs there any solution better than 5) ?\nIs git stash --patch at all able to stash new files?\n\n\nSasa Zivkov\n"}]}