philosofool
philosofool

Reputation: 973

Best way to merge after base branch was squashed into main branch

I have a minor git issue that keeps coming up. I have a feature that's in a PR, and then another feature that is dependent on it.The base for the second feature is squash merged. The second feature now contains commits that are the original.

             /C
       A - B
master/

Becomes:

      A - B -C
    /   
master - D

where D is the squashed A and B.

What's the best way to merge C into master at this point, while droping A and B. I have been doing git rebase -i master and then manually dropping the commits A and B, and then creating a new branch, but this is messy. People havea already reviewed C and we lose the discussion history.

Upvotes: -1

Views: 87

Answers (1)

TTT
TTT

Reputation: 29129

The problem with your rebase command, as you realized, is that you are including commits in the list that no longer exist. There is an easier way to remove them than by using interactive mode:

git fetch # this updates your local copy of origin/master
git rebase B --onto origin/master

In words, that says:

Rebase all of the commits reachable by my branch, which are not reachable by B, and replay them, one by one, on top of the latest remote copy of master.

Essentially it's all your commits minus B, which leaves just C.

Tip: The reason this happens is the combination of starting from a commit that is not yet on master, and also squashing that commit into master instead of using a regular merge. If this pattern happens often, which it sounds like it does, it may help to keep track of the commit you are starting from in a simple way. Here are two ideas for tracking it:

  1. After creating your branch: add an empty commit as the first commit on your branch: git commit --allow-empty -m "wip: starting here".
  2. Include the starting commit ID in the branch name, for example: git switch -c my-feature-start-commit-abcd1234 some-other-branch-name

When it comes time to do your advanced rebase with --onto, the first argument to the rebase (represented by commit B in the question) could be quickly substituted with the commit ID of the wip commit in #1, or the commit ID specified in your branch name from #2.

Upvotes: 1

Related Questions