Search

Search IconIcon to open search

Git Rebase

Last updatedUpdated: by Jakub Žovák · 2 min read

Properties
tags cscs/swe
created 15.03.2026, 00:00
modified Empty
published Empty
topics Git, Rebase
authors Empty
ai-assisted Yes

Rebasing re-applies commits on top of another base, rewriting history to produce a linear commit graph. See also Merging vs rebasing for a conceptual comparison with merging.

# Commands

# Basic Rebase

1
2
3
git rebase <base>                                    # rebase current branch onto <base>
git rebase main feature                              # rebase feature onto main (no checkout needed)
git rebase --onto <newbase> <upstream> <branch>      # transplant <branch> onto <newbase>

# Interactive Rebase

1
2
git rebase -i HEAD~<n>             # interactively edit last n commits
git rebase -i <commit-hash>        # interactively edit commits after <commit-hash>

Interactive actions available per commit:

  • pick — keep commit as-is
  • reword — keep commit, edit message
  • edit — pause to amend the commit
  • squash — meld into previous commit, combine messages
  • fixup — meld into previous commit, discard message
  • drop — remove commit entirely
  • exec — run a shell command after the commit

# Conflict Resolution

1
2
3
git rebase --continue              # after resolving conflicts, continue
git rebase --skip                  # skip the current conflicting commit
git rebase --abort                 # abort and return to pre-rebase state

# Autosquash

1
2
3
git commit --fixup=<commit>        # mark commit as fixup for <commit>
git commit --squash=<commit>       # mark commit as squash for <commit>
git rebase -i --autosquash HEAD~n  # auto-arrange fixup!/squash! commits

# Notes

  • Never rebase public/shared branches — rewrites history and diverges others’ copies (Merging vs rebasing)
  • --onto is useful for moving a stack of commits to a completely different base
  • Interactive rebase is the primary tool for cleaning up local commit history before a PR