Skip to main content

Merge and Rebase: Advanced Cases


Table of contents


  1. Recap of the basics
  2. Advanced interactive rebase
  3. Merge strategies
  4. Resolving complex conflicts
  5. Advanced workflows
  6. Recovery commands


1 - Recap of the basics



Merge vs Rebase

AspectMergeRebase
HistoryPreservedRewritten
CommitsMerge commitLinear
Safety✅ Always safe⚠️ Local branch only

The golden rule of Rebase

⚠️ NEVER rebase a branch that has been shared (pushed).

If others have based their work on it, use merge.

🔝 Back to table of contents



2 - Advanced interactive rebase



Start an interactive rebase

git rebase -i HEAD~5
# ou
git rebase -i abc1234

The available commands

pick   = utiliser le commit
reword = modifier le message
edit = s'arrêter pour modifier
squash = fusionner avec le précédent
fixup = fusionner, ignorer le message
drop = supprimer le commit

Example: Reorganize commits

Initial state:

pick abc1234 feat: add login
pick def5678 fix: typo
pick ghi9012 feat: add logout
pick jkl3456 fix: another typo

Reorganized:

pick abc1234 feat: add login
pick ghi9012 feat: add logout
squash def5678 fix: typo
squash jkl3456 fix: another typo

Modify an old commit

git rebase -i HEAD~3
# Marquer le commit comme "edit"

After saving:

# Modifier les fichiers
git add .
git commit --amend

# Continuer le rebase
git rebase --continue

Split a commit

git rebase -i HEAD~3
# Marquer le commit comme "edit"

Then:

# Annuler le commit mais garder les modifications
git reset HEAD~1

# Commiter en plusieurs fois
git add file1.js
git commit -m "feat: part 1"

git add file2.js
git commit -m "feat: part 2"

git rebase --continue

🔝 Back to table of contents



3 - Merge strategies



Fast-forward (default when possible)

git merge feature
Avant:
D---E main
\
A---B feature

Après:
D---E---A---B main

No fast-forward (force a merge commit)

git merge --no-ff feature
Avant:
D---E main
\
A---B feature

Après:
A---B
/ \
D---E-------M main

Advantage: The history clearly shows where the feature was integrated.

Squash merge

git merge --squash feature
git commit -m "feat: complete feature"
Avant:
D---E main
\
A---B---C feature

Après:
D---E---F main

└── Contient tous les changements de A+B+C

Ours and Theirs

# En cas de conflit, prendre notre version
git merge -X ours feature

# Prendre leur version
git merge -X theirs feature

🔝 Back to table of contents



4 - Resolving complex conflicts



Understanding the markers

<<<<<<< HEAD
Votre code (branche actuelle)
=======
Leur code (branche à merger)
>>>>>>> feature/xyz

Conflicts with 3 sections

<<<<<<< HEAD
Votre version
||||||| parent
Version commune originale
=======
Leur version
>>>>>>> feature

Enable this mode:

git config --global merge.conflictStyle diff3

Resolution tools

# Utiliser un outil graphique
git mergetool

# Voir les versions
git checkout --ours file.txt # Votre version
git checkout --theirs file.txt # Leur version
git checkout --merge file.txt # Revenir au conflit

See the remaining conflicts

git diff --check

Abort if there's a problem

# Annuler le merge
git merge --abort

# Annuler le rebase
git rebase --abort

Resolve without marking as resolved

# Marquer manuellement comme résolu
git add file.txt

# Continuer
git merge --continue
# ou
git rebase --continue

🔝 Back to table of contents



5 - Advanced workflows



"Rebase before merge" workflow

The cleanest for feature branches:

# 1. Mettre à jour main
git checkout main
git pull origin main

# 2. Rebaser la feature sur main
git checkout feature/login
git rebase main

# 3. Résoudre les conflits si nécessaire
# (pour chaque commit)
git rebase --continue

# 4. Forcer le push (branche privée!)
git push --force-with-lease origin feature/login

# 5. Merger dans main (fast-forward)
git checkout main
git merge feature/login

"Squash and merge" workflow (GitHub style)

# Sur GitHub : Squash and merge
# Localement :
git checkout main
git merge --squash feature/login
git commit -m "feat: implement login (#123)"

Rebase --onto

Change the base of a branch:

# Situation : feature basée sur develop, mais doit être basée sur main
#
# A---B---C feature
# /
# D---E---F develop
# /
# G---H main

git rebase --onto main develop feature

# Résultat :
# A'--B'--C' feature
# /
# G---H main
# \
# E---F develop

🔝 Back to table of contents



6 - Recovery commands



Undo a merge

# Si pas encore commité
git merge --abort

# Si déjà commité
git reset --hard HEAD~1

Undo a rebase

# Pendant le rebase
git rebase --abort

# Après le rebase (avec reflog)
git reflog
git reset --hard HEAD@{5}

Return to the state before a rebase

# Trouver le point avant rebase
git reflog
# Chercher "rebase (start)" ou le dernier état de votre branche

# Revenir
git reset --hard HEAD@{n}

Recover lost commits

# Voir tous les commits (même orphelins)
git reflog

# Créer une branche pour les sauver
git branch recovered HEAD@{5}

Summary of advanced commands

CommandUsage
git rebase -iInteractive rebase
git merge --no-ffForce a merge commit
git merge --squashMerge into a single commit
git rebase --ontoChange the base
git checkout --oursTake our version
git checkout --theirsTake their version
git reflogHistory of operations

🔝 Back to table of contents