Home > OS >  Rebase or squash to "hide" already-merged commits on nested branch?
Rebase or squash to "hide" already-merged commits on nested branch?

Time:09-24

I can do basic git commands, but still run into trouble when I work on nested branches (instead of only 1 level away from main). I have an issue which I haven't found answered in other S.O. answers: my 2nd level branch has all 20 commits from my 1st level branch in its history, when what I want to push (for ease of review) are only the 3 new commits since checkout.

I'm having a hard time understanding how to do this - it sounds ALMOST like rebase but not quite (rebase would give the unneeded history, right?). git diagram

CodePudding user response:

Most likely the last commit of big-feature shown in your picture is not the same commit ID in both main and style-edits. (Update: I think this is now confirmed in the comments, due to a "basic" rebase instead of a "fancy" rebase using --onto.) Given that, then the fix is to use a "fancy" rebase:

git fetch
git checkout style-edits
git rebase --onto origin/main style-edits~3
git push --force-with-lease

That series of commands will rebuild your style-edits branch to have just those 3 commits off of the latest origin/main.

CodePudding user response:

If it is as you say, the PR should work. Git should recognize that style shares all the commits of feature. You can check with git log main..style; all the commits reachable from style except those reachable from main. That's just the three commits in style.

From what you're saying is happening, that isn't not the case. Likely at some point feature got rebased onto master.

You had this.

A [main]
 \              
  B - C - D - E [feature]
               \
                1 - 2 - 3 [style]

Then at some point some commits were made to main, let's call them X, Y, Z.

A - X - Y - Z [main]
 \              
  B - C - D - E [feature]
               \
                1 - 2 - 3 [style]

And feature was rebased onto main to get the updates.

$ git switch feature
$ git rebase main

              B1 - C1 - D1 - E1 [feature]
             /
A - X - Y - Z [main]
 \              
  B - C - D - E
               \
                1 - 2 - 3 [style]

This wrote a new set of commits for feature on top of main, but style is left behind on the old feature commits.

Then you merged feature into main.

              B1 - C1 - D1 - E1
             /                 \
A - X - Y - Z ----------------- M [main]
 \              
  B - C - D - E
               \
                1 - 2 - 3 [style]

Now when you try to make a PR for style it resurrects those old pre-rebase feature commits.


To fix this, rebase style onto main. You only want the last three commits, so style~3.

$ git switch style
$ git rebase --onto main style~3

              B1 - C1 - D1 - E1
             /                 \
A - X - Y - Z ----------------- M [main]
                                 \
                                  1A - 2A - 3A [style]

Now you can merge style.

  • Related