Git & GitHub Course
Git & GitHub
/
Advanced

Interactive Rebase

Definition

A tool that allows you to alter individual commits in history. You can alter the commit message, reorder commits, split them, or squash multiple commits together.

Explain Like I'm New

A time machine with a surgical scalpel. You run `git rebase -i`, and Git opens a text file listing your last 5 commits. You can literally just re-arrange the lines of text to re-order the commits, or change the word 'pick' to 'drop' to entirely delete a commit from history.

Real World Example

Cleaning up a messy branch before sending it to your boss for review. You squash 10 messy 'wip', 'typo', 'fix' commits into one beautiful, professional 'Implement User Authentication' commit.

Common Use Cases

  • •Cleaning up local commit history
  • •Squashing commits

Terminal Output

bash / terminal
# Start an interactive rebase for the last 4 commits $ git rebase -i HEAD~4 /* Git opens your text editor with this: pick a1b2c3d Fix the CSS bug pick d4e5f6g Oops, missed a semicolon pick h7i8j9k Add tests pick l0m1n2o Update README # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # s, squash = use commit, but meld into previous commit # d, drop = remove commit */ # Change the text file to this, and save: pick a1b2c3d Fix the CSS bug squash d4e5f6g Oops, missed a semicolon <-- This merges into the commit above it! pick h7i8j9k Add tests drop l0m1n2o Update README <-- This commit is completely deleted!

Interview Questions

basic

  • What flag starts an Interactive Rebase?

intermediate

  • If you change 'pick' to 'reword' in the interactive text file, what happens?

Flash Cards

Question

What flag?

Click to reveal answer
Answer

`-i` (Example: `git rebase -i HEAD~3`).

Question

Reword?

Click to reveal answer
Answer

Git will pause on that specific commit during the rebase process and open your text editor so you can fix a typo in the commit message.