Kris Roofe Kris Roofe - 13 days ago 10
Git Question

How to reverse apply a stash that with conflict?

I have seen the question How to reverse apply a stash? asked by Pat Notz. And I have tried the approved answer, but I get such an error,

sudo git stash show -p | git apply --reverse
error: patch failed: app/controllers/CloudController.php:673
error: app/controllers/CloudController.php: patch does not apply
error: patch failed: app/controllers/CloudGcode.php:1
error: app/controllers/CloudGcode.php: patch does not apply


I have to explain how I run in this situation.
I have a stashes in my stash list, and I have do some modify in my working repository. The changes in my working repository have conflict with the stash@{0}. Then I execute the
git add .
and
sudo git stash apply
commands by fault, and it shows this info,

sudo git stash apply
[sudo] password for xxxx:
Auto-merging app/controllers/CloudGcode.php
CONFLICT (add/add): Merge conflict in app/controllers/CloudGcode.php
Auto-merging app/controllers/CloudController.php
CONFLICT (content): Merge conflict in app/controllers/CloudController.php


After the stash apply, there is conflict in my file like this,

<<<<<<< Updated upstream
for($i = 0; $i < $textlen; $i++)
{
$char = $uchars[$index++];
if($char !== 0)
$text = $text.chr($char);
}
$this->text = $text;
Log::info('LLLLLLLLLLLLLLLLLLLL'.$text);
=======
for($i = 0; $i < $this->textlen; $i++)
$text = $text.$uchars[$index++];
$this->text = $text;
$this->text[$this->textlen] = 0; // Terminate string overwriting checksum
>>>>>>> Stashed changes
$this->waitUntilAllCommandsAreParsed = true; // Don't destroy string until executed
}
$this->formatErrors = 0;
return true;
}
<<<<<<< Updated upstream
=======


Then I google how to revert it. I come in the question How to reverse apply a stash? asked by Pat Notz, and tried the solution of that question.
I want to know is there a way to rool back the state before execute the
sudo git stash apply
, just after or before execute
git add .

Answer

You should simply re-stash, and then git reset (or even git reset --hard, provided you did stash first), as mentioned in "Aborting git stash apply".

If you do a reset --hard without stashing first, you can still see your patch in .git/refs/stash, as described in "Undo git reset --hard after git stash pop" (by a git stash apply would not remove the patch from the stash anyway, like git stash pop does, so here you don't have to worry about that), or you can recover it from git fsck.

I want to roll back the state before execute the sudo git stash apply

Since the git apply --reverse does not work, it is best to get back to HEAD as I suggest above, and redo your operations.