Reputation: 9329
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
Upvotes: 371
Views: 277089
Reputation: 1633
The other solutions didn't work for me. Here's what I did:
git branch temp && git reset --hard stash && git reset temp && git branch -D temp
To hard-reset and then reset back changes your working tree without changing your branch.
Upvotes: 0
Reputation: 125
WARNING This will delete all uncommited changes
git checkout HEAD --force
git stash apply
Upvotes: 0
Reputation: 11916
I think this option from tom's answer is the simplest and least error-prone method of autoresolving conflicts when applying a stash:
$ git merge --squash --strategy-option=theirs stash
You can input it exactly like that: git uses stash
as a commitish to refer to the top of the stash.
It will leave you with all the stashed files added to the index and resolved to match the stash, but doesn't create any commits. Additionally, it will abort if you have local changes in your working copy you don't risk losing changes.
It's so useful, that I think it's worth an alias in ~/.gitconfig:
[alias]
stashforceapply = "!f() { stash=${1:-stash}; git merge --squash --strategy-option=theirs $stash ; }; f"
Use git stashforceapply
for the top stash, or git stashforceapply stash@{1}
for the next, etc.
Upvotes: 2
Reputation: 22939
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:
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
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
Upvotes: 594
Reputation: 1547
In my case git checkout stash --
would throw this error
error: Your local changes to the following files would be overwritten by checkout:
... files ...
Please commit your changes or stash them before you switch branches.
Aborting
The solution was to do git checkout stash -- /path/to/each/individual/file
.
Upvotes: 0
Reputation: 1127
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
Upvotes: 3
Reputation: 8437
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.
Upvotes: 5
Reputation: 3173
To force git stash pop
run this command
git stash show -p | git apply && git stash drop
Upvotes: 20
Reputation: 873
git stash show -p | git apply
and then git stash drop
if you want to drop the stashed items.
Upvotes: 48