You should read Github documentation: when you merge a pull request, it explains the differences between the choices:
Merge all of the commits into the base branch by clicking Merge pull request. If the Merge pull request option is not shown, click the
merge dropdown menu and select Create a merge commit.
Squash the commits into one commit by clicking the merge dropdown menu, selecting Squash and merge and then clicking Squash and merge.
Rebase the commits individually onto the base branch by clicking the merge dropdown menu, selecting Rebase and merge and then clicking
Rebase and merge.
The git operations done by GitHub should be:
git merge target
git rebase -i target
+ squash all except first commit (there is probably an option in git for that, otherwise it is custom made by GitHub) then git merge target
(HEAD = branch with squashed commits)
git rebase target
then git merge target
The three GitHub option all boil down to what you want to see in the history.
In most case, the last option is best because it minimize history by removing redundant merge commit (since the default mode of git pull
is to merge, so a pull request may contains merge with its target branch).
If you want to perform the same locally, you would avoid git merge
(even if git pull
will do it by default, except with --no-ff
):
- you have to squash before fetching or you have to know your ancestor commit. I personally ignore this and do an interactive rebase with
git rebase -i HEAD~10
. Then I carefully "fix" (= squash, but without keeping the commit message) the commit after the one(s) I want to keep.
- after interactive rebase, you can rebase with
upstream/develop
: if a commit bring a conflict onto a file, then any remaining commit touching that file may also bring a conflict. It is normal even if git probably tries to minimize that.
- then you can
git push -f
after performing some check (compiling, testing, ...).
You could also do a git merge
rather than git rebase
, but you will have merge commit in your branch which I think should be avoided as much as possible because it clobber the history.