Try   HackMD

Squash Merge v.s. Merge

tags: git

優點

在 github 上使用,每個 commit 可以直接看到屬於哪個 PR。

缺點

  • 如果刪掉開發的 branch,離開 github 後,就難以 trace 每個 branch 內的開發過程。

    不要離開 github 就好,讚讚讚

  • 需要 rebase,如果沒有正確 rebase 容易 conflict。

    正確使用應該就不算缺點?

Conflict 狀況

有 a, b, c 三個在開發中的 branch:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

假設:

  • a 先完成,merge 進 master。
  • c 完成, merge 進 master。
  • b 想要在有 c.txt 的狀況下繼續開發,所以要 merge master。

用 merge

merge 完 a, c 後 checkout 到 b 的狀態:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這時 git merge master 就很順利,不會有任何 conflict,直接 merge 完成:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

用 squash merge

merge 完 a, c 後 checkout 到 b 的狀態:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

這時 git merge master 就會產生 conflict:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

解法

merge 完 a, c 後,b 先 rebase master 上 squash merge a 的 commit

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

再 merge master

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →