An Interactive Rebase is an advanced feature of the Git version control system that allows you to revise, reorder, combine, or delete multiple commits in a branch. Unlike a standard rebase, where commits are simply "reapplied" onto a new base commit, an interactive rebase lets you manipulate each commit individually during the rebase process.
main
or master
), you can clean up the commit history by merging or removing unnecessary commits.Suppose you want to modify the last 4 commits on a branch. You would run the following command:
git rebase -i HEAD~4
1. Selecting Commits:
pick
, followed by the commit message.Example:
pick a1b2c3d Commit message 1
pick b2c3d4e Commit message 2
pick c3d4e5f Commit message 3
pick d4e5f6g Commit message 4
2. Editing Commits:
pick
commands with other keywords to perform different actions:
pick
: Keep the commit as is.reword
: Change the commit message.edit
: Stop the rebase to allow changes to the commit.squash
: Combine the commit with the previous one.fixup
: Combine the commit with the previous one without keeping the commit message.drop
: Remove the commit.Example of an edited list:
pick a1b2c3d Commit message 1
squash b2c3d4e Commit message 2
reword c3d4e5f New commit message 3
drop d4e5f6g Commit message 4
3. Save and Execute:
4. Resolving Conflicts:
git rebase --continue
.Interactive rebase is a powerful tool in Git that allows you to clean up, reorganize, and optimize the commit history. While it requires some practice and understanding of Git concepts, it provides great flexibility to keep a project's history clear and understandable.