I totally love git add -p
and git stash
but I occasionally have the following problem, which is reproduced by the following sequence of commands:
I asked the question in the git mailing list. What I describe is the expected behaviour. It's not a bug. :-(
Here is the answer I got:
If you did not edit the hunk manually, each hunk will be either in state HEAD or in state A, and applying the diff between HEAD and A to such file will be either a no-op (hunk already applied), or a successfull application.
For me this is a severe limitation of git add --patch
, and I don't understand in what way this behaviour may be useful to anyone, but I will learn to live with it.
git stash --keep-index
preserves your index, but it still adds the index contents as part of the stash.
Try git stash save -p
-- a bit more tedious to save the stash, but will probably do what you want.
To create and test an index containing part of the working tree changes, including manually edited hunks, do:
git add --patch <files>
git stash --keep-index
<test the indexed changes>
git reset --hard
git stash pop --index
At this point there are no conflicts, and the repository, index, and working directory are in the state immediately preceding the git stash
. You can now git commit
the indexed changes.
Of course, this is rather weird and not very intuitive and I would really like to know whether there is an easier way to do this.