How to combine multiple stashes in git

若如初见. 提交于 2019-12-02 14:49:48

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.

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^
    

A better way is to just use git stash show -p stash@{whatever} > stash-{whatever}.diff and then use git apply for each one.

I had a similar problem and solved it like this.

Use git stash pop to apply one of the stashes. Then create a patch of this stash with git diff -p > ../stash.diff. You can then reset your working tree (or stash the changes again), and pop the other stash with git stash pop stash@{1}. If you apply your patch at this moment you can 'merge' the two different stashes.

You'll probably have some conflicts to resolve. If all goes well you can then drop the stashed changes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!