git cherry-pick should be your answer here.

Apply the change introduced by an existing commit.

Do not forget to read bdonlan's answer about the consequence of cherry-picking in this post:
"Pull all commits from a branch, push specified commits to another", where:

A-----B------C
 \
  \
   D

becomes:

A-----B------C
 \
  \
   D-----C'

The problem with this commit is that git considers commits to include all history before them

Where C' has a different SHA-1 ID.
Likewise, cherry picking a commit from one branch to another basically involves generating a patch, then applying it, thus losing history that way as well.

This changing of commit IDs breaks git's merging functionality among other things (though if used sparingly there are heuristics that will paper over this).
More importantly though, it ignores functional dependencies - if C actually used a function defined in B, you'll never know.

Answer from VonC on Stack Overflow
๐ŸŒ
GitHub
gist.github.com โ€บ gembin โ€บ e0fd1e176f978ecbf1da5baef26f5b2c
Git merge commits into one ยท GitHub
Git merge commits into one. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
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.
Discussions

How to merge a specific commit in Git - Stack Overflow
I have forked a branch from a repository in GitHub and committed something specific to me. Now I found the original repository had a good feature which was at HEAD. I want to merge it only without previous commits. What should I do? I know how to merge all commits: git branch -b a-good-feature ... 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
Merge Commit, Squash, or Rebase: how do you close Pull Requests on GitHub?
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. More on reddit.com
๐ŸŒ r/opensource
4
21
December 30, 2022
How do I rebase multiple merge commits into a single one?
You can create first a local backup of you branch with git checkout mybranch && git checkout -b mybackup. Then you can do an interactive rebase where you can choose what to do with each of your commits: keep them as they are, squash, drop with git checkout mybranch && git rebase mymasterbranch -i More on reddit.com
๐ŸŒ r/git
30
5
December 13, 2024
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ git โ€บ how-to-merge-commits-in-git
How to Merge Commits in Git? - GeeksforGeeks
July 23, 2025 - Whether youโ€™re squashing commits, using interactive rebase, or performing a fast-forward merge, understanding these techniques will help you keep your repository organized and efficient. By following the steps outlined in this guide, you can confidently manage your commit history and improve your workflow. Comment ยท Git Introduction ยท Git Introduction5 min read ยท Introduction to Github3 min read ยท
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ git-squash-commits
Git Squash Commits โ€“ Squashing the Last N Commits into One Commit
March 22, 2023 - You can also combine multiple commits into one when youโ€™re about to merge branches. This helps clean up the incoming branch of redundant commits. The downside of this approach is that you donโ€™t have much control as you do with rebase. To do this, Iโ€™m going to undo the squashing I did in the last step by running git reset --hard HEAD@{7}. Running git log --oneline again, I can see all the commits once more: I pushed the branch to GitHub again, so I can see those commits there too:
Find elsewhere
๐ŸŒ
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-pull-request-merges
Pull request merges - GitHub Docs
Rebase and merge on GitHub: Always updates the committer information and creates new commit SHAs, whereas git rebase does not change the committer information when the rebase happens on top of an ancestor commit.
๐ŸŒ
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 - Github's interface makes merging in commits versatile to suit your style of maintaining a clean Git history. The three different styles are Create Commit, Squash, and Rebase.
๐ŸŒ
GitHub
docs.github.com โ€บ articles โ€บ about-merge-methods-on-github
About merge methods on GitHub - GitHub Docs
Pull requests with squashed commits are merged using the fast-forward option. To squash and merge pull requests, you must have write permissions in the repository, and the repository must allow squash merging. You can use squash and merge to create a more streamlined Git history in your repository.
๐ŸŒ
GitHub
gist.github.com โ€บ laanwj โ€บ ef8a6dcbb02313442462
Find/show merge commits ยท GitHub
Find/show merge commits. GitHub Gist: instantly share code, notes, and snippets.
๐ŸŒ
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 conflict occurred due to lack of relation between those two branches, so git doesnโ€™t know which change precedes. Let's take a look what will happen on GitHub when we raise a pull request for the feature/7 : There are our expected conflict and two commits to merge.
๐ŸŒ
GitHub
github.blog โ€บ home โ€บ open source โ€บ squash your commits
Squash your commits - The GitHub Blog
July 23, 2024 - For years, the merge button on GitHub has created merge commits (i.e. git merge --no-ff) which retain all of the commits in your branch and interleaves them with commits on the base branch.
๐ŸŒ
Git Tower
git-tower.com โ€บ learn โ€บ git faq โ€บ how to squash commits in git
How to Squash Commits in Git | Learn Version Control with Git
Learn how to squash commits in Git using interactive rebase and merge. Combine multiple commits into one for a cleaner history.
Published ย  5 days ago
๐ŸŒ
Reddit
reddit.com โ€บ r/git โ€บ how do i rebase multiple merge commits into a single one?
r/git on Reddit: How do I rebase multiple merge commits into a single one?
December 13, 2024 - I have this currently How do I rebase the above into this?
  • Basically "squash" multiple merge commits of different branches into one super branch and one final merge commit with all changes?

๐ŸŒ
Reddit
reddit.com โ€บ r/git โ€บ how can i combine multiple pushed commits to a branch into a single commit?
r/git on Reddit: How can I combine multiple pushed commits to a branch into a single commit?
January 13, 2022 -

I was contributing to my first open source repo, and I opened a pull request solving an issue. I had fixed the issue in like 5 or 6 commits, but then the maintainer of that repo somehow squashed all of them into one commit. How can I do that?

The notification that popped up on GitHub said something like

<maintainer> force-pushed the <issue_branch> from 015b7f3 to fcc9fcd

and he said that he "rebased from the main branch"? What exactly did he do and how can I do that?

๐ŸŒ
Thoughtbot
thoughtbot.com โ€บ blog โ€บ github-pull-request-merge-strategies
GitHub pull request merge strategies
November 13, 2023 - Teams that choose this workflow may decide what happens when pressing the โ€œMergeโ€ button in the last step. Having used all of the currently available options on GitHub (and Gitlab, and Bitbucket) on different projects, Iโ€™d like to share arguments in favour of each one. This one merges all of the commits into the base branch.
๐ŸŒ
Internal Pointers
internalpointers.com โ€บ post โ€บ squash-commits-into-one-git
Squash commits into one with Git - Internal Pointers
November 17, 2017 - A downside of the git rebase --interactive HEAD~[N] command is that you have to guess the exact number of commits, by counting them one by one. Luckily, there is another way: ... Where [commit-hash] is the hash of the commit just before the ...
๐ŸŒ
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.