1

I have two branches that I'm working on: dev, and test. I made some changes in test, and wanted to merge those into dev. However, I only wanted to merge SOME changes to dev, not the whole diff.

I could not cherry-pick commits, because that would have been too many.
What I did instead, was to checkout on dev, then merge test locally. I then staged and committed only the changes that I wanted to merge. The others, I discarded (which did not affect the test branch).

Now I am ready to merge the other changes (that I previously discarded on local dev, but which are still in test) from test into dev. When I checkout on dev again and then try to merge test into dev, it says "Already up to date".
I checked the source code: when checking out on test, I see the method I've added. When switching to dev instead, the method is not there (because it's not merged yet). When I switch back to test, the changes are there again.

How is it possible that the merge does not include all changes I made?


Edit

I followed @Ôrel's advice from the comments and merged dev → test, then test → dev and now the previously discarded changes are gone from both dev and test. How do I restore them?

LinusGeffarth
  • 27,197
  • 29
  • 120
  • 174

1 Answers1

1

What I did instead, was to checkout on dev, then merge test locally. I then staged and committed only the changes that I wanted to merge.

The idea would be, after staging, and before committing, to stash the rest (the part not staged).
See "Stashing only un-staged changes in Git"

git stash save --keep-index

That way, when you are "ready to merge the other changes (that I previously discarded on local dev, but which are still in test)", instead of merging again (which was already done), you stash pop it.
That will apply all those other changes to your dev branch.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • This worked great. Just to understand what's going on: 1) I checkout on `dev`. 2) I merge `test → dev`. 3) I stashed everything (b/c I didn't read closely on how to stash only un-staged changes ), which effectively canceled the merge (right?). 4) I applied the stash and discard all changes I did not want, but kept the stash so I can apply those later. ––– Is this correct? Or am I getting anything wrong here? – LinusGeffarth Feb 01 '20 at 00:13
  • @LinusGeffarth 3) was the equivalent of a `merge --ours`: "records the merge, but do not actually merge anything, keep the destination unchanged" – VonC Feb 01 '20 at 00:21
  • @LinusGeffarth 4) is OK, the only "non-optimal" part if that, by applying the stash "later", you will apply *again* some changes that you already applied before. No big deal. – VonC Feb 01 '20 at 00:23
  • I see. My graph doesn't show a merge from `test → dev` though... Regarding 4), the changes are in the stash, but won't be applied, b/c they have already been applied earlier, right? – LinusGeffarth Feb 01 '20 at 00:25
  • @LinusGeffarth Part of the stash won't be applied indeed. And you don't see a merge because you cancel it with the full stash before. You can do it at any moment though with `git merge --ours`: that will record a merge between two branches without actually merging anything. – VonC Feb 01 '20 at 00:33
  • Aha, I get that now. Thanks! – LinusGeffarth Feb 01 '20 at 00:40