Merge vs Rebase

Initial State

Imagine we have a long running branch, named feature that is behind master, which we want to merge into.

git log --graph --decorate --oneline --all
# * 5e59f79 (HEAD -> master) Master moving even more
# * cd5ca21 Master moving forward
# | * 64488c1 (feature) Feature - 2
# | * 6d10413 Feature - 1
# |/  
# * f38dfc2 Initial

There are several ways we can do it.

Option 1 - Merge feature to master

git checkout master
git merge feature -m "merge feature"
git branch delete feature

git log --graph --decorate --oneline
# *   16bd684 (HEAD -> master) merge feature
# |\  
# | * 64488c1 (feature, delete) Feature - 2
# | * 6d10413 Feature - 1
# * | 5e59f79 Master moving even more
# * | cd5ca21 Master moving forward
# |/  
# * f38dfc2 Initial

Option 2 - Squash feature to master

git checkout master
git merge feature --squash
git commit -m "squashed"
git branch delete feature

git log --graph --decorate --oneline
# * 9741a37 (HEAD -> master) squashed
# * 5e59f79 Master moving even more
# * cd5ca21 Master moving forward
# * f38dfc2 Initial

Option 3 - Rebase and Merge

git checkout feature

git rebase master
# First, rewinding head to replay your work on top of it...
# Applying: Feature - 1
# Applying: Feature - 2

git checkout master
git merge feature
git branch delete feature

git log --graph --decorate --oneline
# * e6ae076 (HEAD -> master, feature, delete) Feature - 2
# * 921c475 Feature - 1
# * 5e59f79 Master moving even more
# * cd5ca21 Master moving forward
# * f38dfc2 Initial