You can merge pull requests by retaining all the commits in a feature branch, squashing all commits into a single commit, or by rebasing individual commits from the head branch onto the base branch.

When you click the default Merge pull request option on your pull request on GitHub, all your commits from the feature branch are added to the base branch in a merge commit.

standard-merge-commit-diagram

You can also squash and merge or rebase and merge your pull request commits on GitHub.

Note: If you don't see the squash and merge or rebase and merge option in your repository, ensure that:

  • you've clicked the merge button drop down menu to see other merge options
  • you have write permissions in your repository to merge pull requests
  • a repository administrator has not disabled these merge options for all pull requests in your repository. For more information, see "About merge methods on GitHub."

Squash and merge your pull request commits

You can also squash the pull request's commits 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 once they're merged into the default branch, creating a more streamlined Git history. Pull requests with squashed commits are merged using the fast-forward option.

commit-squashing-diagram

Work-in-progress commits are helpful when working on a feature branch, but they aren’t necessarily important to retain in Git history. For example, these commits on a feature branch describe the same intended action:

git log
commit 342aa5ae7f24c911c4f082f376eef7fb9f26b852
Author: octocat 
Date:   Tue Sep 20 16:44:38 2016 -0700

 Finally fix the backport test

commit 32273764bc2cff3f576c114251115ebb3916bbf7
Author: octocat 
Date:   Tue Sep 20 16:04:18 2016 -0700

 Maybe fix that test?

commit 18686eae87dce534027af96a904a7bf900bb4885
Author: octocat 
Date:   Tue Sep 20 16:03:51 2016 -0700

 Oops! Fix typo.

If you squash these commits into one commit upon merging to the default branch, you can retain the original changes with a clear Git history.

git log
commit a356ad4a123895edfa2668e6e928dfee8ab22883
Author: octocat 
Date:   Tue Sep 20 17:04:38 2016 -0700

 Fix the backport test

Rebase and merge your pull request commits

You can also rebase your pull request's commits, which adds all commits from your topic branch (or head branch) onto the base branch individually without a merge commit. Pull requests with rebased commits are merged using the fast-forward option.

For a visual representation of git rebase, see The "Git Branching - Rebasing" chapter from the Pro Git book.

When you rebase your pull request's commits you simplify the history of your changes in Git. For more information about rebasing pull request commits, see the "Git rebase" chapter from the Pro Git book.

You aren't able to automatically rebase and merge on GitHub when:

  • Your pull request has merge conflicts.
  • Rebasing the commits from the base branch into the head branch runs into conflicts.
  • Rebasing your commits is considered "unsafe", such as when a rebase is possible without merge conflicts but would produce a different result than a merge would.

If you still want to rebase your commits but can't rebase and merge automatically on GitHub you must:

Anyone with write access to the repository, can then merge your changes using the rebase and merge button on GitHub.

Further reading