Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force git stash to overwrite added files

Tags:

git

People also ask

How do I force git stash?

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.

How do I force git pull to overwrite local files?

Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force ) allows overwriting local branches.

Does git stash remove added files?

By default git ignores untracked files when doing stash. If those files need to be added to stash you can use -u options which tells git to include untracked files. Ignored files can be added as well if -a option is specified.

Will a git pull overwrite my changes?

The reason for error messages like these is rather simple: you have local changes that would be overwritten by the incoming new changes that a "git pull" would bring in. For obvious safety reasons, Git will never simply overwrite your changes.


Use git checkout instead of git stash apply:

$ git checkout stash -- .
$ git commit

This will restore all the files in the current directory to their stashed version.


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

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

git stash show -p | git apply

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


To force git stash pop run this command

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

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.