Make git pull –rebase preserve merge commits

git pull --rebase removes unpushed merge commits. Is there a way to make it preserve them?

Say my history looks like—

  • Undo a git merge that has been pushed and redo the merge
  • Can I override working copy by index content?
  • Stripped commits on GIT repository with Bitbucket
  • IntelliJ IDEA - different team members on mac/PC
  • Resolve conflicts using remote changes when pulling from Git remote
  • How to configure 'git diff' to use emacs diff
  • A
    | \ 
    B  H
    |  |
    C  G
    |  |
    D  F
    | /
    E
    

    (A being the merge commit.)

    After a git pull --rebase it becomes—

    H
    |
    G
    |
    F
    |
    X
    |
    B
    |
    C
    |
    D
    |
    E
    

    (X being the new commits git pull --rebase inserted into my history.)—A is removed.

    I know you can use git rebase --preserve-merges to preserve them with git rebase, but I don’t see a way to preserve them with git pull --rebase.

  • PHP Composer Repo push Jenkins Trigger
  • How to stage only part of a new file with git?
  • git show/log without shell escape sequences, for use with python sh
  • How do I configure Jenkins to build all branches except a few which I exclude?
  • testing/info/refs not found in gitolite after removing R @all rule
  • invalid username/password in egit when pushing changes to googlecode
  • 3 Solutions collect form web for “Make git pull –rebase preserve merge commits”

    you can split your pull in a fetch and a rebase

    git fetch origin master
    git rebase origin master --preserve-merges
    

    Or (for the upcoming git 1.8.5 Q4 2013, now delivered in git 1.8.5, 2013-11-27):

    git pull --rebase” always chose to do the bog-standard flattening rebase.
    You can tell it to run “rebase --preserve-merges” by setting “pull.rebase” configuration to “preserve“.

    So a simple config will be enough to make sure your pull --rebase does preserve merge:

    git config pull.rebase preserve
    

    See commit 66713ef3 for more (thanks to Stephen Haberman):

    pull: allow pull to preserve merges when rebasing

    If a user is working on master, and has merged in their feature branch, but now has to “git pull” because master moved, with pull.rebase their feature branch will be flattened into master.

    This is because “git pull” currently does not know about rebase’s preserve merges flag, which would avoid this behavior, as it would instead replay just the merge commit of the feature branch onto the new master, and not replay each individual commit in the feature branch.

    Add a --rebase=preserve option, which will pass along --preserve-merges to rebase.

    Also add ‘preserve‘ to the allowed values for the pull.rebase config setting.

    Simply:

    git pull --rebase=preserve
    

    From the docs:

    When set to preserve, rebase with the --preserve-merges option passed to git rebase so that locally created merge commits will not be flattened.

    Git Baby is a git and github fan, let's start git clone.