'Force git stash to overwrite added files

I have some files which were untracked in git. I made some changes and wanted to commit them, but realised I had forgotten to check in the unmodified files first. So I stashed the files, then added the unmodified versions.

Then when I apply the stash to the repository, I get conflicts due to the files having already been added.

How can I apply the stash, and force the versions in the stash to be used in preference to the originals in the repository?

Thanks

git


Solution 1:[1]

git stash show -p | git apply

and then git stash drop if you want to drop the stashed items.

Solution 2:[2]

To force git stash pop run this command

git stash show -p | git apply && git stash drop

Solution 3:[3]

TL;DR:

git checkout HEAD path/to/file
git stash apply

Long version:

You get this error because of the uncommited changes that you want to overwrite. Undo these changes with git checkout HEAD. You can undo changes to a specific file with git checkout HEAD path/to/file. After removing the cause of the conflict, you can apply as usual.

Solution 4:[4]

Keep local source backup then apply force reset to align with GIT repo. Then change your local code and commit.

git reset --hard FETCH_HEAD

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hassan TBT
Solution 2 NetEmmanuel
Solution 3 User12547645
Solution 4 Mahbub Tito