Home > Mobile >  tried to stash using branch, how to restore without merging?
tried to stash using branch, how to restore without merging?

Time:08-27

I had some temporary changes which I needed to stash so I could switch between branches without losing them. I know this is what git stash is for, but I didn't want to use it (details below for the curious), so I thought, why not just commit my temporary changes to their own branch? And that worked fine, no problem there.

The problem came after I tried to "unstash" my changes from that branch. I had assumed I'd just do a merge, but I realized that would do a commit, which I don't want to do; the temporary changes are still temporary and won't be part of the next commit I'm about to prepare.

I discovered that git merge has a --no-commit option which seemed like just the ticket. That should merge the changes in, without doing the final commit, which is just what I want.

But, I've just discovered, those merged changes are now in the index, meaning they'll go into my next commit anyway, unless I can get them cleanly out of the index. Furthermore, git status says the merge is still in progress, and I don't want that merge I did to look like a merge; I want to pretend it never happened, although not by doing git merge --abort, since I do want the (partial) merge's changes to continue to be reflected in my working tree.

So my questions are, how do I get myself out of this little mess, and how do I avoid getting into this mess next time?

I think I might be able to get out of my mess by doing git reset --mixed, but my understanding of git reset is still imperfect, and of course reset is always scary, and I really don't want to screw up again and make things worse. Also I'm not at all sure that git reset will reset the notion of the current merge-in-progress.

For next time, it looks like a better way to "merge" my stashed-on-a-branch changes might have been to do git merge --squash, but I'm even less sure of that, as I'd previously convinced myself (in another context) that the squash option was one I'd never want to use at all.

(So why didn't I just use git stash to save those uncheckedin changes in the first place? Partly because I don't understand how git stash manages its stashes, and I decided I didn't want to take time to learn that right now. Also I may want these changes on another development machine soon, so committing them to a branch and then pushing that branch was attractive.)

(Also, a side question: What state bit, stored where, records the fact that there's a merge in progress? I'd rather use a "real" git command to reset it; I'd rather not manipulate that bit by hand, but I've been wondering where it is.)

CodePudding user response:

To get out of the state (if I understand it correctly) is to use

git reset

This keeps the changes, unstages them, and removes the merge-in-progress state. (This state is identified by the presence of the file .git/MERGE_HEAD.)

And, yes,

git merge --squash --no-commit temp-branch

would have been the command to "unstash" the temporary changes from the temporary commit.

  • Related