0

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?). Here's a similar question, but different in that I didn't squash anything...

Full description of what I did:

  1. checkout new local branch ‘big feature’ off main
  2. Make 20 commits to ‘big feature’
  3. checkout new local branch ‘style edits’ off big feature
  4. Make 3 commits to ‘style edits’
  5. PR & merge ‘big feature’ into remote main
  6. Attempt to PR & merge ‘style edits’ into main, but issue: ‘style edits’ contains 23 commits

Diagram shows the unnecessary 'style edits' commits that now already exist on main - how do I get rid of those and only push the 3 new commits?

git diagram

ezeYaniv
  • 412
  • 5
  • 18
  • 1
    What's the greyed out line in "Style Edits" supposed to be? – Schwern Sep 23 '21 at 22:52
  • That is the redundant commit history that the 'style edits' branch copied from 'big feature' branch – ezeYaniv Sep 23 '21 at 22:55
  • And when you tried to do a PR of Style Edits into main you got 23 commits, not 3? – Schwern Sep 23 '21 at 22:55
  • Yup exactly. It seems I need to catch up 'style edits' or somehow remove those 20 commits – ezeYaniv Sep 23 '21 at 22:57
  • What does `git log --oneline main..style_edits` say? 3 commits or 23? – Schwern Sep 23 '21 at 22:57
  • 1
    Check your true history with `git log --graph --decorate --all` or a tool such as GitX. Are you *sure* you didn't squash or rebase Big Feature? – Schwern Sep 23 '21 at 22:58
  • `git log --oneline main..style_edits` says 3 commits. – ezeYaniv Sep 23 '21 at 23:00
  • So I rebased earlier as a friend suggested that would help catch up the branch to main. BUT the issue started before I rebased - that was his suggestion to fix it. – ezeYaniv Sep 23 '21 at 23:01
  • Can you do `git fetch` and then do that command again with origin: `git log --oneline origin/main..style_edits` – TTT Sep 23 '21 at 23:06
  • It is also showing only the 3 new commits I made – ezeYaniv Sep 23 '21 at 23:12
  • Is your remote branch up to date with your local? git status doesn't have differences? And the PR of your branch still has 23 commits? – TTT Sep 23 '21 at 23:55
  • The PR now is down to 4 commits! Still not sure how exactly - the only thing that changed was I just merged origin/main and pushed back up to origin/styleedits. I guess git finally behaved as expected and recognized all the shared commits and ignored them as @Schwern said in their answer. – ezeYaniv Sep 24 '21 at 00:10
  • 1
    @ezeYaniv `git log --graph --decorate --all --oneline` will show you the true history of your repo. If we saw that we could explain what's going on. – Schwern Sep 24 '21 at 02:38

2 Answers2

1

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 possibly confirmed in the comments, due to a "basic" rebase onto master instead of a "fancy" rebase using --onto, which would explain this if that happened before big-feature's PR was completed into main.) Regardless of how the commits changed, 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.

TTT
  • 22,611
  • 8
  • 63
  • 69
0

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.

Schwern
  • 153,029
  • 25
  • 195
  • 336