3

I can't make correct branch merge in git. For example, I have two branches:

         C - D - E  my_branch
       /    
A - B -  F - G - K  *master

Now I am on master. After "git merge my_branch" I got at master some like this:

A - B - C - D - F - G - K - E - M  *master

(M - merge commit) But I except this:

A - B - F - G - K - M

It important, because at my_branch I often make not-compilable commits, for myself only; and I don't want to push them into master. How make merge that I want?

Pavel
  • 2,602
  • 1
  • 27
  • 34

2 Answers2

3

I think what you're asking for is a way to make a single commit that appears as if you did all the work in your personal branch in one go. Note that this isn't a "merge commit" in git terminology; it's just a normal commit.

Edit: one thing to note here is that doing this means that git doesn't keep any knowledge of the merge, so it starts getting awkward if you want to continue using your personal branch from where you merged it. It's perfect though for experimental branches that don't get used again after your squashed "merge".

You can do exactly what you're after by using git merge --squash (and then commit the result using git commit). This will give you your desired result of:

A - B - F - G - K - M

What I usually do is manipulate my personal branch first (using git rebase -i master) to adjust the commits so that they appear exactly as I want, and then use git merge to bring them into master. This way I can also do:

A - B - F - G - K - M1 - M2 - M3

where M1, M2 and M3 are a rework of all the work in the personal branch, but in a way that also makes sense from the point of view of the master branch. This is useful since keeping individual commits small and logically divided is handy when using the revision history in the future.

Robie Basak
  • 6,492
  • 2
  • 30
  • 34
0

Use the --squash option to git merge: it'll take your branch, squash it into a single commit, and then merge that in.

mipadi
  • 398,885
  • 90
  • 523
  • 479