DEV Community

Cover image for Why Git Rebase Will Change How You Use Git Forever
Farhad Rahimi Klie
Farhad Rahimi Klie

Posted on

Why Git Rebase Will Change How You Use Git Forever

Introduction

When working with Git, managing commit history is just as important as writing clean code. One of the most powerful — and sometimes misunderstood — tools for this purpose is git rebase.


What is Git Rebase?

At a high level, git rebase is used to move or “replay” commits from one branch onto another.

Instead of merging branches together (like git merge), rebase rewrites commit history to make it look like your work was built on top of another branch from the beginning.


The Core Idea

Let’s say you have:

main:     A --- B --- C
feature:             D --- E
Enter fullscreen mode Exit fullscreen mode

Now, new commits are added to main:

main:     A --- B --- C --- F --- G
feature:             D --- E
Enter fullscreen mode Exit fullscreen mode

If you run:

git rebase main
Enter fullscreen mode Exit fullscreen mode

Git will:

  1. Temporarily remove commits D and E
  2. Move your branch to G
  3. Replay D and E on top of G

Result:

main:     A --- B --- C --- F --- G
feature:                         D' --- E'
Enter fullscreen mode Exit fullscreen mode

Note: D' and E' are new commits (rewritten versions)


Why Use Rebase?

1. Clean History

Rebase creates a linear commit history, making it easier to read and understand.

Instead of this:

      /--- D --- E
A --- B --- C --- M
Enter fullscreen mode Exit fullscreen mode

You get:

A --- B --- C --- D --- E
Enter fullscreen mode Exit fullscreen mode

2. Better for Code Reviews

Reviewing a clean, linear history is much easier than dealing with merge commits.


3. Avoids Unnecessary Merge Commits

Rebase prevents clutter like:

Merge branch 'main' into feature
Enter fullscreen mode Exit fullscreen mode

Rebase vs Merge

Feature git merge git rebase
History Non-linear Linear
New commits Adds merge commit Rewrites commits
Safety Safe (no rewrite) Risky if misused
Collaboration Preferred Careful usage needed

Basic Commands

Rebase current branch onto another

git rebase main
Enter fullscreen mode Exit fullscreen mode

Interactive Rebase

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

This allows you to:

  • Edit commits
  • Squash commits
  • Reorder commits
  • Remove commits

Example:

pick 123abc Add login
pick 456def Fix bug
pick 789ghi Improve UI
Enter fullscreen mode Exit fullscreen mode

You can change to:

pick 123abc Add login
squash 456def Fix bug
pick 789ghi Improve UI
Enter fullscreen mode Exit fullscreen mode

What Happens Internally?

When you run rebase:

  1. Git finds the common ancestor
  2. Extracts your commits as patches
  3. Resets your branch pointer
  4. Applies patches one by one

This is why commit hashes change — they are new commits.


Handling Conflicts

During rebase, conflicts may occur.

Git will pause and show:

CONFLICT (content): Merge conflict in file.c
Enter fullscreen mode Exit fullscreen mode

Steps to resolve:

  1. Fix the conflict manually
  2. Stage the file:
   git add file.c
Enter fullscreen mode Exit fullscreen mode
  1. Continue:
   git rebase --continue
Enter fullscreen mode Exit fullscreen mode

If needed:

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

Golden Rule of Rebase

❗ Never rebase public/shared branches

Why?

Because rebase rewrites history, and this can break other developers’ work.


When Should You Use Rebase?

Use rebase when:

  • Working on local feature branches
  • Cleaning up commits before pushing
  • Keeping history linear

Avoid rebase when:

  • Working on shared branches
  • The branch is already pushed and used by others

Advanced Example: Squashing Commits

Before:

Add feature
Fix typo
Fix bug
Improve code
Enter fullscreen mode Exit fullscreen mode

After squash:

Add feature (clean version)
Enter fullscreen mode Exit fullscreen mode

Command:

git rebase -i HEAD~4
Enter fullscreen mode Exit fullscreen mode

Common Mistakes

1. Rebasing Shared Branches

This can cause major confusion and broken history.


2. Forgetting Force Push

After rebase, you must push with:

git push --force
Enter fullscreen mode Exit fullscreen mode

(or safer:)

git push --force-with-lease
Enter fullscreen mode Exit fullscreen mode

3. Losing Commits

If something goes wrong:

git reflog
Enter fullscreen mode Exit fullscreen mode

You can recover lost commits.


Mental Model

Think of rebase as:

“Take my changes and pretend I wrote them after the latest version of the branch.”


Conclusion

Git Rebase is a powerful tool that:

  • Keeps history clean
  • Improves collaboration (when used correctly)
  • Helps maintain a professional commit structure

But it must be used with discipline and understanding.

If you master rebase, you move from just “using Git” to truly controlling your project history.


Final Tip

Start using rebase in your local workflow first, then gradually introduce it into team practices with clear guidelines.


Happy coding 🚀

Top comments (0)