Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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—

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.

like image 712
user1569050 Avatar asked Aug 08 '12 11:08

user1569050


People also ask

Does rebase preserving merge commits?

Merging is a safe option that preserves the entire history of your repository, while rebasing creates a linear history by moving your feature branch onto the tip of main .

Does git rebase remove merge commits?

One problem I face with rebase is, it removes merge commits. Suppose we have a common feature branch shared by multiple developers, we are merging commits to this branch first, and to keep atomic commits properly we are using no-fast-forward merge commits.

When to pull rebase or merge?

best practice. It is best practice to always rebase your local commits when you pull before pushing them. As nobody knows your commits yet, nobody will be confused when they are rebased but the additional commit of a merge would be unnecessarily confusing.


2 Answers

Update: As I presented in "What exactly does Git's "rebase --preserve-merges" do (and why?)", since Git 2.18 (Q2 2018), you would prefer the new option --rebase-merges to the legacy one --preserve-merge

Since then:

  • Git 2.22, Q2 2019, actually deprecates --preserve-merge, and
  • Git 2.25, Q1 2020, stops advertising it in the "git rebase --help" output.

So:

git rebase --interactive --rebase-merges origin/master 
# or
git config pull.rebase merges
git rebase --interactive origin/master (would use rebase-merges)

Original answer 2013:

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.

like image 181
VonC Avatar answered Sep 26 '22 20:09

VonC


you can split your pull in a fetch and a rebase

git fetch origin master
git rebase origin master --preserve-merges
like image 24
Mario F Avatar answered Sep 23 '22 20:09

Mario F