DEV Community

Cover image for Git - Use Interactive Rebasing to Clean Up Commits
Keyur Ramoliya
Keyur Ramoliya

Posted on

Git - Use Interactive Rebasing to Clean Up Commits

Interactive rebasing is a feature in Git that allows you to modify, reorganize, and clean up your commit history before pushing your changes to a remote repository. It's particularly useful for:

  1. Squashing Commits: If you have multiple small commits that logically belong together, you can squash them into a single commit. This helps to keep your commit history concise and makes it easier to understand.

  2. Reordering Commits: You can reorder commits to present them in a more logical order, making it easier for others (and your future self) to follow the history.

  3. Editing Commit Messages: You can amend commit messages to provide more descriptive and informative messages.

To perform an interactive rebase, use the following command:

git rebase -i HEAD~n
Enter fullscreen mode Exit fullscreen mode

Replace n with the number of commits you want to include in the interactive rebase (e.g., git rebase -i HEAD~5 for the last 5 commits).

Git will open an interactive text editor displaying a list of commits. You can then choose what to do with each commit (pick, squash, edit, reorder, etc.), save and exit the editor, and Git will perform the rebase according to your instructions.

Interactive rebasing is an excellent way to maintain a clean and well-structured commit history, making it easier for you and your collaborators to understand the evolution of your project.

Top comments (0)