Stashing subset of changed file plus a new file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Let's suppose I changed a file e.txt and introduced two changes to it: C1 and
C2. Additionally, I have a new file n.txt in my working tree. My goal is to
stash the change C1 plus the complete file n.txt while keeping the change C2 in
the working tree.

For Git experts who don't want to read the complete post: what is the best
(most intuitive) way to achieve that?


I don't know if I somehow missed a trivial and obvious solution but here is
what I went through:

1) First, I tried:
        > git stash --patch
and answered 'y' for C1 and 'n' for C2. For the e.txt this worked as expected
but git stash didn't ask me about the (changes of the) file n.txt. n.txt
remained in the working tree.  OK, reading through this list I found out that
git stash never looks at the non-tracked files.

2) Second trial:
        > git add n.txt
        > git stash --patch
and again answered the same for C1 and C2 as in 1). However, this again didn't
ask me about the n.txt and n.txt remained in the working tree.

3) Thinking how to make git stash --patch not ignore the n.txt. Obviously, it
looks only at the hunks produced by the diff between the working tree and the
index... let me add the n.txt to the index but not its content (thus producing
the desired diff):
        > git add -N n.txt
        > git stash --patch
        n.txt: not added yet
        fatal: git-write-tree: error building trees
        Cannot save the current index state

4) Similar like 3) trying to make diff between the index and working tree for
n.txt:
        > git add n.txt
        > rm n.txt
        > git stash --patch
this went through but again didn't ask me for n.txt and n.txt wasn't part of
the stash commit:
        > git stash show
        ... no n.txt ...


5) Giving up git stash --patch, using git add --patch:
        > git add --patch
        ... skipped C1, added C2 ...
        > git add n.txt
        > git stash --keep-index
finally, produced the stash that includes the creation of the n.txt! However,
the n.txt is still in both the working tree and the index and it has to be
removed:
        > git rm -f n.txt
using the -f option in order to remove it both from the index and the working
tree.

The 5) is a solution but it has its drawbacks:
- one has to remove the new files after git stash
- when using git add --patch one has to select hunks that have to stay in the
  working tree which is exactly opposite as when using git stash --patch where
  one has to select the hunks to be stashed.
- unintuitive:
        new files are added to the index in order to be stashed
        hunks of diff of an existing file are added to the index in order to stay
        in the working tree

Questions:
Is there any solution better than 5) ?
Is git stash --patch at all able to stash new files?


Sasa Zivkov
--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html


[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]