0
0
Gitdevops~7 mins

Interactive rebase (git rebase -i) - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to clean up your commit history before sharing your work. Interactive rebase lets you change, combine, or reorder commits easily.
When you want to combine several small commits into one clear commit before pushing.
When you need to fix a typo or mistake in an earlier commit message.
When you want to reorder commits to make the history easier to understand.
When you want to remove unnecessary commits from your branch.
When you want to split a big commit into smaller logical commits.
Commands
Shows the last 5 commits in short form so you can decide which commits to edit or reorder.
Terminal
git log --oneline -5
Expected OutputExpected
a1b2c3d Fix typo in README b2c3d4e Add user login feature c3d4e5f Update styles for homepage d4e5f6a Improve error handling e5f6a7b Initial commit
--oneline - Shows each commit in one line for easy reading
-5 - Limits output to last 5 commits
Starts an interactive rebase for the last 3 commits so you can edit, reorder, or squash them.
Terminal
git rebase -i HEAD~3
Expected OutputExpected
pick c3d4e5f Update styles for homepage pick d4e5f6a Improve error handling pick e5f6a7b Add user login feature # Rebase a1b2c3d..e5f6a7b onto a1b2c3d (3 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # d, drop = remove commit # # These lines can be re-ordered; they are executed from top to bottom.
-i - Starts interactive mode
HEAD~3 - Specifies the last 3 commits to rebase
Shows the last 3 commits after rebase to verify changes.
Terminal
git log --oneline -3
Expected OutputExpected
f7g8h9i Update styles and improve error handling j8k9l0m Add user login feature m1n2o3p Initial commit
--oneline - Shows commits in one line
-3 - Limits output to last 3 commits
Key Concept

If you remember nothing else from interactive rebase, remember: it lets you rewrite your recent commit history safely to make it cleaner and clearer before sharing.

Common Mistakes
Trying to rebase commits that have already been pushed to a shared repository.
This can cause conflicts and confusion for others who have the old history.
Only use interactive rebase on local commits that have not been pushed yet.
Not saving and closing the editor properly during the interactive rebase.
The rebase will not start or will be aborted if the editor is not closed correctly.
Make sure to save changes and exit the editor (e.g., :wq in vim) to continue.
Using the wrong number after HEAD~ to select commits.
You might rebase too many or too few commits, causing unexpected changes.
Count commits carefully and use HEAD~N where N is the exact number of commits you want to edit.
Summary
Use 'git rebase -i HEAD~N' to start editing the last N commits interactively.
In the editor, you can reorder, squash, edit, or drop commits to clean history.
Always verify your commit history after rebase with 'git log --oneline'.