Force git stash to overwrite added files

git stash show -p | git apply

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


Use git checkout instead of git stash apply.

WARNING: The command below will restore all the files in the current directory (.) to their stashed version. If you have uncommitted or unstaged changes, they will be permanently lost:

  • If you edited files after creating the stash, those changes will be lost.
  • If you only stashed specific files (using git stash push <pathspec>... or git stash -p), do not use this command because changes in all other files will be lost.

Use git status to check that there are no uncommitted or unstaged changes before running this command.

# WARNING: uncommitted/unstaged changes will be permanently lost
$ git checkout stash -- .
$ git commit

If there are changes to other files in the working directory that should be kept, here is a less heavy-handed alternative:

$ git merge --squash --strategy-option=theirs stash

Note that this only works in the situation described in the question, where you've made a commit after creating the stash (if there are no commits, git will fast-forward, which is not what we want).

If there are changes in the index, or the merge will touch files with local changes, git will refuse to merge. Individual files can be checked out from the stash using

$ git checkout stash -- <paths...>

or interactively with

$ git checkout -p stash

Tags:

Git