Bird
Raised Fist0
Gitdevops~10 mins

Interactive rebase (git rebase -i) - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Process Flow - Interactive rebase (git rebase -i)
Start: git rebase -i HEAD~3
Open editor with last 3 commits
User edits commands (pick, squash, reword)
Save and close editor
Git applies commits one by one
If conflict
Resolve conflict
git rebase --continue
Rebase finishes successfully
Updated commit history
Interactive rebase lets you edit, reorder, squash, or fix commits by opening an editor and applying changes step-by-step.
Execution Sample
Git
git rebase -i HEAD~3
# Edit commands in editor
# Save and close
# Resolve conflicts if any
# git rebase --continue
This command starts an interactive rebase of the last 3 commits allowing you to modify commit history.
Process Table
StepActionEditor ContentGit AppliesResult
1Run 'git rebase -i HEAD~3'Shows last 3 commits with 'pick' commandsNo commits applied yetEditor open for user commands
2User changes second commit from 'pick' to 'squash'First commit pick, second squash, third pickNo commits applied yetWaiting for user to save and close editor
3User saves and closes editorCommands acceptedGit applies first commitFirst commit applied successfully
4Git applies second commit (squash)Squashes second commit into firstCombines commit messagesCommit history updated with combined commit
5Git applies third commitPick third commitThird commit appliedAll commits applied successfully
6Rebase completeNo editorNo actionCommit history rewritten with changes
💡 All commits applied or combined; rebase finished successfully
Status Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
Commit HistoryC1 - C2 - C3C1 - C2 - C3C1+2 (squashed) - C3C1+2 - C3C1+2 - C3 (final)
Editor StateClosedClosedClosedClosedClosed
Rebase StatusNot startedApplying commitsApplying commitsApplying commitsFinished
Key Moments - 3 Insights
Why does git open an editor during interactive rebase?
Git opens the editor to let you choose what to do with each commit (pick, squash, reword). This is shown in execution_table step 1 and 2.
What happens if you change 'pick' to 'squash' for a commit?
The commit is combined with the previous one, merging their changes and messages. See execution_table step 4 for this action.
How do you continue the rebase after resolving conflicts?
You run 'git rebase --continue' to tell git to proceed applying commits. This is part of the flow after conflicts, shown in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the commit history after step 4?
AC1 - C3
BC1 - C2 - C3
CC1+2 (squashed) - C3
DC2 - C3
💡 Hint
Check the 'Commit History' row in variable_tracker after step 4
At which step does the user save and close the editor?
AStep 1
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column in execution_table where editor commands are accepted
If the user changes all 'pick' commands to 'reword', what changes in the process?
AGit prompts to edit commit messages one by one
BCommits are reordered
CCommits are combined
DRebase stops immediately
💡 Hint
Recall what 'reword' means in interactive rebase and how git applies commits
Concept Snapshot
git rebase -i HEAD~N
- Opens editor with last N commits
- Commands: pick, squash, reword, etc.
- Save and close to apply changes
- Resolve conflicts if any, then git rebase --continue
- Rewrites commit history interactively
Full Transcript
Interactive rebase with 'git rebase -i' lets you change commit history by opening an editor showing recent commits. You can pick, squash, or reword commits. After editing commands, git applies commits one by one, combining or changing them as instructed. If conflicts occur, you resolve them and continue. This process updates your commit history cleanly and interactively.

Practice

(1/5)
1. What is the main purpose of using git rebase -i (interactive rebase)?
easy
A. To reorder, edit, or combine recent commits before sharing
B. To create a new branch from the current commit
C. To permanently delete the entire commit history
D. To push commits directly to the remote repository

Solution

  1. Step 1: Understand the purpose of interactive rebase

    Interactive rebase allows you to change the order, combine, edit, or remove recent commits.
  2. Step 2: Compare with other git commands

    Creating branches, deleting history, or pushing commits are different git operations.
  3. Final Answer:

    To reorder, edit, or combine recent commits before sharing -> Option A
  4. Quick Check:

    Interactive rebase = reorder/edit commits [OK]
Hint: Interactive rebase = rewrite recent commits easily [OK]
Common Mistakes:
  • Confusing rebase with branch creation
  • Thinking it deletes all history
  • Assuming it pushes commits automatically
2. Which of the following is the correct command to start an interactive rebase for the last 3 commits?
easy
A. git rebase -i HEAD^^3
B. git rebase -i HEAD~3
C. git rebase -i HEAD~
D. git rebase -i HEAD^3

Solution

  1. Step 1: Recall the syntax for interactive rebase

    The correct syntax uses HEAD~N to specify the last N commits, so HEAD~3 means last 3 commits.
  2. Step 2: Check the options

    HEAD^3 and HEAD^^3 are invalid for this purpose; HEAD~ alone is incomplete.
  3. Final Answer:

    git rebase -i HEAD~3 -> Option B
  4. Quick Check:

    HEAD~3 selects last 3 commits [OK]
Hint: Use HEAD~N to select last N commits for rebase [OK]
Common Mistakes:
  • Using HEAD^3 instead of HEAD~3
  • Omitting the number after ~
  • Using double carets ^^ incorrectly
3. Given the following interactive rebase todo list:
pick a1b2c3 Commit A
pick d4e5f6 Commit B
pick 789abc Commit C

If you change it to:
pick d4e5f6 Commit B
squash 789abc Commit C
pick a1b2c3 Commit A

What will happen after completing the rebase?
medium
A. Only Commit C will be applied, others dropped
B. Commits A and B will be combined, Commit C will be last
C. The rebase will fail due to invalid order
D. Commits B and C will be combined, and Commit A will be last

Solution

  1. Step 1: Understand 'pick' and 'squash' in rebase

    'pick' applies a commit as is; 'squash' combines the commit with the previous one.
  2. Step 2: Analyze the new order

    Commit B is picked first, Commit C is squashed into B, Commit A is picked last.
  3. Final Answer:

    Commits B and C will be combined, and Commit A will be last -> Option D
  4. Quick Check:

    squash merges commits; order changed [OK]
Hint: Squash merges commit into previous one in order [OK]
Common Mistakes:
  • Thinking squash drops commits
  • Assuming order stays same after rebase
  • Confusing squash with fixup
4. You run git rebase -i HEAD~2 and change the second commit's action from pick to edit. After saving, what should you do next to modify that commit?
medium
A. Make changes, then run git commit --amend and git rebase --continue
B. Run git push immediately to update remote
C. Abort the rebase with git rebase --abort
D. Run git reset --hard HEAD~1 to undo the commit

Solution

  1. Step 1: Understand 'edit' in interactive rebase

    When a commit is marked 'edit', rebase pauses to let you change it.
  2. Step 2: Modify commit and continue rebase

    You make changes, amend the commit with git commit --amend, then continue with git rebase --continue.
  3. Final Answer:

    Make changes, then run git commit --amend and git rebase --continue -> Option A
  4. Quick Check:

    Edit = amend commit + continue rebase [OK]
Hint: Edit commit: amend changes, then continue rebase [OK]
Common Mistakes:
  • Pushing before finishing rebase
  • Aborting instead of continuing
  • Resetting commits incorrectly
5. You want to clean up your last 4 commits by combining the second and third commits into one, and also reorder commits so the last commit becomes the first. Which interactive rebase todo list correctly achieves this?
pick 111aaa Commit 1
pick 222bbb Commit 2
pick 333ccc Commit 3
pick 444ddd Commit 4
hard
A.
pick 444ddd Commit 4
squash 222bbb Commit 2
pick 333ccc Commit 3
pick 111aaa Commit 1
B.
pick 444ddd Commit 4
pick 111aaa Commit 1
squash 222bbb Commit 2
pick 333ccc Commit 3
C.
pick 444ddd Commit 4
pick 111aaa Commit 1
pick 222bbb Commit 2
squash 333ccc Commit 3
D.
pick 111aaa Commit 1
pick 333ccc Commit 3
squash 222bbb Commit 2
pick 444ddd Commit 4

Solution

  1. Step 1: Reorder commits to put last commit first

    Commit 4 (444ddd) should be first in the list to reorder it first.
  2. Step 2: Combine second and third commits

    To combine commits 2 and 3, use 'pick' on commit 2 and 'squash' on commit 3 immediately after.
  3. Step 3: Verify the todo list

    pick 444ddd Commit 4
    pick 111aaa Commit 1
    pick 222bbb Commit 2
    squash 333ccc Commit 3
    places commit 4 first, then commit 1, then picks commit 2 and squashes commit 3, matching requirements.
  4. Final Answer:

    pick 444ddd Commit 4
    pick 111aaa Commit 1
    pick 222bbb Commit 2
    squash 333ccc Commit 3
    -> Option C
  5. Quick Check:

    Reorder + squash =
    pick 444ddd Commit 4
    pick 111aaa Commit 1
    pick 222bbb Commit 2
    squash 333ccc Commit 3
    [OK]
Hint: Put last commit first, squash 3rd into 2nd commit [OK]
Common Mistakes:
  • Squashing commits in wrong order
  • Not moving last commit to first position
  • Using squash on wrong commits