Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine multiple stashes in git

Tags:

git

git-stash

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.

like image 680
sscirrus Avatar asked Feb 04 '12 19:02

sscirrus


People also ask

Can you have multiple stashes git?

If you want to git stash pop twice because you want both stashes in the same commit but you encounter "error: Your local changes to the following files would be overwritten by merge:" on your 2nd git stash pop , then you can: 1) git stash pop , 2) git add . , and 3) git stash pop . This helped me.

How many git stashes can you have?

Show activity on this post. 1 - How many stashes are saved? So how many are saved? As many as you create.

How do I merge changes in git stash?

I just discovered that if your uncommitted changes are added to the index (i.e. "staged", using git add ... ), then git stash apply (and, presumably, git stash pop ) will actually do a proper merge. If there are no conflicts, you're golden. If not, resolve them as usual with git mergetool , or manually with an editor.

What is git stash stack?

Git stash is used in order to save all the changes done to the current working directory and to go back to the last commit done on the branch (also called HEAD). Stashing changes comes with a special set of Git commands designed to create, delete and apply stashes at will.


2 Answers

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^ 
like image 104
bkeepers Avatar answered Sep 27 '22 22:09

bkeepers


You can only apply a stash if there are no conflicts with modified files in the working tree, so, first, ensure there are no modified files in git status, if there are, commit them. Then do:

git stash apply stash@{1} git commit -a # Enter your commit message git stash apply stash@{3} 

Then you can either make a new commit, or amend the previous one to combine them. You may need to resolve merge conflicts after each apply.

Also, if you ever decide to use git stash pop rather than apply, note that stash@{3} would become stash@{2} since the first one was popped off.

like image 37
Andrew Marshall Avatar answered Sep 27 '22 23:09

Andrew Marshall