Git commands

git rebase

git rebase: reapply commits from one branch on top of another branch.

The git rebase command is used to move or combine a sequence of commits to a new base commit. Unlike git merge, which creates a merge commit to combine two branches, rebasing takes the commits from your current branch and replays them on top of another branch, creating a linear commit history.

Rebasing is particularly useful for maintaining a clean, linear project history. Instead of creating a "commit salad" where commits from different branches are interleaved, rebasing ensures that your feature commits appear as if they were developed directly on top of the latest changes from the target branch.

There are two main types of rebasing:

  1. Regular rebase: Takes all commits from your current branch and replays them on top of another branch, maintaining the same commit structure but with new base commits.

  2. Interactive rebase (git rebase -i): Arguably the most powerful feature of Git, it allows you to interactively edit, reorder, squash, or drop commits during the rebase process.

Rebasing rewrites commit history by creating new commits with the same changes but different commit hashes. This means you should never rebase commits that have been shared with others, as it can cause confusion and conflicts for collaborators.

git merge vs git rebase

Imagine you're making a sandwich (branch: feature/sandwich) while your friend reorganizes the kitchen (branch: feature/kitchen-reorg). Then you try to combine your work.

With git merge:

YOU:
Start making sandwich
THEM:
Start reorganizing kitchen
THEM:
Move bread from table → cabinet
YOU:
Reach for bread on table ⚠️ CONFLICT: bread not found!
THEM:
Move vegetables → fridge
YOU:
Look for lettuce on table ⚠️ CONFLICT: lettuce not there!
THEM:
Move condiments to new shelf
YOU:
Search for mayo in drawer ⚠️ CONFLICT: mayo relocated!
THEM:
Kitchen reorganization complete
YOU:
Finally finish sandwich

HISTORY: YOU-THEM-THEM-YOU-⚠️-THEM-YOU-⚠️-THEM-YOU-⚠️-THEM-YOU-MERGE

With git rebase:

THEM:
Start reorganizing kitchen
THEM:
Move vegetables → fridge
THEM:
Move condiments to new shelf
THEM:
Move bread from table → cabinet
THEM:
Kitchen reorganization complete
YOU:
Start making sandwich ⚠️ CONFLICT: Adjust to new kitchen layout
YOU:
Get lettuce from fridge ✓
YOU:
Get mayo from new shelf ✓
YOU:
Get bread from cabinet ✓
YOU:
Sandwich complete ✓

HISTORY: THEM-THEM-THEM-THEM-⚠️-YOU-YOU-YOU-YOU

The key difference:

  • Merge preserves the chaotic reality of parallel work with multiple conflicts.
  • Rebase creates a clean timeline with one conflict resolution point, then smooth sailing.

Both end with the same result (sandwich + organized kitchen), but rebase tells a simpler story by pretending you started after the kitchen was already reorganized.

Pulling with rebase

When you want to update your current branch with the latest changes from the remote branch, you can use git pull --rebase. This command fetches the latest changes and then rebases your local commits on top of the fetched changes, ensuring a clean commit history.

This behavior can be set as the default for your repository by configuring Git:

git config pull.rebase true

Examples

Rebase the current branch onto main:

git rebase main

Start an interactive rebase for the last 3 commits:

git rebase -i HEAD~3

Continue a rebase after resolving conflicts:

git add .
git rebase --continue

Abort a rebase and return to the original state:

git rebase --abort

Update your current branch with the latest changes from main (take the main branch and put all of your commits on top of it):

git fetch origin
git rebase origin/main

Set rebase as the default behavior for git pull:

git config pull.rebase true
© 2024-2025 GitByBit.All rights reserved.

Hi! I'm Alex, creator of GitByBit.

And this is Gitopedia—a reference that summarizes knowledge in my Git course.

Gitopedia is designed to be a personal knowledge base. When you progress through the main Git course in VS Code, you unlock all of these topics, one by one, bit by bit. This gives you time to digest the information and gradually build the mental model of Git. Here's an example of how it works:

Learning directly in VS Code lets you practice Git exactly as you would in real life, doing real jobs, writing real code. In addition, the course has access to your actual terminal, so it can point out mistakes, suggest workarounds, etc.

The course is FREE, there are no Ads or other bullshit. There are optional premium add-ons you can purchase, mainly to support my work (regrettably, I have to eat every day), but that's totally up to you.

Learn Git in VS Code