wasAsked.

What is the difference between git merge and git rebase?

Frequently Asked (All Companies) · Role not specified · technical

Published

Year not specified · Git, DevOps

Community answers

Posted by wasAsked editorial collection

Both integrate changes from one branch into another, but they produce different histories. git merge - Combines the histories of two branches by creating a merge commit (unless a fast-forward is possible). git checkout main git merge feature/payments - History shows exactly what happened, including when branches diverged and were merged. - Non-destructive: existing commits are not changed, so it is safe for shared branches. - Downside: frequent merges create a branchy, harder-to-read history with many merge commits. - Fast-forward merge: if main has not moved since the branch was created, Git simply moves the pointer forward with no merge commit (use --no-ff to force a merge commit for traceability). git rebase - Moves (replays) your branch's commits on top of another branch, creating new commits with new hashes. git checkout feature/payments git rebase main - Produces a linear history, as if you had started your work from the latest main. - Rewrites history: the original commits are replaced. - Interactive rebase (git rebase -i) lets you squash, reorder, edit or drop commits to clean up before sharing. Visualising it Before: main has commits A-B-C; your feature branch started at A and has commits D-E. - Merge result: A-B-C-M, where M merges in D-E (D and E keep their original identity). - Rebase result: A-B-C-D'-E' (new copies of D and E applied on top of C). The golden rule of rebasing Do not rebase commits that others have already pulled from a shared branch. Rewriting public history forces others to reconcile duplicate commits. Rebase your own local or personal feature branches only. If you must update a pushed personal branch after rebasing, use git push --force-with-lease (safer than --force because it refuses to overwrite others' work). Conflict handling - Merge: resolve conflicts once in the merge commit. - Rebase: conflicts may need resolving for each replayed commit (git rebase --continue after fixing, or --abort to stop). When to use which - Rebase: updating your feature branch with the latest main before opening or updating a pull request; cleaning up local commits; keeping a linear history. - Merge: integrating completed features into shared branches like main or develop; preserving the true history of collaboration. - Many teams combine both: rebase locally to stay current, then merge the pull request (often with "squash and merge" or "rebase and merge" options on GitHub or Azure DevOps). Related commands - git pull --rebase: fetch and rebase local commits instead of creating merge commits. - git merge --squash: combine all branch changes into one commit on the target branch. Interview summary: "Merge preserves history and creates a merge commit; rebase rewrites your commits on top of another branch for a linear history. Rebase private branches to keep history clean, but never rebase shared, already-pushed history."

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics