Vlad Aleshin
Vlad Aleshin

Reputation: 411

Git stash pop error - Your local changes to the following files would be overwritten by merge

I have the following error when i use git stash pop:

error: Your local changes to the following files would be overwritten by merge:
        user/h2oai_driver.py
        user/quantile_lift.R
        user/scratch.ipynb
Please commit your changes or stash them before you merge.
Aborting
The stash entry is kept in case you need it again.

I want to pop my changes from stash. But I don't need any changes in user/... files. What should i do in this situation? (i have nothing to commit or stash so i can't follow the advice commit your changes or stash them before you merge)

Upvotes: 0

Views: 1952

Answers (2)

Jonas Kölker
Jonas Kölker

Reputation: 7837

Here's my approach:

  1. Unstage all local modifications
  2. Run git diff > /tmp/local-modifications.patch
  3. Run patch -Rp1 < /tmp/local-modifications.patch to remove all local changes
  4. Pop the stash I want to pop
  5. Run patch -p1 < /tmp/local-modifications.patch to restore the first set of changes

Look carefully at the output of the last patch to see how it conveyed conflicts (if any).

If you want full undo-ability of all of this, save a second patch file after step 4. Then reset your repo to a clean working tree and index, apply the second patch, stash those changes, then apply the first patch to restore your initial local changes.

Upvotes: 0

torek
torek

Reputation: 487755

(i have nothing to commit or stash so i can't follow the advice commit your changes or stash them before you merge)

You could follow that advice, but it leads you down a rabbit hole, especially if you use git stash for this. Stashing might let you pop the other stash, but then you have a new stash, and what will you do with that? 😅 So, let's look at another option.

The reason git stash pop is complaining here is that the application of a stash—the git stash apply step1—involves running git merge.2 That merge operation is going to overwrite those three files.

But I don't need any changes in user/... files.

Perhaps, by this, you mean: After restoring the stashed changes, I'd like to discard the updated user/... files and put back the ones I had just before I ran git stash pop.

If that is what you mean, then just do that. For instance:

cd user
mkdir /tmp/save
cp h2oai_driver.py quantile_lift.R scratch.ipynb /tmp/save
git restore -s HEAD -SW h2oai_driver.py quantile_lift.R scratch.ipynb

This makes a place to save the three files, then uses git restore to undo your current changes, so that git merge can merge these three files. Then:

git stash pop

This step merges into those three files, along with everything else that git stash pop does. Then:

cp /tmp/save/* .

puts the three files back. These commands all assume a Unix-like shell, of course; make any necessary alterations for whatever shell / CLI you use.

(Note: I am not an R or ipynb user, so I am not sure what this is all about here.)


1Remember, git stash pop just means git stash apply && git stash drop: do the apply first, and then if and only if the apply succeeds, drop the now-applied stash.

2Technically it runs git merge-recursive directly, rather than using git merge. This is because the set of commits that store a stash are ... weird. Stashes are commits, and this weirdness is one reason I actually recommend avoiding git stash if possible. The git stash command is convenient, when it works, but when it gets you into corner cases, it can become extremely difficult to use.

Upvotes: 1

Related Questions