LF-DevJourney
LF-DevJourney

Reputation: 28529

git apply multi stash at one time

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.

Upvotes: 2

Views: 1975

Answers (3)

pesho hristov
pesho hristov

Reputation: 2060

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.

Upvotes: 0

Rahul Sharma
Rahul Sharma

Reputation: 5995

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}

Upvotes: 1

Mark Bramnik
Mark Bramnik

Reputation: 42441

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.

Upvotes: 1

Related Questions