HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Perform an interactive rebase in Git

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
performrebaseinteractivegit

Problem

If you want to rewrite the commit history of a branch, Git provides an interactive rebase feature that allows you to reorder, squash, and edit commits interactively. This can be useful for cleaning up your commit history before merging a branch or for combining multiple commits into a single one.
Simply using git rebase -i <commit> will open an editor with a list of commits and actions to perform for each one. You can reorder the commits, squash them together, edit commit messages, and more.
> [!TIP]
>
> If you want to learn how to configure Git's default text editor before using this command, check the linked article.

Solution

# Syntax: git rebase -i [--autosquash] <commit>

git rebase -i 3050fc0de
# Performs an interactive rebase starting from `3050fc0de`

git rebase -i --autosquash HEAD~5
# Performs an interactive rebase of the last 5 commits,
# automatically squashing fixup commits


> [!TIP]
>
> If you want to learn how to configure Git's default text editor before using this command, check the linked article.
If you encounter merge conflicts or need to stop the rebase to make changes, you can continue the rebase when ready using git rebase --continue or abort it using git rebase --abort.
Similarly, if you want to edit the list of commits after stopping the rebase, you can use git rebase -i --edit-todo to open the list in the editor again. This can be particularly useful, if the rebase stops due to some error or conflict.
Additionally, you can use the --autosquash option to automatically squash fixup commits into the commits they are fixing.

Code Snippets

# Syntax: git rebase -i [--autosquash] <commit>

git rebase -i 3050fc0de
# Performs an interactive rebase starting from `3050fc0de`

git rebase -i --autosquash HEAD~5
# Performs an interactive rebase of the last 5 commits,
# automatically squashing fixup commits

Context

From 30-seconds-of-code: interactive-rebase

Revisions (0)

No revisions yet.