I began making changes to my codebase, not realizing I was on an old topic branch. To transfer them, I wanted to stash them and then apply them to a new branch off of master. I used git stash pop
to transfer work-in-progress changes to this new branch, forgetting that I hadn't pulled new changes into master before creating the new branch. This resulted in a bunch of merge conflicts and loss of a clean stash of my changes (since I used pop).
Once I recreate the new branch correctly, how I can I recover my stashed changes to apply them properly?
After that just fire git stash pop again and you get the same stash, that conflicted before. Keep in mind: The stash is safe, however, uncommitted changes in the working directory are of course not. They can get messed up. What I understand is that you can simply cleanup and pop again, but you can't undo it.
The stash entry is kept in case you need it again. There's no magic remedy for such merge conflicts. The only option for developers is to edit the file by hand and keep what they want and dispose of what they don't want. Once they merge and save the file, they will have effectively resolved the git stash conflict.
When popping out commits using the git stash pop command, you will have some merge conflicts sometimes, you can move to resolve those conflicts or abort the whole process. command. This has worked for me.
You can use the git reset --merge command. You can also use the git merge --abort command. As always, make sure you have no uncommitted changes before you start a merge.
As it turns out, Git is smart enough not to drop a stash if it doesn't apply cleanly. I was able to get to the desired state with the following steps:
git reset HEAD .
(note the trailing dot)git stash
git checkout master
git fetch upstream; git merge upstream/master
git checkout new-branch; git rebase master
git stash apply stash@{1}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With