GitHub Merge strategies explained by examples
I used to had a lot of discussion around the right pull request merge strategy to adopt.
I had a hard time to understand the exact behavior while looking at these github explainations of the merge strategies:
So I decided to experiment the 3 possibilities by myself in this repo!
For each of the 3 merge strategy proposed by github I repeated the following steps:
1- Create a branch with the name of the strategy into it.
here are the 3 branches I've created:
- merge-strategy-branch
- squash-merging-strategy-branch
- rebase-merging-strategy-branch
2- Create 3 commits
- 1: Add a small text file in the Specific Strategy folder (eg:
merge-strategy/file1.txt
) - 2: Add some text to the file
- 3: Complete the sentence on the file
3- Create a PR to merge the branch on master
4- Merge the PR with the strategy mentionned on the branch name.
Merge commits strategy adds all commits from the branch to the base branch with a merge commit.
Pros | Cons |
---|---|
|
|
Squash merging strategy combines all commits from the branch into a single commit in the base branch.
Note: it the same as doing
git checkout master
git pull
git --squash merge branch
Pros | Cons |
---|---|
|
|
Rebase merging recommit all commits from the branch onto the base branch individually.
Pros | Cons |
---|---|
|
|
Note:
The rebase and merge behavior on GitHub deviates slightly from git rebase.Rebase and merge on GitHub will always update the committer information and create new commit SHAs, whereas git rebase outside of GitHub does not change the committer information when the rebase happens on top of an ancestor commit
An issue is open on GitHub since we are losing commits signatures & verifications.
If your master branch rules prevent you from creating a not-signed commit, this strategy cannot be used
This table shows a summary of all the pros and cons of each strategies
source: https://medium.com/@elliotchance/comparison-of-merging-strategies-in-github-2f948c3b8fdc
- Commit early and often.
- Small pieces mean easier code reviews, unit testing and better alignment to Single Responsibility Principle.
- As much as you can, try to keep a clear commit history
- Fixup/squash useless commits (typo, fix linting issues or import sorting)
- Write meaningful messages (title, details and a link to the originating issue)
- Make sure your branch is up to date with the master before creating and merging a PR (No mather the merge strategy used)
- The simplest way to do so is to rebase your branch on master before creating PR and before merging the PR. (Obviously, if the merge strategy used is to Rebase and merge, this procedure is useless.)
- This ensures every merge commit is working and not breaking the build (no failing tests, no conflicting migrations)
There is not "right" choice for the merging strategy. It's up to you to decide which one to use and to understand the pros and cons of the selected strategy.