Pergunta

I had a dirty working directory and used git stash save -p to selectively stash some of the changes. My intention is splitting what used to be a big commit into two smaller commits. The problem now is that I accidentally stashed the wrong thunks so I want to do it again. I tried doing a git stash pop, as suggested in this question but doing that gives me this error:

error: Your local changes to the following files would be overwritten by merge:
    my_file.js
Please, commit your changes or stash them before you can merge.
Aborting
Foi útil?

Solução

For me, the following was enough:

% git add myfile.js
% git stash pop
% git reset myfile.js

Alternatively, commit myfile.js, then pop the stash, resolving any conflicts. To get rid of the dummy commit, git reset --soft HEAD^. Something like:

% git add myfile.js
% git commit
% git stash pop
    # resolve conflicts if needed
% git reset --soft HEAD^

--soft means don't touch the working tree or index; HEAD^ means the commit just before the last one.

The manual actually mentions this behavior, though I've never noticed it until now:

pop [--index] [-q|--quiet] [<stash>]

Remove a single stashed state from the stash list and apply it on top of the current working tree state, i.e., do the inverse operation of git stash save. The working directory must match the index.

(emphasis mine) When we do git add, we're causing the index and working directory to match (they both contain the change that wasn't stashed). Similarly, when we create a commit, they match. (they both contain no changes)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top