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
Interactive Rebase with git rebase -i
📖 Scenario: You are working on a small project with a few commits. You want to clean up your commit history before sharing your work with your team. This means combining some commits and editing commit messages to make the history clearer.
🎯 Goal: Learn how to use git rebase -i to interactively edit, squash, and reorder commits in your local git repository.
📋 What You'll Learn
Have git installed and a local git repository initialized
Have at least 3 commits in your current branch
Use git rebase -i to modify commit history
Understand basic git commands like git log and git commit
💡 Why This Matters
🌍 Real World
Cleaning up commit history before sharing code helps keep project history clear and understandable for everyone.
💼 Career
Using interactive rebase is a common skill for developers and DevOps engineers to maintain clean and professional git histories.
Progress0 / 4 steps
1
Create initial commits
Create three commits in your git repository with these exact commit messages in order: "Add README", "Add LICENSE", and "Update README". Use git commit with the -m option to set the messages.
Git
Hint
Use git commit --allow-empty -m "message" to create commits without changing files.
2
Start interactive rebase
Run git rebase -i HEAD~3 to start an interactive rebase for the last three commits.
Git
Hint
The command git rebase -i HEAD~3 opens the last 3 commits for editing.
3
Edit the rebase todo list
In the opened editor, change the second commit's action from pick to squash to combine it with the first commit. Leave the others as pick. Save and close the editor to continue.
Git
Hint
Change only the second commit's action from pick to squash in the editor that opens after running git rebase -i.
4
View the updated commit history
Run git log --oneline -3 to display the last three commits and verify that the second commit was squashed into the first.
Git
Hint
The output of git log --oneline -3 should show two commits with messages including "Add README" and "Update README" after squashing.
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
Step 1: Understand the purpose of interactive rebase
Interactive rebase allows you to change the order, combine, edit, or remove recent commits.
Step 2: Compare with other git commands
Creating branches, deleting history, or pushing commits are different git operations.
Final Answer:
To reorder, edit, or combine recent commits before sharing -> Option A
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
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.
Step 2: Check the options
HEAD^3 and HEAD^^3 are invalid for this purpose; HEAD~ alone is incomplete.
Final Answer:
git rebase -i HEAD~3 -> Option B
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
Step 1: Understand 'pick' and 'squash' in rebase
'pick' applies a commit as is; 'squash' combines the commit with the previous one.
Step 2: Analyze the new order
Commit B is picked first, Commit C is squashed into B, Commit A is picked last.
Final Answer:
Commits B and C will be combined, and Commit A will be last -> Option D
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
Step 1: Understand 'edit' in interactive rebase
When a commit is marked 'edit', rebase pauses to let you change it.
Step 2: Modify commit and continue rebase
You make changes, amend the commit with git commit --amend, then continue with git rebase --continue.
Final Answer:
Make changes, then run git commit --amend and git rebase --continue -> Option A
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?