Please enable JavaScript to use CodeHS

Agentic Engineering & AI Software Topics

Rebasing and history

Rebase rewrites history by moving or replaying commits on top of another branch. It can produce a cleaner, straighter graph than a merge, but it changes commit hashes, so it is safest on branches that no one else has shared yet.

Git ~12 min High School / College Prep
The Big Idea

Why rewrite history?

A clean commit history is like a well-written story. Each commit does one thing, the order makes sense, and you can read the history top-to-bottom and understand what happened. A messy history makes it hard to find bugs, review code, and understand why a change was made.

Rebase is a tool that lets you rewrite history before you share it. You can use it to:

  • move a branch onto the latest main so the history is linear,
  • reorder or drop commits,
  • combine small commits into one, called squashing.

Golden rule: Do not rebase commits that have already been pushed to a shared branch. Rewriting shared history confuses your teammates because the commits they based their work on no longer exist.

Workflows

How rebase works

When you run git rebase main while on a feature branch, Git finds the first commit the feature branch shares with main, then replays each feature commit on top of the latest main commit.

The commits have the same changes and messages, but their parent relationships are different, so Git gives them new IDs. That is why rebasing is called rewriting history.

Because each commit may apply to a slightly different codebase, the rebase can pause and ask you to resolve conflicts, one commit at a time. This is called a rebase conflict and is resolved the same way as a merge conflict.

Pattern

Interactive rebase and squash

git rebase -i HEAD~3 opens an editor with the last three commits listed. Each line starts with a command:

  • pick — keep the commit as is.
  • reword — keep the commit but edit its message.
  • squash — combine the commit into the one above it.
  • drop — remove the commit.
  • reorder — move the line to change commit order.

Squashing is useful when you made five tiny "work in progress" commits while fixing a bug and want to turn them into one clean commit before sharing.

Code

A rebase todo file

This is what Git shows when you run git rebase -i HEAD~3. You can edit the commands and the order, save, and Git rewrites the history to match.

Interactive

Merge vs rebase graph

Start with a feature branch that has two commits off main. Click Merge to see a merge commit, or Rebase to see the feature commits replayed onto main as a straight line. Then Reset to try the other one.

Ready. Choose merge or rebase.
Interactive

Try squashing

Check the commits you want to combine into one. Then click Squash selected to see the rewritten history.

Check Yourself

Quick quiz

1. What does git rebase main do?

2. When is it unsafe to rebase?

3. What does squash do in an interactive rebase?

Keep learning

You now understand branches, merging, conflicts, and rebasing. These are the core Git skills teams and coding agents use every day. Next, see how collaboration and remote repositories fit into the picture.

Part of the “Agentic Engineering & AI Software Topics” series.