Cleaning up your commits with Interactive rebase

Interactive Rebase Guide

Interactive rebasing is an effective approach for modifying your commits, such as squashing them together or renaming them.

Configuration

TIP

Add the following setting and alias to your .gitconfig. You can open your .gitconfig with vi ~/.gitconfig in VIM mode.

abbreviateCommands = true This ensures that keys like pick are displayed as p, which will speed up your workflow.

rbi = !sh -c \"git rebase -i $(git merge-base $1 HEAD)\" - This alias helps you avoid thinking about the rebase range.

Steps for Interactive Rebasing

  1. Prepare Your Branch
  • Make commits on a branch that is not main.
  1. Start Interactive Rebase
  • Use git rbi main where main is the branch you want to compare your changes with.
  • Enter the interactive rebase dialog.
  1. Interactive Rebase Options
  • pick/p: Keep the commit as is.
  • rename/r: Renames the commit. Note that you cannot rename commits in this initial screen.
  • squash/s: Squashes the commit into the one above.
  • drop/d: Removes the commit.
  • Change the words/letters before the commit hash to indicate what you want to do with this commit. You can also shuffle the commit order here.
  1. Execute the Rebase Plan
  • After planning, start the rebase with esc key + :wq + enter key.
  • Depending on your actions, either pick the commit name/description to keep (when squashing) or rename your commit.
  • After each step, continue with esc key + :wq + enter key.

Repeat this process until you have completed your modifications.