DEV Community

Cover image for Git Rebase: A Dev Friendly Guide
Athreya aka Maneshwar
Athreya aka Maneshwar

Posted on

Git Rebase: A Dev Friendly Guide

Hello, I'm Maneshwar. I’m building LiveReview, a private AI code review tool that runs on your LLM key (OpenAI, Gemini, etc.) with highly competitive pricing -- built for small teams. Do check it out and give it a try!

Rebasing is one of Git’s most powerful but also most misunderstood commands.

It helps you keep a clean, linear commit history, but it also introduces conflicts that can be tricky to resolve.

In this post, we’ll break down what rebasing is, when to use it, how conflicts occur, and how to deal with them.

What is Rebase?

git rebase is a command that takes commits from one branch and reapplies them on top of another.

Instead of merging (which creates a new commit joining two branches), rebasing replays your commits one by one on top of the target branch.

Example:

# Rebasing your feature branch on top of main
git checkout feature
git rebase main
Enter fullscreen mode Exit fullscreen mode

This rewrites commit history so that it looks like you started your work from the latest main.

a4mid6 (1)

Rebase vs Merge

  • Merge: Preserves history but introduces merge commits.
  • Rebase: Creates a linear history, easier to read, but rewrites commits.

👉 Use merge when you want to keep a record of how branches diverged.
👉 Use rebase when you want a clean, straight commit history.

0_LFtbSIWWXBrcHMD9 (1)

Aborting a Rebase

If something goes wrong during a rebase (for example, conflicts you don’t want to deal with), you can stop it:

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

This restores your branch to the state before the rebase started.

6ijxfb (1)

Preventing Automatic Rebase by Default

Sometimes Git suggests rebasing when you git pull.

If you prefer merges by default, disable auto-rebase globally:

git config --global pull.rebase false
Enter fullscreen mode Exit fullscreen mode

You can also enable it (if you want linear history always):

git config --global pull.rebase true
Enter fullscreen mode Exit fullscreen mode

When Do Rebase Conflicts Occur?

Conflicts occur during rebase for the same reason they occur in merges:

  • Two branches change the same lines in a file.
  • One branch deletes a file while another modifies it.
  • File renames combined with edits.

Example scenario:

  • Branch main changes function foo().
  • Your feature branch also changes the same foo().
  • During git rebase main, Git cannot decide which change to keep → conflict.

How to Solve Rebase Conflicts

  1. Check conflict files
   git status
Enter fullscreen mode Exit fullscreen mode

You’ll see files marked as both modified.

  1. Open the files and look for conflict markers:
   <<<<<<< HEAD
   Code from main
   =======
   Code from feature branch
   >>>>>>> feature
Enter fullscreen mode Exit fullscreen mode
  1. Manually edit to keep the correct version (sometimes both).

  2. Mark as resolved

   git add <file>
Enter fullscreen mode Exit fullscreen mode
  1. Continue rebase
   git rebase --continue
Enter fullscreen mode Exit fullscreen mode
  1. If it gets messy
   git rebase --abort
Enter fullscreen mode Exit fullscreen mode

git-rebase-the (2)

Best Practices for Rebasing

  • Try not to rebase public/shared branches (rewriting history confuses collaborators).
  • Rebase your feature branch on main before merging → keeps history clean.

Summary

  • git rebase makes history linear, git merge keeps history intact.
  • Use --abort to stop a rebase if things go wrong.
  • Control default rebase behavior with git config pull.rebase.
  • Conflicts happen when the same file/line is modified in multiple branches.
  • Solve conflicts by editing files, staging them, and running git rebase --continue.

Done right, rebase keeps your Git history tidy and professional, but remember to use it wisely.

LiveReview helps you get great feedback on your PR/MR in a few minutes.

Saves hours on every PR by giving fast, automated first-pass reviews.

If you're tired of waiting for your peer to review your code or are not confident that they'll provide valid feedback, here's LiveReview for you.

Top comments (0)