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 ControlCLIgit rebase main
git fetch origin && git rebase origin/main
git pull --rebase origin main
--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.git rebase -i HEAD~3
pick (keep), reword (edit message), squash (combine into previous), drop (delete commit).git rebase -i $(git merge-base HEAD main)
git rebase -i --autosquash HEAD~5
--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.# 1. Resolve conflicts in the conflicting files # 2. Stage the resolved files git add resolved-file.txt # 3. Continue the rebase git rebase --continue
git add, and then run git rebase --continue to proceed with the remaining commits.git rebase --abort
git rebase --skip
git rebase main on a feature branch to replay its commits on top of latest main.git rebase -i HEAD~N to interactively clean up the last N commits.pick to squash to combine commits or drop to remove them.git add + git rebase --continue.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.
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.
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.