Workflows
Git Interactive Rebase
Interactive Rebase
Git rebase -i rewrites history with squash and edit.
Understanding Git Interactive Rebase
Git Interactive Rebase is a powerful tool that allows developers to rewrite commit history. It offers granular control over the commits in a branch, enabling actions such as squashing multiple commits into one, editing commit messages, and reordering commits.
By using the git rebase -i
command, you can clean up your commit history before pushing changes to a shared repository, making it easier for others to follow your project's development.
Starting an Interactive Rebase
To begin an interactive rebase, check out the branch you want to work on and run the following command:
This command opens an editor with the last 3 commits on the feature-branch
. You can adjust the number of commits by modifying the number after HEAD~
.
The Interactive Rebase Editor
The interactive rebase editor displays a list of commits. By default, each line starts with the word pick
, which means the commit will be included in the rebase as is. You can replace pick
with other commands:
squash
(ors
): Combine this commit with the previous one.edit
(ore
): Pause to edit this commit.reword
(orr
): Change the commit message.drop
: Remove the commit.
In this example, the second commit will be squashed into the first, resulting in a single commit with a combined message. After saving and closing the editor, Git will prompt you to edit the commit message for the squashed commit.
Editing Commits During Rebase
If you choose to edit
a commit, Git will pause the rebase process and allow you to make changes. You can amend the commit using:
After making the necessary changes, continue the rebase process with:
Finalizing the Interactive Rebase
Once all edits are complete and you've saved your changes, the interactive rebase will finish. It's essential to ensure everything looks correct before pushing to a remote repository, especially if you've rewritten history that others may have based work upon.
Use git log
to verify your commit history, and then push your changes:
Remember, using --force
is necessary when rewriting history, but it should be used cautiously as it can overwrite others' work.
Workflows
- Worktrees
- Sparse Checkout
- Partial Clone
- LFS
- Commit Amend
- Interactive Rebase
- Squash Commits
- Split Commits
- Previous
- Commit Amend
- Next
- Squash Commits