1

I did some changes to some file in the working directory.

I then stashed the changes:

$ git stash
Saved working directory and index state WIP on B03799: 627b569 Merge pull request #376 from xxx/B02814
HEAD is now at 627b569 Merge pull request #376 from xxx/B02814

Then I ran a couple of commands, the related commands of which are

git checkout staging

and some other commands which I guess are not related, and then

$ git checkout  B03799
warning: refname 'B03799' is ambiguous.
Switched to branch 'B03799'

When I tried to reapply the stashed changes, it failed

$ git stash show -p stash@{0} | git apply -R
<stdin>:9: trailing whitespace.
                ...
<stdin>:18: trailing whitespace.
                ...
<stdin>:29: trailing whitespace.
                ...
<stdin>:116: trailing whitespace.
                                ...
<stdin>:117: trailing whitespace.
                                ....
error: patch failed: xxx.cs:24
error: xxx.cs: patch does not apply

I am not sure if I have provided enough information. What are some possible reason that caused the failure of reapplying the stashed changes?

This is my first time using git stash, and I really hope to avoid misusing it and to utilize its power, if you could let me know some possibilities which can cause reapplying stashed changes to fail.

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • 2
    Does `git stash apply` do anything different (vs. `git stash | git apply`)`? – Whymarrh Apr 24 '17 at 22:40
  • Thanks. I have been redoing the changes to the files, so it is still safe to run `git stash apply` now? I am worrying about messing up again. (1) What's the difference between `git stash apply` and `git stash | git apply`? (2) What do you suspect the cause? – Tim Apr 24 '17 at 22:52
  • I don't know off the top of my head what the difference between `git show -p x | git apply` is vs `git stash apply` but I would recommend using `git stash apply` as that's the command for what you're trying to do. (I *imagine* the prior is reformatting the diff as a patch and then trying to apply the patch.) – Whymarrh Apr 24 '17 at 22:57
  • If you're working copy is "clean" then you should safely be able to run stash apply and see what changes. – Whymarrh Apr 24 '17 at 22:57
  • Also, you're using `-R` in `git apply`—is that intentional? – Whymarrh Apr 24 '17 at 22:58
  • @Whymarrh not intentionally. I copied `git stash show -p stash@{0} | git apply -R` from https://git-scm.com/book/en/v1/Git-Tools-Stashing – Tim Apr 24 '17 at 23:02
  • I think your checkout is a problem. Given things are a bit doubtful I'd save your stash as a patch file somewhere else before doing anything else - something like `git stash show -p stash@{0} > ../../MyWorkTemp.patch` The 'refname is ambiguous' is worrying - we're not 100% sure what you've got, but it seems that xxx.cs is so different from what the patch is expecting that git can't figure out what to do. I'd be seriously tempted to clone the repo again (DON'T remove / overwrite your current one, don't lose your patch file) - as per xkcd. https://xkcd.com/1597/ – GregHNZ Apr 24 '17 at 23:58
  • @Greg: Thanks. for the refname ambiguous problem, see http://stackoverflow.com/questions/43598452/warning-refname-xxx-is-ambiguous – Tim Apr 25 '17 at 00:01
  • `git apply -R` applies the patch in reverse. Probably not what you wanted. – Mort Apr 25 '17 at 01:26

0 Answers0