Reputation: 19684
In my current branch I have several stashes:
stash@{0}
stash@{1}
stash@{2}
If I apply stash@{0}
:
$ git stash apply stash{0}
Modify this stash
I want to save the changes to the current stash stash@{0}
I don't want to create a 4th stash I just want to update the first stash.
Can someone tell how to do this? I'm looking at the man page… perhaps I'm overlooking something.
Upvotes: 42
Views: 15023
Reputation: 9190
I needed to do this as well.
My main use of git stash
I keep a set of stashes that I like to apply (not pop) fairly often. For instance, I have a stash that applies a certain configuration which is different than what is checked into the repo. After getting latest, I will just run something like git stash apply 0
after checking the stash list.
The problem I just now ran into
When applying a stash, you might run into merge conflicts. When this occurs, you are forced to fix the merge conflicts. This is something you obviously don't want to do every time you apply the stash.
Resolution
This obviously won't keep the index the same, unless the item you are applying is at the top of the stack, but it works for me. I only care that I have the item in the stash, not where it is in relation to other stashed items.
# Find the item in the stash I'm trying to apply, based on the description.
git stash list
# Apply the stashed item, based on its index.
git stash apply 0
# Merge conflicts found... correct them.
git mergetool
# Drop the stash by whatever index was found.
# This is necessary since doing a git stash pop doesn't remove a stash that has merge-conflicts.
# In that scenario, it says "The stash entry is kept in case you need it again."
git stash drop 0
# Resave the stash with the merge-conflicts taken care of.
git stash save "Some message for identifying the stash in the list"
Upvotes: 3
Reputation: 64923
You can stash your working tree and then drop the old one that you don't need.
git stash apply
# Make changes
git stash
git stash drop stash@{1}
Alternatively, you can pop instead of apply, which will drop the stash at the same time:
git stash pop
# make changes
git stash
Another alternative, if you already were already making the changes that you want to do before realizing that you want to merge the changes to the top of the stash:
# make changes
git add <all files>
git stash pop # or apply
git rm --cached <files that you want to merge>
git stash --keep-index
Upvotes: 31