375

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

3
  • 14
    Ugly solution until somebody comes up with a better one: remove the new files using git rm before doing git stash apply. Commented May 17, 2013 at 10:08
  • Won't that lose the history? The reason for adding the unmodified versions was to keep the history. Commented May 17, 2013 at 10:11
  • 1
    No, it will not lose the history. If you remove a file, add it back with a small modification, and then commit, git will treat it as just a small modification. Commented May 17, 2013 at 10:19

9 Answers 9

600

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 

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 
Sign up to request clarification or add additional context in comments.

11 Comments

git checkout stash -- . This command literally did nothing when I ran it.
@RotsiserMho Git is not very good about providing confirmation messages. Are you sure it did nothing? Worked fine for me.
I had to run git checkout stash -- . in the highest parent folder that contained my changes, otherwise it only applied changes to the folder from where I ran the command.
@Leo: That's right, it's because . specifies the current directory (you could replace it with a different path instead of changing directory). I updated my answer to make that clearer.
@tom Semi-hard way. I did overwrite unstaged changes, but luckily there was another recovery option: PHPStorm's local history saved my sorry ass.
|
51

git stash show -p | git apply

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

6 Comments

Note: you should be in the root of the repository for this to work correctly, otherwise you'll get this.
@Ruslan I'm getting that while in the root of my repo 🤔
and what to do if I get "patch does not apply"? I just would like to get my stashed changes!
@eis you can add --reject --whitespace=fix as options for git apply. Check this explanation for how it works. All together: git stash show -p | git apply --reject --whitespace=fix
@HassanTBT Bizarrely, this completely ignored one of the files with stashed changes for me. It also left me with some .rej files to clean up. I had to resort to this: stackoverflow.com/a/76680580/1715765
|
20

To force git stash pop run this command

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

2 Comments

good answer. this is so frustrating, though. why couldn't they just have added a --force flag? ugh.
This answer could be improved by explaining what it does.
7

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.

3 Comments

"You get this error because of the uncommited changes that you want to overwrite." This is not necessarily true. If the stash is originally from a different branch and both your current branch and the stashed changes modify the same files, you will get a merge conflict as well.
Thumbs up for the idea to revert your changes to solve the issue. This is especially useful when changes' reversion is manageable.
This solution works for binary files like *.db as well.
4

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.

Comments

3

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

Comments

1

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.

1 Comment

This solved my issue.
0

WARNING This will delete all uncommited changes

git checkout HEAD --force git stash apply 

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.