DevOpsLesson
DevOpsLesson

Free, comprehensive DevOps tutorials and learning roadmaps. Master Docker, Kubernetes, CI/CD, and more.

Stay Updated

Get notified about new tutorials and features.

Tutorials

  • What is DevOps?
  • Docker Tutorial
  • Terraform Tutorial
  • CI/CD Pipeline
  • All Tutorials

Roadmaps

  • DevOps Engineer
  • Cloud Engineer
  • SRE Path
  • All Roadmaps

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service

© 2026 DevOpsLesson. All rights reserved.

DOCKERKUBERNETESTERRAFORMAWSCI/CDLINUXGITDEVOPS ROADMAPCLOUD ROADMAPSRE ROADMAPGIT CHEATSHEETDOCKER CHEATSHEETK8S CHEATSHEETTF CHEATSHEETLINUX CHEATSHEETDOCKERFILE LINTERYAML VALIDATORCRON PARSERREGEX TESTER

Git Tutorial

Introduction to Git
Installing Git
Git Basics
Git Branching
Git Merging
Git Rebasing
Git Remote
Git Stash
Undoing Changes in Git
Git Tagging
GitHub Workflow with Git
Advanced Git
Git Hooks
Git Workflows
Git Aliases and Configuration
Git Submodules
Git Commit Messages

Git Rebasing

PreviousPrev
Next

Learn what rebase does, how interactive rebase works, when to squash commits, and how rebase compares with merge.

What Rebase Does

Rebase moves or reapplies commits onto a new base commit. Instead of combining two branches with a merge commit, rebase rewrites the branch so it appears to start from a later point in history.

Suppose feature/login was created from main, but main has advanced since then. Rebasing feature/login onto main replays the feature branch commits on top of the newer main.

git switch feature/login
git rebase main

This can make history look cleaner and more linear.

Tip: Rebase rewrites commit history. That is powerful, but it means you should be careful with commits other people are already using.

Why Developers Rebase

Common reasons to use rebase include:

  • updating a feature branch with the latest main
  • cleaning up commit history before a pull request
  • squashing many small commits into one logical commit
  • reordering or editing recent commits

Rebase does not add new project content by itself. It changes how commits are arranged in history.

Basic Rebase Example

Imagine this history:

  • main: A → B → C
  • feature/login: A → D → E

After rebasing feature/login onto main, the feature becomes:

  • main: A → B → C
  • feature/login: A → B → C → D' → E'

The apostrophes matter. Git usually creates new commit IDs because it is replaying the changes, not simply moving old commits.

Use:

git switch feature/login
git rebase main

If everything applies cleanly, you get the same code changes on a newer base.

Rebase Conflicts

Rebase can produce conflicts just like merge. When that happens, Git pauses and asks you to resolve them.

A typical flow looks like this:

git rebase main
git status

Edit the conflicted file, remove conflict markers, then continue:

git add app.js
git rebase --continue

If another conflict appears, repeat the process.

If you want to stop and return to the original state:

git rebase --abort

Interactive Rebase with -i

Interactive rebase lets you edit a series of recent commits. It is one of the most useful cleanup tools in Git.

To review the last three commits:

git rebase -i HEAD~3

Git opens your editor with something like:

pick a1b2c3d Add login form
pick d4e5f6g Fix typo in login form
pick h7i8j9k Update login button color

You can change the action for each commit.

Common Interactive Actions

  • pick: keep the commit as is
  • reword: keep the content but edit the message
  • edit: pause so you can modify the commit
  • squash: combine this commit with the previous one
  • drop: remove the commit

Squashing Commits

Suppose your history contains several tiny “fix typo” commits that belong with the main feature. Interactive rebase can combine them.

Example:

pick a1b2c3d Add login form
squash d4e5f6g Fix typo in login form
squash h7i8j9k Update login button color

After saving, Git combines the commits and lets you write a final commit message.

This is useful before opening a pull request because reviewers often prefer a clean, logical commit history.

Note: Squashing is best used on your own unpublished work. Avoid rewriting shared history unless your team explicitly agrees on that workflow.

Rebase vs Merge

Both rebase and merge combine lines of development, but they tell the story differently.

TopicMergeRebase
History shapePreserves branch structureCreates a more linear history
New commit IDsUsually no rewrite of existing commitsRewrites rebased commits
Safety for shared historySaferNeeds more care
Typical useIntegrating branchesCleaning history or updating a feature branch

Simple Rule of Thumb

  • Use merge when you want to preserve real branch history
  • Use rebase when you want a cleaner branch history before integration

A Safe Team Habit

A common safe habit is:

  1. Rebase your own feature branch onto the latest main
  2. Push the updated branch
  3. Merge it through a pull request

Example:

git switch main
git pull
git switch feature/login
git rebase main
git push --force-with-lease

--force-with-lease is safer than plain --force because it checks whether the remote changed in unexpected ways.

What You Should Remember

Rebase replays commits onto a new base, which can make history look cleaner. Interactive rebase is especially useful for rewording, reordering, or squashing recent commits. But because rebase rewrites history, it should be used carefully on shared branches.

If you are unsure, use merge. If you want a polished branch before review, rebase can be a great tool.

Test Your Understanding

Let's see how well you understood the concepts! These exercises will help reinforce what you just learned.

Exercise 1: Understanding rebased commits

Scenario: Your feature branch had commits D and E, and you rebased it onto a newer main with commits A, B, and C.

Question: Why did the tutorial show the rebased branch as A -> B -> C -> D' -> E' instead of keeping the old D and E exactly?

Exercise

Exercise 1: Understanding rebased commits

Identify why rebased commits usually receive new identities.

Exercise 2: Cleaning up tiny commits

Scenario: Your feature branch contains Add login form, Fix typo in login form, and Update login button color as three small related commits.

Question: Which interactive rebase action from the tutorial is designed to combine the later two into the first logical commit?

Exercise

Exercise 2: Cleaning up tiny commits

Choose the interactive rebase action that combines related commits.

Exercise 3: Pushing a rebased branch safely

Question: After rebasing your own feature branch onto the latest main, why did the tutorial recommend git push --force-with-lease instead of plain --force?

Exercise

Exercise 3: Pushing a rebased branch safely

Pick the reason force-with-lease is safer after a history rewrite.

PreviousPrev
Next

Continue Learning

Git Basics

Learn the everyday Git workflow with init, clone, status, add, commit, log, and diff using practical beginner-friendly examples.

15 min·Easy

Git Branching

Understand Git branches, HEAD, switching branches, deleting branches, and detached HEAD with practical beginner examples.

18 min·Easy

Git Merging

Learn fast-forward merges, three-way merges, merge conflicts, and why teams sometimes use git merge --no-ff.

20 min·Medium

Explore Related Topics

CI

CI/CD Tutorials

Trigger automated pipelines from Git workflows

Li

Linux Tutorials

Use Git effectively on the Linux command line

On This Page

What Rebase DoesWhy Developers RebaseBasic Rebase ExampleRebase ConflictsInteractive Rebase with `-i`Common Interactive ActionsSquashing CommitsRebase vs MergeSimple Rule of ThumbA Safe Team HabitWhat You Should RememberTest Your UnderstandingExercise 1: Understanding rebased commitsExercise 2: Cleaning up tiny commitsExercise 3: Pushing a rebased branch safely