Interactive Rebase in Git
Introduction to Interactive Rebase in Git
Introduction to Interactive Rebase in Git
Interactive rebase in Git is a powerful tool that allows you to edit and reorder commits on a branch before applying them to the main branch. It is useful for cleaning up commit history, merging commits, and resolving conflicts.
What is an Interactive Rebase?
An interactive rebase lets you stop and edit individual commits on a branch before merging them with another branch. This is especially useful for cleaning up commit history, as you can combine, modify, or delete commits before completing the rebase.
Why Use Interactive Rebase?
Some reasons why you might want to use interactive rebase include:
- Combining several small commits into one: Makes reading the commit history easier.
- Editing previous commit messages: To correct typos or add additional information.
- Reordering commits: So that they make more logical sense.
- Deleting unnecessary commits: That might contain irrelevant or erroneous changes.
Basic Example
Suppose you have the following commits on your branch:
plaintext
To start an interactive rebase, use the following command:
bash
This will open an interface where you can specify what to do with each commit:
plaintext
Interactive Rebase Interface
When you perform an interactive rebase, your default text editor will open with a list of commits. Each line contains the commit hash and the commit message.
You can replace the word pick
at the beginning of each line with different commands:
pick
(orp
): Use the commit as is.reword
(orr
): Edit only the commit message.edit
(ore
): Pause to modify the commit.squash
(ors
): Combine this commit with the previous one.fixup
(orf
): Similar to squash but discards the commit message.exec
(orx
): Execute a shell command.drop
(ord
): Discard the commit.