Sobes.tech
Middle

What is the difference between git rebase and git merge?

sobes.tech AI

Answer from AI

git merge and git rebase are two ways to integrate changes from one branch into another, but they work differently and have different consequences for the commit history.

  • git merge combines two branches, creating a new merge commit. The history retains all branches and their points of divergence, making it branchy and reflecting the actual development process.

  • git rebase re-applies (rewrites) commits from the current branch onto another branch, creating a linear history without merge commits. It’s like "rewriting" history to make it cleaner, but it can cause issues when collaborating if the branch has already been published.

Example:

# merge
git checkout feature
git merge main

# rebase
git checkout feature
git rebase main

Use merge if you want to preserve the branching history, and rebase if you want a linear history without merge commits.