G
GuideDevOps
Lesson 9 of 18

Git Rebase

Part of the Git & Version Control tutorial series.

What is Rebase?

Rebase replays commits from one branch on top of another. It rewrites commit history to create a linear timeline.

Rebase vs. Merge

OperationResultUse Case
MergeCreates merge commitOfficial record of branch integration
RebaseLinear historyClean, readable project history

Before Rebase

main:    A --- B --- C
              |
feature:     D --- E --- F

After Rebase

main:    A --- B --- C
                      |
feature:              D' --- E' --- F'

(Commits D, E, F are replayed on top of C as D', E', F')

Simple Rebase

Basic Rebase

git checkout feature/auth
git rebase main
 
# Now feature is based on main, with linear history
# Then merge back:
git checkout main
git merge feature/auth  # Fast-forward!

Upstream Tracking

# If your branch tracks a remote:
git rebase
 
# Equivalent to:
git fetch origin
git rebase origin/main

Interactive Rebase

Rewrite commits interactively for cleanup.

Start Interactive Rebase

# Rebase last 3 commits
git rebase -i HEAD~3
 
# Rebase feature branch on main interactively
git rebase -i main

This opens an editor showing your commits:

pick abc1234 Add user model
pick def5678 Add validation
pick ghi9012 Fix typo in validation

Interactive Commands

CommandAction
pickUse commit
rewordUse, but edit message
squash (s)Combine with previous
fixup (f)Squash without keeping message
dropRemove commit
editStop to amend

Example: Squash Commits

Combine multiple commits into one:

pick abc1234 Add user model
squash def5678 Add validation
squash ghi9012 Fix typo in validation

Result: One commit "Add user model" with all changes.

Example: Reword Commits

Fix commit messages:

reword abc1234 Add user model
reword def5678 Add validation

Git asks for new message for each.

Example: Clean Up Before PR

# Interactive rebase of 5 commits
git rebase -i HEAD~5
 
# Change to:
pick abc1234 Add search feature
squash def5678 Fix search typo
squash ghi9012 Add tests
drop jkl3456 WIP debugging
 
# Result: Clean 3-commit feature ready for PR

Handling Rebase Conflicts

When rebasing, conflicts can occur:

git rebase main
 
# If conflicts:
# 1. Edit conflicted files
# 2. Resolve conflicts
git add .
git rebase --continue
 
# Or abort
git rebase --abort

Resolving Conflicts

# See conflicts
git status
 
# Edit files to resolve
# Then:
git add resolved_file.ts
git rebase --continue

Rebase vs. Merge: When to Use

Use Merge When:

  • ✅ Preserving official record of integration
  • ✅ Working on shared/main branches
  • ✅ Multiple people involved
  • ✅ Feature important enough to be visible

Use Rebase When:

  • ✅ Cleaning up local commits before pushing
  • ✅ Keeping history linear
  • ✅ Fixing commit messages
  • ✅ Removing WIP/debugging commits

Never Rebase Public Commits

GOLDEN RULE: Never rebase commits already pushed to shared repository!

# OK: Rebase local changes before push
git rebase main
git push
 
# DANGEROUS: Rebase already-pushed commits
git push
git rebase main      # ❌ DON'T DO THIS
git push --force     # ❌ Breaks others' history

If you accidentally rebase pushed commits:

git reflog          # See all changes
git reset --hard abc1234  # Restore previous state

Autostash

git rebase -i main --autostash
# Automatically stashes uncommitted changes
# Reapplies them after rebase

Common Workflows

Clean Up Feature Branch Before PR

git checkout feature/auth
git rebase -i main
 
# Edit commits: fix typos, squash WIP, reword messages
 
git push -f origin feature/auth  # Force push to PR

Update Feature with Latest Main

git checkout feature/payment
git rebase main
 
# Replay your commits on latest main
# Linear history maintained

Fix Multiple Commit Messages

git rebase -i HEAD~5  # Last 5 commits
 
# Mark all as 'reword':
# reword abc1234 ...
# reword def5678 ...
 
# Edit each message when prompted

Tips

✓ Only rebase local/unpushed commits
✓ Use --autostash to save uncommitted work
interactive rebase is powerful but use carefully
--abort to cancel if something goes wrong
✓ Rebasing + squashing = clean PR history
✓ Team should agree on usage policy