gitgit-rebase

Merging divergent branches without rebase


I have the following git situation:

I was working on a branch, say profiles taken from master. The profiles branch has the profiles feature.

While working I commit a million times every small change. So when it was time to merge, the team decided that we take a new branch off of profiles, say profiles_to_merge, where the commits would be squashed into a single commit. profiles_to_merge was merged into master.

So far so good.

Now while profiles was being reviewed and tested I needed to work on another feature called games. That feature need some of the code included in the profiles PR. So I created a branch off of my profiles branch.

When my profiles branch was reviewed some changes were requested. Those were added to the profiles branch. Then those logically were included in the profiles_to_merge branch. Those changes were not included in the games branch however.

Now when games feature was done, it was to merge this branch. Again I created a new branch off of this called games_to_merge, and I want to squash as well.

When I use git rebase -i master git does not see that some of the changes I already have, and so are introduced as new changes.

I realize I must have taken a several wrong turns here. So my questions are.

  1. Given the current situation, what is the easiest way to squash all the games commits into one. Without conflicts of course. And create a PR of this to master?
  2. Is it correct to take a separate branch for merging purposes only?

Solution

  • Ok.... you need to learn how to use git rebase A B --onto C. This way to use git says something like "git, please, take the commits that make up the history of B, discarding all commits that make up the history of A... place them on top of C".

    So, for you, you could try:

    git rebase profiles games --onto master # this works because stuff from profiles is already in master, right?
    # and then to easily squash you can try
    git checkout -b games_squashed # a new branch
    git reset --soft master
    git commit -m "Squashing games branch into a single commit"