Reputation:
I have a stash with a bunch of files in it.
But I can't apply my stash because of a conflicting file. I've identified the problematic file in my stash and I want to remove it.
How can I remove a single file from a stash without destroying the entire thing?
Upvotes: 67
Views: 60925
Reputation: 187
I had the same issue today. I was working on a branch, stashed my modifications and wanted to apply them on another branch. But I noticed that I also stashed an unwanted file that created a conflict. Here's what I did to solve the issue:
git restore --staged path/to/file
git reset HEAD -- path/to/file
to remove the file from the staging areaThen, I added the files I wanted only and was able to make my commit.
Upvotes: 1
Reputation: 3482
Upvotes: 4
Reputation: 9
delete the required file from your local codebase and then push it to stash. The changes will get reflected and your files will be deleted from stash.
Upvotes: -1
Reputation: 5083
There is a workaround for this situation:
save your stash as a patch file:
$ git stash show -p > stash.patch
apply this patch, skipping the conflicts (you will be asked for resolution of conflicts, just skip missing files):
$ patch -p1 < stash.patch
Don't forget to clean up stash.patch
afterwards!
Upvotes: 36
Reputation: 488193
A stash is a commit (or really, two or even sometimes three commits) and you cannot change a commit. The literal answer to your question, then, is "you can't". Fortunately, you don't need to.
You say you can't apply your stash because of a conflicting file. But you can apply it, you just get a merge conflict. All you need to do is resolve the merge conflict.
Let's say the conflict is in file README.txt
, just so there's something to write about here.
If you want to resolve it by keeping the on-branch version, apply the stash, then check out the on-branch version to resolve the conflict:
git stash apply
git checkout --ours -- README.txt # or git checkout HEAD -- README.txt
If you want to keep the in-stash version, extract that one:
git checkout --theirs -- README.txt # or git checkout stash -- README.txt
Or, use any old merge resolution tool (I just use a text editor), and then "git add" the result.
Once you are all done with the stash, git stash drop
will "forget" the commits that make up the stash. (Don't do this until you are sure you are done with it; it's very hard to get it back afterward.)
Upvotes: 42