Skip to contentSkip to conversation

"git stash pop" is doing an unwanted "git add" when there are conflicts.

DISCUSSION Alan Mackenzie 4 participants 10 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

Hello, git project.

Last night, whilst clearing out a stale "stash stack", I did "git stash pop". There were conflicts in two files.

However, all the popped files became staged. This doesn't normally happen. It was intensely irritating, and required me to do "git reset HEAD" on each of the files, none of which I wanted to commit.

I searched the git-stash man page for this scenario, but found nothing about it.

Surely staging all the files is a bug?

Signature · 1 line
Alan Mackenzie (Nuremberg, Germany).
#1 lore raw
Quoted 5 lines · Alan Mackenzie

Hello, git project.

Last night, whilst clearing out a stale "stash stack", I did "git stash pop". There were conflicts in two files.

Quoted 8 lines

However, all the popped files became staged. This doesn't normally happen. It was intensely irritating, and required me to do "git reset HEAD" on each of the files, none of which I wanted to commit.

I searched the git-stash man page for this scenario, but found nothing about it.

Surely staging all the files is a bug?

That depends. A stash is two commits: one for all changes that were in the index when you ran 'git stash save' and one for all changes not yet in the index. When you pop the stash, these then get restored as staged resp. unstaged changes. So if your changes are now all staged, I'd wager that they were staged when you ran git stash save.

Signature · 2 lines
Dennis Kaarsemaker
www.kaarsemaker.net
#2 lore raw
Quoted 13 lines · Dennis Kaarsemaker
Quoted 12 lines · Alan Mackenzie

Hello, git project.

Last night, whilst clearing out a stale "stash stack", I did "git stash pop". There were conflicts in two files.

However, all the popped files became staged. This doesn't normally happen. It was intensely irritating, and required me to do "git reset HEAD" on each of the files, none of which I wanted to commit.

I searched the git-stash man page for this scenario, but found nothing about it.

Quoted 7 lines
Quoted 1 line

Surely staging all the files is a bug?

That depends. A stash is two commits: one for all changes that were in the index when you ran 'git stash save' and one for all changes not yet in the index. When you pop the stash, these then get restored as staged resp. unstaged changes. So if your changes are now all staged, I'd wager that they were staged when you ran git stash save.

No, I think there's something else going on. Try this:

    git init repo &&
    cd repo &&
    
    echo base >one &&
    echo base >two &&
    git add . &&
    git commit -m base &&
    
    echo stash >one &&
    echo stash >two &&
    git stash &&
    
    echo "==> No conflicts, nothing staged"
    git stash apply &&
    
    git reset --hard &&
    echo changes >two &&
    git commit -am changes &&
    
    echo "==> Conflict stages non-conflicting file 'one'"
    ! git stash apply &&
    git status

It seems to be a side effect of merge-recursive to stage the results, and in the no-conflict path we explicitly reset the index. For the conflicting case, it's trickier, because we would want to retain the unmerged entries.

So I agree it's kind of weird, but the conflicting case is inherently going to touch the index, and you'd generally have to git add to mark the resolutions (but if you really want to just touch the working tree, you'd need to git reset).

-Peff

#9 lore raw
In reply to Alan Mackenzie

Alan Mackenzie <[email protected]> wrote: on Mon, 21 Dec 2015 14:29:54

Quoted 5 lines

Hello, git project.

Last night, whilst clearing out a stale "stash stack", I did "git stash pop". There were conflicts in two files.

Quoted 8 lines

However, all the popped files became staged. This doesn't normally happen. It was intensely irritating, and required me to do "git reset HEAD" on each of the files, none of which I wanted to commit.

I searched the git-stash man page for this scenario, but found nothing about it.

Surely staging all the files is a bug?

Sorry, forgot to mention I'm using git 2.4.10 on GNU/Linux.

Signature · 1 line
Alan Mackenzie (Nuremberg, Germany).
#3 lore raw
In reply to Jeff King

Hello, Jeff.

Quoted 40 lines · Jeff King
Quoted 13 lines · Dennis Kaarsemaker
Quoted 11 lines · Alan Mackenzie

Hello, git project.

Last night, whilst clearing out a stale "stash stack", I did "git stash pop". There were conflicts in two files.

However, all the popped files became staged. This doesn't normally happen. It was intensely irritating, and required me to do "git reset HEAD" on each of the files, none of which I wanted to commit.

I searched the git-stash man page for this scenario, but found nothing about it.

Quoted 7 lines
Quoted 1 line

Surely staging all the files is a bug?

That depends. A stash is two commits: one for all changes that were in the index when you ran 'git stash save' and one for all changes not yet in the index. When you pop the stash, these then get restored as staged resp. unstaged changes. So if your changes are now all staged, I'd wager that they were staged when you ran git stash save.

No, I think there's something else going on. Try this:

    git init repo &&
    cd repo &&
    
    echo base >one &&
    echo base >two &&
    git add . &&
    git commit -m base &&
    
    echo stash >one &&
    echo stash >two &&
    git stash &&
    
    echo "==> No conflicts, nothing staged"
    git stash apply &&
Quoted 7 lines
    git reset --hard &&
    echo changes >two &&
    git commit -am changes &&
    
    echo "==> Conflict stages non-conflicting file 'one'"
    ! git stash apply &&
    git status

Thanks for creating a reproducible test case for me!

Quoted 11 lines

It seems to be a side effect of merge-recursive to stage the results, and in the no-conflict path we explicitly reset the index. For the conflicting case, it's trickier, because we would want to retain the unmerged entries.

So I agree it's kind of weird, but the conflicting case is inherently going to touch the index, and you'd generally have to git add to mark the resolutions (but if you really want to just touch the working tree, you'd need to git reset).

From the point of view of a user, this is suboptimal. git stash is an

abstraction: the preservation of uncomitted changes for later. Staging previously unstaged changes with git stash pop severely damages this abstraction.

Are there any prospects of this getting fixed?

Quoted 1 line

-Peff

Signature · 1 line
Alan Mackenzie (Nuremberg, Germany).
#4 lore raw
Quoted 10 lines · Alan Mackenzie
Quoted 9 lines

It seems to be a side effect of merge-recursive to stage the results, and in the no-conflict path we explicitly reset the index. For the conflicting case, it's trickier, because we would want to retain the unmerged entries.

So I agree it's kind of weird, but the conflicting case is inherently going to touch the index, and you'd generally have to git add to mark the resolutions (but if you really want to just touch the working tree, you'd need to git reset).

Quoted 4 lines

From the point of view of a user, this is suboptimal. git stash is an
abstraction: the preservation of uncomitted changes for later. Staging previously unstaged changes with git stash pop severely damages this abstraction.

Yeah, I think I agree. But keep in mind that we have to mention the conflicts _somewhere_, so we're going to touch the index regardless (and the user is going to have to erase the conflicts in the index eventually, either with git add or git reset).

Quoted 1 line

Are there any prospects of this getting fixed?

Somebody needs to write a patch. I am not 100% convinced that it _should_ be fixed, but I am leaning that way. But I am not planning to work on it myself anytime soon. The best way to get more discussion going is to post a patch. :)

-Peff

#5 lore raw

Hello, Jeff.

Quoted 17 lines · Jeff King
Quoted 10 lines · Alan Mackenzie
Quoted 9 lines

It seems to be a side effect of merge-recursive to stage the results, and in the no-conflict path we explicitly reset the index. For the conflicting case, it's trickier, because we would want to retain the unmerged entries.

So I agree it's kind of weird, but the conflicting case is inherently going to touch the index, and you'd generally have to git add to mark the resolutions (but if you really want to just touch the working tree, you'd need to git reset).

Quoted 4 lines

From the point of view of a user, this is suboptimal. git stash is an
abstraction: the preservation of uncomitted changes for later. Staging previously unstaged changes with git stash pop severely damages this abstraction.

Quoted 4 lines

Yeah, I think I agree. But keep in mind that we have to mention the conflicts _somewhere_, so we're going to touch the index regardless (and the user is going to have to erase the conflicts in the index eventually, either with git add or git reset).

When the stash consists entirely of changes in the working directory, and "git stash pop" has conflicts, why can't these conflicts simply be marked by "<<<<<<<<" (etc.) in the working directory, leaving the index unchanged? The index is left unchanged when there are no conlicts.

Quoted 6 lines
Quoted 1 line

Are there any prospects of this getting fixed?

Somebody needs to write a patch. I am not 100% convinced that it _should_ be fixed, but I am leaning that way. But I am not planning to work on it myself anytime soon. The best way to get more discussion going is to post a patch. :)

Hmm. I would very much prefer to remain just a user of git.

Quoted 1 line

-Peff

Signature · 1 line
Alan Mackenzie (Nuremberg, Germany).
#7 lore raw
In reply to Jeff King
Quoted 4 lines · Jeff King

Yeah, I think I agree. But keep in mind that we have to mention the conflicts _somewhere_, so we're going to touch the index regardless (and the user is going to have to erase the conflicts in the index eventually, either with git add or git reset).

I do not think there is much we can do at this point in time (but notice that I did say "much", and didn't say "anything").

The way we replay any potentially conflicting change and present the result to the end user in Git is uniformly to add the cleanly applied parts to the index while leaving the conflicted ones as unmerged index entries, but "git stash apply/pop" does not follow this pattern. I think this is partly because the command is also used to "stash away" a work-in-progress changes to the index, and avoiding to touch the index with the changes in the working tree would more closely match the behaviour of the command when there is no conflict.

I notice that I said the same thing long time ago, i.e. in the initial round of review:

http://article.gmane.org/gmane.comp.version-control.git/50749

| However, I think it is a bit counterintuitive to update the
| working tree change to the index if the merge did not conflict.
| It might be better to run an equivalent of "git reset", so that
| after "git save restore", the user can say "git diff" (not "git
| diff HEAD") to view the local changes.  Perhaps...

In the above, I suggested to "git reset" when there is no conflict. I think this line of thinking can be followed even further to selectively reset the paths that were cleanly merged (which is added by the call to merge-recursive), leaving _only_ the conflicted paths.

Would that give us a better outcome? I dunno.

Quoted 6 lines
Quoted 1 line

Are there any prospects of this getting fixed?

Somebody needs to write a patch. I am not 100% convinced that it _should_ be fixed, but I am leaning that way. But I am not planning to work on it myself anytime soon. The best way to get more discussion going is to post a patch. :)

#6 lore raw
In reply to Alan Mackenzie
Quoted 9 lines · Alan Mackenzie
Quoted 4 lines

Yeah, I think I agree. But keep in mind that we have to mention the conflicts _somewhere_, so we're going to touch the index regardless (and the user is going to have to erase the conflicts in the index eventually, either with git add or git reset).

When the stash consists entirely of changes in the working directory, and "git stash pop" has conflicts, why can't these conflicts simply be marked by "<<<<<<<<" (etc.) in the working directory, leaving the index unchanged? The index is left unchanged when there are no conlicts.

I don't think that's a good idea. Git always marks conflicts in the index for other operations. Besides being inconsistent with the rest of git, it drops useful information that other tools can use. For example, one cannot "git checkout --conflict=diff3" afterwards, or use "git mergetool" to kick off a third-party merge tool.

Not to mention that the information is lost to the user themselves. If we touched 10 files and 2 had conflicts, there is now no way for the user to ask "where were the conflicts?". They can either find the stash output in their terminal scrollback, or grep for things that look like conflict markers.

-Peff

#8 lore raw
In reply to Junio C Hamano
Quoted 7 lines · Junio C Hamano

[...]
In the above, I suggested to "git reset" when there is no conflict. I think this line of thinking can be followed even further to selectively reset the paths that were cleanly merged (which is added by the call to merge-recursive), leaving _only_ the conflicted paths.

Would that give us a better outcome? I dunno.

Yes, I think that is the only sensible change to make. The only other option (besides leaving it as-is) would be to unstage _everything_ leaving conflict-marker cruft in the working tree, but no conflicts in the index. I think that's a mistake, but I won't repeat the arguments I left elsewhere in the thread.

So it's probably something like:

git ls-files -t |
grep -v ^M |
cut -d ' ' -f2- |
xargs git reset --

(modulo some quoting robustness improvements). But I guess we'd want to preserve any modifications that were originally in the index. So maybe the intersection of the files above and the output of "git diff-tree --name-only" on the stash commit.

There are probably some corner cases to look at (e.g., with "--index").

As I said, I'm not planning to work on it anytime soon, but the above may give some clues to somebody who wants to pursue it.

-Peff