Earn it: GitHub Achievements Academy home

Git 4: Rebase without fear

Rebasing has a scary reputation, but it does one simple thing: it replays your commits on top of a different starting point. Learn the one rule below and it becomes one of your favorite tools.

What rebase does

Your branch started from commit B, but main has moved on:

my-feature started from B, and main has moved on to D.Try this in the sandbox
git switch my-feature
git rebase main

Git takes your commits E and F, sets them aside, moves your branch to the tip of main, then replays them one by one:

After git rebase main. New copies of E and F sit on top of D. The originals fade out, no longer on any branch.Try this in the sandbox

The new E and F have the same changes as the originals, but they're new commits with new IDs, because their parent changed. (Git people often write them as E′ and F′.) Remember that, because it's why the golden rule exists.

Watch it: in the rebase sandbox challenge your old commits fade out and new ones appear on top of main.

Merge or rebase?

Comparedgit merge maingit rebase main
HistoryKeeps exactly what happened, plus a merge commitStraight line, as if you started today
Rewrites commits?NoYes, so your commits get new IDs
ConflictsResolved onceResolved per commit, which can mean several times
Safe on shared branches?YesOnly if nobody else uses the branch
The same starting point, merged instead of rebased. E and F keep their IDs, and M joins the two lines.Try this in the sandbox

Both are fine. Many teams merge on GitHub and rebase locally to stay current.

The golden rule

Never rebase commits that other people have already based work on.

Rebasing creates new commits. If a teammate built on the old ones, their history and yours no longer match, and everyone has a bad afternoon. Rebasing your own feature branch, even after pushing it for a PR, is normal. Just update the remote safely:

git push --force-with-lease

--force-with-lease refuses to overwrite the remote if someone else pushed in the meantime. Never use plain --force on shared branches.

Conflicts during a rebase

Conflicts appear one commit at a time. For each one:

# fix the file(s), then:
git add <file>
git rebase --continue

Other options:

git rebase --skip     # drop this commit entirely (rarely what you want)
git rebase --abort    # cancel and go back to exactly where you started

Interactive rebase: tidy up before you share

Before opening a PR, you can clean up messy local history ("wip", "fix typo", "actually fix it"):

git rebase -i main

Git opens a list of your commits, oldest first:

pick 1a2b3c4 Add search box
pick 5d6e7f8 wip
pick 9a8b7c6 fix typo in search

Change the word at the start of each line:

CommandEffect
pickkeep the commit as is
rewordkeep it, but edit the message
squashcombine with the commit above, and edit the combined message
fixupcombine with the commit above, keeping the message above
dropdelete the commit

You can also reorder lines to reorder commits. Save and close, and Git does the rest. Mistake? git rebase --abort, or see the "Oh no" guide.

The pro shortcut: fixup commits

Noticed a bug in a commit from earlier on your branch?

git commit --fixup 1a2b3c4          # makes a commit titled "fixup! Add search box"
git rebase -i --autosquash main     # it's already placed and marked as fixup, so just save

Keep pull from making messy merges

By default git pull can create "Merge branch 'main' of github.com…" commits when your local branch and GitHub's have both moved:

You and a teammate both committed after B. A plain git pull joins the two with a merge commit, M.Try this in the sandbox

Many people prefer:

git config --global pull.rebase true

Now git pull rebases your local commits on top of what you fetched.

Exercise (in your sandbox)

  1. Make a branch with three small commits, including one called wip.
  2. Run git rebase -i main and squash wip into the commit before it.
  3. Check with git log --oneline. You should see two clean commits.

What you learned

  • Rebase replays your commits onto a new base, creating new commits
  • The golden rule, and --force-with-lease
  • Interactive rebase and fixup commits to tidy history before review

Next: Git 5: Remotes, fetch, pull and push

Check what you learned

1. What does rebase do to your commits?
2. What's the golden rule of rebasing?
3. After rebasing your own PR branch, how should you push?

Show the answers
  1. Replays them on a new base as new commits with new IDs: Same changes, new parents, so new IDs. That's why the golden rule exists.
  2. Never rebase commits others have already based work on: Rewriting shared history breaks your teammates' copies.
  3. git push --force-with-lease: --force-with-lease refuses to overwrite commits you haven't seen.

Your progress is saved in this browser only.

Last reviewed November 2026. Improve this page on GitHub. Every fix you send counts toward Pull Shark.