37

In my current branch I have several stashes:

  • stash@{0}
  • stash@{1}
  • stash@{2}
  1. If I apply stash@{0}:

    $ git stash apply stash{0}
    
  2. Modify this stash

  3. 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.

SQB
  • 3,926
  • 2
  • 28
  • 49
Nick
  • 19,198
  • 51
  • 185
  • 312
  • 3
    The problem is clear, but what is the use case? Why are you not using branches for this? – Andrejs Cainikovs Oct 02 '13 at 17:41
  • Which stash index do you want to modify? – Bucket Oct 02 '13 at 17:41
  • 1
    Stashes aren't commits. If you need this sort of flexibility, it might be better to use a patch queue system. (I'm not sure what the preferred one for Git is, Quilt or StGIT.) Or maybe just private branches that you rebase. – millimoose Oct 02 '13 at 17:41
  • Are you asking how to modify the `topmost stash` only or a stash at a `particular index` and save it back to that index ? the latter would be pretty interesting – Ashish Gaur Oct 02 '13 at 17:47
  • @millimoose: stashes do have hashes so in a sense they are like commit if you consider how they were made in the tree, they're just treated only slightly differently than regular commits. – Lie Ryan Oct 02 '13 at 17:54
  • Stashes *are* commits, under the covers. Specifically they're two-or-three-parent commits of a work-dir state. `stash^` is the commit that was `HEAD` when the stash was made, `stash^2` is the index state when the stash was made, and `stash^3` (if it exists) contains untracked/ignored files. `stash` itself represents work-tree state. – torek Oct 02 '13 at 23:56

2 Answers2

26

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
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
1

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"

Seth Flowers
  • 8,990
  • 2
  • 29
  • 42