Search code examples
gitvisual-studio-codegithubgit-mergegit-rebase

Why does local git merge share the same commit history as a branch but remote pull request does not?


TL;DR

My question is: how do I get my local git merge behavior to resemble the way it works when I do a pull request from a branch back to main in GitHub, i.e. showing a branched commit history rather than a rebase-like history? I don't have it set to automatically do anything with rebase from what I can tell -- see details below.


Here's the scenario:

  1. The main branch is clean.
  2. I perform git checkout -b test-branch.
  3. I make a change to README.md; check in, publish branch to origin.
  4. Another change to README.md; check in; repeat two more times (3 total local commits), push to origin. Here's what git graph shows:

    enter image description here
  5. Switch to main; merge test-branch from VS code menu:

    enter image description here

    Then push to origin. Here's what git graph shows now:

    enter image description here

    The result looks like a single line of commits as if it did a rebase (which I don't want).
  6. Now I repeat steps 1 through 4 creating a new branch test-branch1. Here's what git graph shows:

    enter image description here
  7. Now, instead of merging the branches locally, I perform a pull request from the branch in GitHub:

    enter image description here
  8. Locally, I switch to main and pull from remote. Here's what git graph now shows:

    enter image description here

    Now instead of looking like a rebase happened, it has a separate history.

My user gitconfig just has my name and email address, and here is what my repo git config looks like (i.e. no rebase setting from what I can tell):

enter image description here

So again, my question is how do I get my local merge behavior to resemble the way it works when I do a pull request from a branch back to main in GitHub, i.e. maintaining the separate commit history from the branch?


Solution

  • --no-ff flag in git prevents the execution of a "fast-forward" merge and it's the merge method that GitHub uses in pull requests. - reference.

    git merge <branch-name> --no-ff
    git pull <remote-name> <branch-name> --no-ff
    

    enter image description here

    You might consider --ff-only flag as well.