Always commit work as soon as possible, even if you're in the middle of something and don't have working product yet. always make separate branches for separate changes, and keep the change in each branch relatively small. Create a MR as soon as your change works and the pipeline (including testing, linting, etc) works. If you follow the above strategies, i think squash commits really are the best option. You don't clutter your history with loads of small "low information" commits, you dont end up seeing a line of code first entered in a commit only to realize it was 5 commits later when the code actually ran succesfully. Instead, you have a clear commit where the new functionality was added, and that commit contains working code and unit tests. In fact, if your tasks are defined small and clear enough, any time you have a working addition to your codebase, that should probably be exactly the moment your task is done and you want to start a MR(excluding unit tests, documentation and linting). So in theory all the commits you 'cut out' are only commits that don't have a working product in them anyway. Of course, if your assumptions dont work, and you have longer lived branches or big MRs with multiple/bigger features, or with multiple people adding multiple different things to one branch, you'll lose a lot of info with squash commit. Answer from asphias on reddit.com
🌐
Reddit
reddit.com › r/opensource › merge commit, squash, or rebase: how do you close pull requests on github?
r/opensource on Reddit: Merge Commit, Squash, or Rebase: how do you close Pull Requests on GitHub?
December 30, 2022 -

When Merging a Pull Request on GitHub, you mainly have three options: merge commit, squash or rebase.

Is there anything wrong with always doing a merge commit? Well, there isn’t right or wrong here, but considering the other strategies will likely bring you some benefits.

I made a video about it: https://www.youtube.com/watch?v=rFRtsiQEJZw

In the video, I will go into 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 behavior 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 becomes 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 up 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 on multiple commits you might end up resolving a merge conflict for every 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 do you and your team have to look back at old commits? How many members are on 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 use case.

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 to 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 saw, 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 on 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!

With that said, happy new year, may it bring you a lot of merged PRs and no merge conflicts!

Top answer
1 of 3
3
Always commit work as soon as possible, even if you're in the middle of something and don't have working product yet. always make separate branches for separate changes, and keep the change in each branch relatively small. Create a MR as soon as your change works and the pipeline (including testing, linting, etc) works. If you follow the above strategies, i think squash commits really are the best option. You don't clutter your history with loads of small "low information" commits, you dont end up seeing a line of code first entered in a commit only to realize it was 5 commits later when the code actually ran succesfully. Instead, you have a clear commit where the new functionality was added, and that commit contains working code and unit tests. In fact, if your tasks are defined small and clear enough, any time you have a working addition to your codebase, that should probably be exactly the moment your task is done and you want to start a MR(excluding unit tests, documentation and linting). So in theory all the commits you 'cut out' are only commits that don't have a working product in them anyway. Of course, if your assumptions dont work, and you have longer lived branches or big MRs with multiple/bigger features, or with multiple people adding multiple different things to one branch, you'll lose a lot of info with squash commit.
2 of 3
1
Most of the time, I would recommend using merge commit when merging PRs. This should be the default flow that you use with most PRs. Except when the contributors makes a complete mess of the history, such as they clicked on sync with master/main two dozens times for what should've been half dozen commit change, so that there's more merge commits than there is actual content commits, or they were committing two dozens commit for a one line changes, then I would rebase/squash them, because those are unnecessary complexity that I don't have to deal with. If you're contributing to projects, please don't re-sync the PR branch to master unless there's a merge conflict, or you're asked to do that by the maintainer. Each re-sync creates an unnecessary merge commit, which clutters the history. If you just want to re-run the CI, the maintainer will do that themselves when they're looking at the PR if they think it's necessary. I don't really want to rebase your work, because it strips off any GPG signatures you may have on the commit, and it'll likely screw your local branches as well. But if your git history looks like a completely avoidable dog's breakfast, then I'll have to do that.
🌐
GitHub
docs.github.com › articles › about-merge-methods-on-github
About merge methods on GitHub - GitHub Docs
When you select the Squash and merge option on a pull request, the pull request's commits are squashed into a single commit. Instead of seeing all of a contributor's individual commits from a topic branch, the commits are combined into one commit ...
Discussions

git: what exactly is a merge commit in git? - Stack Overflow
I went through couple of blogs to understand a merge commit, but not completely clear as to what it is and what is the best practice to avoid it. And the tree diagram of the git log added further More on stackoverflow.com
🌐 stackoverflow.com
git - Squash all my commits into one for GitHub pull request - Stack Overflow
Squash-merging can be easily done in many GIT-applications, such as Github Desktop. The only downside is that now you have two branches, but if you don't need to keep your original commits, you can safely delete the original one after squash-merging into the new one. More on stackoverflow.com
🌐 stackoverflow.com
Should You Squash Merge or Merge Commit?
I always squash the commits since I am usually not working continuously on one task and also use git as a backup copy of my work. So if I switch tasks or make a break I tend to commit my changes. This leads to a dirty commit history so I squash it to keep it clean in the main branch. I also do not like to have commits like "implemented review feedback" or "renamed variable x" which might result from code reviews in the main branch. Where I work it basically is default to commit at any point you are stopping the work on that task so others who might need to continue your work can pull your recent changes. More on reddit.com
🌐 r/programming
72
155
August 6, 2022
Selecting the Right Git Merging Strategy: Merge Commit, Squash and Merge, or Rebase and Merge
I dislike Rebase and merge just because the Feature branches has commits like “update code” I like linear small amount of full work done squashed into one commit and then merged into a develop or main branch to know what work is done More on reddit.com
🌐 r/programming
36
15
September 25, 2023
🌐
GitHub
docs.github.com › en › repositories › configuring-branches-and-merges-in-your-repository › configuring-pull-request-merges › configuring-commit-merging-for-pull-requests
Configuring commit merging for pull requests - GitHub Docs
On GitHub, navigate to the main page of the repository. Under your repository name, click Settings. If you cannot see the "Settings" tab, select the dropdown menu, then click Settings. Under "Pull Requests", select Allow merge commits.
🌐
GitHub
github.com › orgs › community › discussions › 16271
How to change the default “Squash & Merge” commit message? · community · Discussion #16271
Migrated from https://github.community/t/how-to-change-the-default-squash-merge-commit-message/1155/2 When “Squash & Merge” a branch in a PR, the default commit message is either: the commit messag...
🌐
DEV Community
dev.to › zdybit › when-to-use-particular-options-to-close-pull-requests-on-github-3ce8
Merge, squash & rebase on GitHub - pros & cons - DEV Community
March 20, 2023 - The second thing, we can squash features into the main, or rebase them if we would like to keep multiple commits of them. We've just learnt more about the outcomes of the options to close pull requests on GitHub, and what consequences they raise. Now, we can handle our pull requests more effectively and wisely. We are not afraid of other options than Create a merge commit anymore.
Find elsewhere
🌐
GitHub
docs.github.com › articles › about-pull-request-merges
Pull request merges - GitHub Docs
When you select the Squash and merge option on a pull request, the pull request's commits are squashed into a single commit. Instead of seeing all of a contributor's individual commits from a topic branch, the commits are combined into one commit ...
🌐
GitHub
github.com › orgs › community › discussions › 5955
Merge commit message template · community · Discussion #5955
Having the PR description go into commit messages is extremely valuable for searchability and portability. I can't grep the log from GitHub and I enjoy being able to read the most complete log I can from wherever my repository is: be it GitHub, local git or GitLab. I use a strictly merge commits workflow on some projects, strictly squash commit on others.
🌐
Brandon Pugh's Blog
brandonpugh.com › posts › tips for creating merge commits
Tips for creating merge commits | Brandon Pugh's Blog
August 31, 2024 - Merge commits that merge a pull request are more straight forward because, these days, they’re generally created by the git hosting service so you know they only contain all the changes from that feature branch. But you can still make sure they convey the most useful info. I like the default from Azure DevOps: ... You can get a similar default in GitHub by configuring the default merge commit settings in the repository settings.
🌐
Luke Merrett
lukemerrett.com › different-merge-types-in-git
Different Merge Types in Git
August 7, 2021 - Lost of granularity, any useful detail in those commits that made up the branch is lost, as are any interesting decisions, changes in logic etc captured during the development process. A rebase and merge will take where the branch was created and move that point to the last commit into the base branch, then reapply the commits on top of those changes.
🌐
Git
git-scm.com › docs › git-merge
Git - git-merge Documentation
Incorporates changes from the named commits (since the time their histories diverged from the current branch) into the current branch. This command is used by git pull to incorporate changes from another repository and can be used by hand to merge changes from one branch into another.
🌐
GitHub
docs.github.com › articles › merging-a-pull-request
Merging a pull request - GitHub Docs
If the option is not shown, click the merge dropdown menu and select Create a merge commit.
🌐
GitHub
github.com › orgs › community › discussions › 20583
Default merge strategy (per org, repo or branch) · community · Discussion #20583
In our GitHub organization we have a workflow where the preferred merge strategy is Squash and merge. Whenever we merge into master, we usually use this strategy. We would like to be able to set this as the default merge strategy, without removing the options for Create a merge commit and Rebase and merge for special occasions.
🌐
Matt Stauffer
mattstauffer.com › blog › how-to-merge-only-specific-commits-from-a-pull-request
How to merge only specific commits from a pull request with git cherry-pick | MattStauffer.com
Get back into the branch you're merging into. You'll likely do this by running git checkout master. Find the commits you want to pull into your branch. Go to either the git log or the GitHub UI and grab the unique commit hashes for each of the commits that you want.
🌐
Graphite
graphite.com › blog › pull-request-merge-strategy
What's the best GitHub pull request merge strategy?
February 27, 2024 - The merge commit strategy is best for teams that want to retain a complete record of all changes and commits related to a feature. The “squash and merge” option on GitHub condenses all the commits from your branch into a single combined commit.
🌐
Atlassian
atlassian.com › git › tutorials › using branches › git merge
Git Merge | Atlassian Git Tutorial
Git merge will combine multiple sequences of commits into one unified history. In the most frequent use cases, git merge is used to combine two branches. The following examples in this document will focus on this branch merging pattern.
🌐
GitHub
docs.github.com › en › repositories › configuring-branches-and-merges-in-your-repository › configuring-pull-request-merges
Configuring pull request merges - GitHub Docs
You can enforce, allow, or disable merging with a merge commit for all pull request merges on GitHub.com in your repository.
🌐
GitHub
github.com › MarcBoissonneault › github-merge-strategies
GitHub - MarcBoissonneault/github-merge-strategies: GitHub Merge strategies explained by examples · GitHub
Merge commits strategy adds all commits from the branch to the base branch with a merge commit.
Starred by 23 users
Forked by 5 users
🌐
Rietta Inc.
rietta.com › 🏡 home › blog › what's the difference between the 3 github merge methods?
What's the Difference Between the 3 Github Merge Methods?
June 3, 2024 - We can apply this way of thinking ... Merge pull request on Github will take all commits from the pull request and add them to the master branch with a new commit in a merge commit....