2

I have stash two times, and I need to commit the two stash in one commit.

I use git stash apply to apply the latest stash, but when I use it again, it throw below error,

error: Your local changes to the following files would be overwritten by merge:
        library/HQ/groupsql.sql
Please commit your changes or stash them before you merge.
Aborting
The stash entry is kept in case you need it again.

How can I pop the two stash then commit them.

LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    https://stackoverflow.com/questions/9143865/how-to-combine-multiple-stashes-in-git read this – abby37 Nov 19 '19 at 04:50
  • @abby37 it make each stash a commit. – LF00 Nov 19 '19 at 04:52
  • 1
    after u run first stash apply u add a file to given commit, u again run stash apply and then add file to given commit. So all those stash changes would be commited in given commit by using git commit --amend – abby37 Nov 19 '19 at 04:54
  • Is it possible to make it in one time. – LF00 Nov 19 '19 at 05:01
  • 2
    The best advice I have for you is: *don't*. Do not attempt to combine multiple stashes like this. Just apply one and commit, then apply the next one and commit, then use `git rebase -i` to squash the two commits. – torek Nov 19 '19 at 07:57

3 Answers3

1

I think you can do the following:

// to see the list of "stashes" (like every time when stash run 'git stash' a new entry is created in the list
> git stash list

// Lest assume you need stash@{1} and stash@{3} for the sake of example

> git stash apply stash@{1} 
git commit -am "Whatever message"

> git stash apply stash@{3}
git commit --amend  // to add changes from stash3 from the previous commit

Bottom line, each stash apply once done produces commit on the local branch but you can alter the history as longs as the changes are local (not pushed)

I suggest you also to read This thread, it contains some neat trick, available since git 2.11: you can use git stash apply n instead of git stash apply stash@{n}

Another advice: you can use git stash show stash@{3} to see what files were changed in the stash 3 (if you're not sure which stash to apply)

I've never seen the feature of doing this in one command though. So for now my answer is "no, you can't apply multiple stashes at once". I'll be glad to be proven otherwise by more experienced git users.

Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
  • 1
    OP wants to do that in single command, for that purpose he have to write his own custom command. Right ?? – abby37 Nov 19 '19 at 05:40
  • 1
    I believe yes, OP has to create a custom script for that, but than again, if there are conflicts between stashes (that potentially can happen) they must be resolved somehow (probably manually) – Mark Bramnik Nov 19 '19 at 05:46
1

I don't think you can do that in a single go unless, of course, you're ready to write your own script for doing so. The reason is you might have overlapping changes across your current HEAD and two stashes. Even if you write a script for doing that for you, you'd have trouble resolving conflicts since your script doesn't know which changes to keep and which to discard. So, the correct way would be doing this by the book i.e.:

git stash apply stash@{1}
# resolve conflicts if any
git add .
git commit -m "<commit_message>"
git stash apply stash@{2}
Rahul Sharma
  • 5,562
  • 4
  • 24
  • 48
0

If you want to have the changes at once - then you can do this:

git stash apply stash@{1}
git commit -am "Dummy commit"
git stash apply stash@{2}
git reset HEAD~

After the above sequence - you will have the changes of both stashes at the same time - and you can make a single commit from both of them.

pesho hristov
  • 1,946
  • 1
  • 25
  • 43