Github “Squash and Merge” - subsequent pull request showing all previous changes

后端 未结 2 1312
忘掉有多难
忘掉有多难 2021-02-20 05:14

I have a branch which I pulled from master branch. Let\'s call it integration.

In the integration branch, I made various commits (C1, C2, C3). When I am done, I made a

2条回答
  •  醉梦人生
    2021-02-20 06:19

    It's easier to understand visually. Here's your repo. master is at commit B, and your feature branch is at commit C3.

    A - B [master]
         \
          C1 - C2 - C3 [feature]
    

    A normal merge does this. A new merge commit, BC123, is added combining the content in master with those in feature. The histories are linked together. Note that feature does not move, it's still at C3.

    A - B ------------- BC123 [master]
         \            /
          C1 - C2 - C3 [feature]
    

    A squash and merge does this.

    A - B ------------- BC123 [master]
         \
          C1 - C2 - C3 [feature]
    

    BC123 contains the same merged content as before, but there's no connection to the feature branch. And again, feature does not change. feature does not get squashed, it sticks around. Instead BC123 contains the squashed changes from feature.

    When you did more work on feature, commits C4 and C5 here, this happened.

    A - B ------------- BC123 [master]
         \
          C1 - C2 - C3 - C4 - C5 [feature]
    

    And when you issue a pull request, all the changes in feature not in master will appear. As far as Git is concerned that's C1 to C5. If you were to squash & merge again, there would be a new commit on master, but only with the content of C4 and C5 because Git is pretty good at figuring out duplicate content between branches.

    A - B ------------- BC123 - C45 [master]
         \
          C1 - C2 - C3 - C4 - C5 [feature]
    

    While you can work this way, it's confusing.

    Long story short: once you merge a branch, don't work on it anymore. Delete it. If you need to do more work open a new branch.

    This issue is not seen if I use the default "Create an Merge commit" in my earlier commit.

    Going back to the merged version...

    A - B ------------- BC123 [master]
         \            /
          C1 - C2 - C3 [feature]
    

    If you do more work on feature...

    A - B ------------- BC123 [master]
         \            /
          C1 - C2 - C3 - C4 - C5 [feature]
    

    And then do a pull request, Git will show you the commits in feature which are not in master. Because of the merge, master contains C1, C2, and C3. So the PR only shows you C4 and C5. This is still confusing, and the same advice applies: once you merge a branch, delete it. Open another one if you need to do more work.

    While squash & merge is simpler, merging (done right) provides a healthier history and more information for Git to work from. You'll get more out of Git if you understand how branching and merging works.

提交回复
热议问题