Master Git & GitHub From Scratch

Clear, practical, and structured lessons designed to take you from Git fundamentals to real-world GitHub collaboration.

Git Rebasing

In Git, rebasing is the process of moving or replaying a sequence of commits onto a new base commit. It is commonly used as an alternative to merging when you want to maintain a linear project history.

For example, if you created a feature branch from main and new commits were added to main, you can rebase your feature branch to replay your commits on top of the latest version of main.

Remember: Rebasing rewrites commit history by creating new commits with new hashes. Use it carefully when working with shared branches.

1. How Rebasing Works

Imagine the repository has this commit history:

main:           A---B---C
                     \
feature:              D---E

The main branch receives another commit, F. Rebasing the feature branch onto main replays commits D and E on top of F.

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

The commits D' and E' contain the replayed changes and have new commit identities.


2. Common Rebase Commands

Standard Rebase

Switch to your feature branch and rebase it onto the latest local main branch.

git switch feature/login
git fetch origin
git rebase origin/main

git fetch origin downloads remote updates. The rebase then replays your feature commits on top of origin/main.

Rebase During a Pull
git pull --rebase origin main

Fetches updates from the remote main branch and replays your local commits on top of the fetched history.

This command is typically used when you are on a local branch that needs to incorporate changes from the specified remote branch.

Interactive Rebase
git rebase -i HEAD~3

Opens an interactive editor for the last three commits. You can choose to reorder, squash, reword, or drop commits.

  • pick: Keep a commit.
  • reword: Change a commit message.
  • squash: Combine a commit with the previous commit.
  • drop: Remove a commit from the rebased history.
Important: Interactive rebase changes commit history. Avoid rewriting commits that others are already using unless the team has agreed on the procedure.

3. Resolving Rebase Conflicts

A rebase may stop when Git cannot automatically apply a commit. Resolve the conflicts before continuing.

Step 1: Identify Conflicts
git status
Step 2: Resolve the Files

Open each conflicted file, review the competing changes, remove the conflict markers, and save the correct code.

Step 3: Stage the Resolved File
git add filename.ext
Step 4: Continue Rebasing
git rebase --continue

Repeat these steps if another commit causes a conflict during the rebase.

Cancel the Rebase
git rebase --abort

Use this command to abandon the current rebase and return the branch to its pre-rebase state, provided the rebase is still in progress.


4. Merge vs. Rebase

Feature Merge Rebase
Purpose Combines branch histories. Replays commits onto another base.
History Preserves existing commits; may create a merge commit. Creates new commits for the replayed changes.
Conflict handling Resolve conflicts, then complete the merge. Resolve conflicts and continue the rebase.
Common use Integrating branches while preserving branch history. Updating a feature branch with a linear history.

5. Rebasing Best Practices

  • Commit or stash unfinished work before rebasing.
  • Use git status to check your repository state.
  • Rebase your own feature branches when appropriate.
  • Avoid rewriting shared commits without team coordination.
  • Test your code after the rebase completes.
Key Takeaway: Use git rebase to replay commits onto a new base and maintain a linear history. Use git rebase -i to organize local commits, git rebase --continue after resolving conflicts, and git rebase --abort to cancel an in-progress rebase.