Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge status lost when stashing

Tags:

git

merge

It happens that information about a merge are lost after we do a stash. A usual merge and a stashed merge are compared below.

Right: merge + push

git merge release-2013-10-29
# Everything works, cool
git commit -am "Upgraded to release 2013-10-29"
git push origin dev

Result is as expected, a merge commit:

enter image description here

Wrong: merge + stash + push

Here I am in a scenario where I need to stash the merge changes just in order to have a look at the previous-to-the-merge behaviour.

git merge release-2013-11-06
# Conflicts fixed, but detected inappropriate behaviour
git stash
git stash pop
git commit -am "Upgraded to release 2013-11-06"
git push origin dev

Result: this commit is not a "merge" anymore. We also lost all single authored commits contained by the merge, just like if I would have made all these changes.

enter image description here


So, shouldn't we stash anything when merging? How to avoid such behaviour then?

like image 495
Stéphane Bruckert Avatar asked Oct 01 '22 09:10

Stéphane Bruckert


1 Answers

When you do a merge, git stores a reference to the branch you're merging in MERGE_HEAD. But it seems like git stash doesn't save the reference.

You can try to set the MERGE_HEAD back to the commit which you were merging after applying the stash:

Suppose you're merging release-2013-11-06 branch onto master, and the last commit there is - 3be2c99, then you can do:

git stash apply --index
git update-ref MERGE_HEAD 3be2c99
git status
# All conflicts fixed but you're still merging
# ....

Notice the usage of --index while applying the stash. That is required to get the changes back in the index, else you will get the stashed changes only in the working tree.

P.S: Ideally you should avoid stashing your uncommitted merge.

like image 119
Rohit Jain Avatar answered Oct 11 '22 08:10

Rohit Jain