Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Git Rebase

Caution

rebase modifies history.

Do not rebase commits that others may have based work on.

Two branches, the start.

---
config:
  gitGraph:
    parallelCommits: true
---
gitGraph LR:
   commit id: "A"
   commit id: "B"
   commit id: "C"
   branch dev
   checkout dev
   commit id: "HEAD → E"
   checkout main
   commit id: "HEAD → D"

Normally, with two branches, we’d do a merge

Merging

---
config:
  gitGraph:
    parallelCommits: true
---
gitGraph LR:
   commit id: "A"
   commit id: "B"
   commit id: "C"
   branch dev
   checkout dev
   commit id: "HEAD → E"
   checkout main
   commit id: "D"
   merge dev id: "HEAD → F"

F is the commit the combines the diff of the endpoints of the branches main and dev.

The dev branch is just hanging out.


Rebasing

---
config:
  gitGraph:
    parallelCommits: true
---
gitGraph LR:
   commit id: "A"
   commit id: "B"
   commit id: "C"
   branch dev
   checkout dev
   commit id: "HEAD → E"
   checkout main
   commit id: "HEAD → D"

Finding the common ancestor to both branches, C we go “This is the common base, just play the diffs forward from both branches onto main

git checkout dev
git rebase main
---
config:
  gitGraph:
    parallelCommits: true
---
gitGraph LR:
   commit id: "A"
   commit id: "B"
   commit id: "C"
   commit id: "HEAD/main → D"
   commit id: "HEAD/dev → E"

Now the HEAD for two branches main and dev are on the same branch. Main can be FF’d to move the HEAD.

git checkout main
git merge dev
---
config:
  gitGraph:
    parallelCommits: true
---
gitGraph LR:
   commit id: "A"
   commit id: "B"
   commit id: "C"
   commit id: "D"
   commit id: "HEAD/main, dev → E"

References

Git - Rebasing

Git - git-rebase Documentation

Last Modified • Sunday, June 14, 2026. 5:09 am UTC+00:00 • Commit: 4a9f867