Git Rebase Command Examples

git rebase replays commits on top of a different base, giving you a clean linear history. Interactive rebase lets you squash, reorder, reword, or drop commits before merging. It is a powerful tool for keeping feature branches up to date and producing polished pull requests.

GitRebaseVersion ControlCLI
Basic Rebase
Rebase current branch onto main
git rebase main
Replays all commits from the current feature branch on top of the latest main. This produces a linear history without a merge commit. After rebasing, a fast-forward merge to main is possible.
Rebase onto remote main
git fetch origin && git rebase origin/main
Fetch the latest changes from origin first, then rebase onto the remote main branch. This is the standard workflow for keeping a feature branch up to date with the main branch on GitHub or GitLab.
Pull with rebase instead of merge
git pull --rebase origin main
Instead of creating a merge commit when pulling, --rebase replays your local commits on top of the incoming commits. This keeps your history linear. Set as default with git config pull.rebase true.
Interactive Rebase
Interactively edit last N commits
git rebase -i HEAD~3
Opens your editor with the last 3 commits listed. You can reorder lines, change the command keyword, or delete lines. Commands: pick (keep), reword (edit message), squash (combine into previous), drop (delete commit).
Squash all feature branch commits
git rebase -i $(git merge-base HEAD main)
Finds the commit where the feature branch diverged from main and opens interactive rebase from that point. This lets you squash all feature commits into one clean commit before merging, regardless of how many commits there are.
Auto-squash fixup commits
git rebase -i --autosquash HEAD~5
The --autosquash flag automatically reorders and marks commits that start with fixup! or squash! to be squashed into their target commits. Create such commits with git commit --fixup SHA.
Conflict Resolution
Continue after resolving conflicts
# 1. Resolve conflicts in the conflicting files
# 2. Stage the resolved files
git add resolved-file.txt
# 3. Continue the rebase
git rebase --continue
When a rebase hits a conflict, it pauses and lets you resolve it manually. Edit the conflicting files, stage them with git add, and then run git rebase --continue to proceed with the remaining commits.
Abort a rebase in progress
git rebase --abort
Cancels the rebase and returns your branch to exactly the state it was in before you started. Use this if the conflicts are too complex or you realize the rebase approach was wrong. Nothing is permanently lost.
Skip a conflicting commit
git rebase --skip
Discards the current commit that caused the conflict and moves on to the next one. Use this only when the conflicting commit's changes are already included elsewhere and the commit itself is no longer needed.

How to Use

  1. Run git rebase main on a feature branch to replay its commits on top of latest main.
  2. Use git rebase -i HEAD~N to interactively clean up the last N commits.
  3. In the editor, change pick to squash to combine commits or drop to remove them.
  4. Resolve any conflicts with git add + git rebase --continue.
  5. Never rebase commits that have already been pushed and shared with others.

Frequently Asked Questions

What is the difference between rebase and merge?

Merge combines two branch histories with a merge commit, preserving all commits as they happened. Rebase replays commits to create a linear history with no merge commit. Rebase produces cleaner git logs but rewrites commit SHAs, which is why you should never rebase shared/public branches.

Is it safe to rebase a feature branch?

Yes, if the branch is only used by you locally or the team agrees to force-push after rebasing. Rebasing rewrites commit history (new SHAs), so anyone else who has pulled the old branch will have diverged history. Always communicate with teammates before rebasing shared branches.

What is the difference between squash and fixup in interactive rebase?

Both combine a commit into the one above it. squash opens the editor so you can write a combined commit message from both. fixup silently discards the squashed commit's message and uses only the previous commit's message. Use fixup for minor typo fixes and squash for meaningful message edits.

Related Tools