Skip to content

Home

Perform an interactive rebase in Git

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.

Perform an interactive rebase

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.

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.

Additionally, you can use the --autosquash option to automatically squash fixup commits into the commits they are fixing.

💬 Note

You can read more about fixup commits in the relevant article.

# 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

Interactive rebase actions

The options that are available to you during an interactive rebase are multiple, but the ones that are most often used are the following:

You can either use the full command or the shorthand version when specifying the action for each commit. Below is an example of a commit list in an interactive rebase:

p c191f90c7 Initial commit        # Keep this commit
pick 3050fc0de Fix network bug    # Keep this commit
r 7b1e3f2a2 Update README         # Edit the commit message
d 3e4f5d6a7 Commit sensitive data # Remove this commit
edit 9a8b7c6d5 Add new feature    # Stop for amending
pick 1a2b3c4d5 Fix bug            # Keep this commit
f 6d5c4b3a2 Add new feature       # Squash this fixup commit
pick 5a6b7c8d9 Update README      # Keep this commit
s 4b3c2d1a0 Update README         # Squash this commit

More like this

Start typing a keyphrase to see matching snippets.