

How to Close a Pull Request - Merge Commit vs Squash vs Rebase on GitHub
source link: https://dev.to/this-is-learning/how-to-close-a-pull-request-merge-commit-vs-squash-vs-rebase-on-github-14pi
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Open Source (13 Part Series)
When Merging a Pull Request on GitHub, you mainly have three options, with a merge commit, squash or rebase.
Is there anything wrong in always doing a merge commit? Well, there isn’t right or wrong here, but considering the other strategies might will likely bring you some benefits. Let me tell you why.
In the video I will go in detail about the three strategies, and I will also do a "live demo" with GitHub stickers and JS pins, if you're curious I'd recommend you to watch the video, otherwise you can find the full text content here below.
Merge Commit
Merge commit is probably the most common as it’s the default option on GitHub and also the default behaviour when you manually use git merge
.
The history of all your commits remains untouched and on the main branch you see an additional commit that merges all your branch content.
The biggest advantage is that you can easily track down the exact commit where a line has been modified, as long as there aren’t too many commits.
The downside is that when multiple commits are on multiple branches, the history quickly become really tangled and following the path of a change can be quite a challenge.
Squash merge
The question is, do you really need to keep track of every single commit? Including typos, missing files, formatting… if the answer is no, then you should consider Squash merge.
When squashing a merge, the result is that you get rid of all commits on the branch and only add a single commit on main with all the content.
As a result, the history is much cleaner and while working on a branch you can do all the commits you want, since you know that with the squash all your sins will be forgotten. Kind of.
Who doesn’t like squash merge, usually brings the fact that you’re losing a lot of commit history, but it’s up to you if you think commits on a branch are valuable when reviewing old activity or rather is just noise.
Rebase
A third way is that you don’t really need a commit that indicates a merge took place, and you still want to end up having all commits in a single, straight line.
Merge commit has all tangled lines plus an extra commit, Squash is a straight line but loses the history so here comes Rebase, which keeps the history AND the straight line AND doesn’t require an extra commit for the merge.
Does this make Rebase the best strategy ever? Well... actually not necessarily, because it has some potentially destructive side effects.
If you get a merge conflict during a rebase, it’s fairly easy to unintentionally lose parts of your code code and if you need to catch up multiple commits you might end up resolving a merge conflict for each single commit, rather than just one in the other merge strategies.
My two cents
After this overview, what do you think? Let me know in the comments!
In my opinion, if branches don’t stay open for a long time, squash merge is a good option, also I’m not a fan of rebase because it’s too easy to make huge mistakes. But before deciding which strategy to apply you should take some considerations.
For example, how often you and your team have to look back at old commits? How many members are in the team? Does your CI need the full commit history?
Think about it, talk with your team, and you can find together the best strategy for your own usecase.
Actually, a good strategy could be subscribing to my YouTube channel if you like my content!
Config
If you want to enforce only some merge strategies on GitHub, head over the settings of your repository and scroll down a little bit.
Here you will find three sections to allow merge commit, squash or rebase in your pull requests.
If you keep only one selected, all PRs in your repo will be merged with that specific strategy.
For the first two, you can also define the default commit message that can take information from the PR or the commits in the branch in case of a squash.
As you can see rebase doesn’t have this option and the reason is simple. As we just seen, rebasing does not create an extra commit for the merge, so there is nothing to do here. The existing commits are simply moved to the head of your branch.
Since you’re already in this page, if you scroll down a little bit more you might also want to check this flag to automatically delete branches after a PR is merged.
Closing
If you have any preference or you want to share your experience, please leave a comment, I'll be really happy to hear from you!
But didn’t we just forget about something important? Like… what happens if the two branches have a conflict? Let me show you how Visual Studio Code can help you resolve the conflict, so that you can merge with your desired strategy.
Hit like ❤️ if you liked this article and have a look here:
Thanks for reading this article, I hope you found it interesting!
I recently launched my Discord server to talk about Open Source and Web Development, feel free to join: https://discord.gg/bqwyEa6We6
Do you like my content? You might consider subscribing to my YouTube channel!
You can find it here:
Feel free to follow me to get notified when new articles are out ;)
Recommend
-
28
git 我在提交中犯了一个错误,我该如何解决? 我的提交历史一团遭,我改如何让它整洁? 如果您有过上述问题,那么这篇文章适合您。这篇文章介绍了一...
-
10
不通过 git rebase 来合并 commit logs. 实在没弄明白 git rebase 为什么会要求我解决我从来没有修改过的文件冲突, 真是莫名其妙. 网上一大堆人深受其害. 大家 rebase 的目的非常简单, 就是要合并 commit logs. 在已经 merge maste...
-
18
原文网址:http://yanhaijing.com/git/2020/09/23/git-rebase-merge/ rebase是git中一个神奇的命令,可以让并...
-
20
聊下git merge --squash 你经常会面临着将dev分支或者很多零散的分支mer...
-
9
Engineering Two years of squash merge Simone Carletti — 24 January 2019 At DNSimple we use pull-requests ever...
-
6
How to Use Git Rebase to Squash a Sequence of Commits to a New Base CommitMay 4th 2021 new story10
-
4
What happens if I use a squash instead of a true merge when performing one of the git tricks?
-
7
10 tips for having a good Git commit and Pull request a.k.a Merge request (+Bonus tip)Android Development Lightning Tips & Tricks
-
4
Published Dec 21, 2021 什么是 Squash Commit Gitlab 上在合并 Merge Request 的时候有一个 Squash Commit 的选项。这个选项其实对应的是 git merge 的命令行参数
-
7
Squash Multiple Git Commit Into One Commit April 18, 2018 in pro...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK