How to combine multiple stashes in git

sscirrus picture sscirrus · Feb 4, 2012 · Viewed 28.4k times · Source

This is a pipeline on branch frontend over the last two weeks.

| Stash@{3} is all code since Stash@{1} (excluding the two tiny commits)
| Tiny Commit
| Tiny commit
| Huge bulk commit two weeks ago, now rebased and moved to Stash@{1}

My working tree is currently clean.
Stash@{1} is the contents from a bulk commit of general development code two weeks ago (this should have been stashed in the first place). This commit was undone and moved to stash.
Stash@{3} is the newest work on that tree since Stash@{1} (minus a couple of changes that have been committed).

I need to combine these two stashes together in my working tree so I can make a number of commits from this huge pool of work.

I ran git stash apply stash@{1} then I tried:

git stash apply stash@{3}
git stash show -p | git stash apply stash@{3}

but I get 'dirty working tree' in both cases. How can I merge this work together? Because stash@{3} is newer, I want it to supersede stash@{1} wherever there are conflicts.

Answer

bkeepers picture bkeepers · Jan 24, 2013

It's a little involved, but this almost always works:

  1. Pop the first stash

    $ git stash pop
    
  2. Temporarily commit the changes from the first stash

    $ git add . && git commit -am 'WIP'
    
  3. Pop the second stash

    $ git stash pop
    
  4. Undo the temporary commit, keeping the changes it introduced

    $ git reset --soft HEAD^