How to squash your last N commits together

Posted on

To squash your last N commits together in Git involves combining multiple consecutive commits into a single commit. This process is useful for cleaning up commit history before pushing changes to a remote repository or for creating meaningful, atomic commits. By using interactive rebase in Git, you can specify the number of commits to squash and interactively manage the commit history to maintain clarity and organization.

Using Interactive Rebase for Squashing Commits

1. Start Interactive Rebase

  • To begin squashing commits, open your terminal and use the git rebase command with the -i or --interactive flag followed by the commit ID or branch name from which you want to rebase.
    git rebase -i HEAD~N
  • Replace N with the number of commits you want to squash. For example, HEAD~3 for squashing the last 3 commits.

2. Squash Commits

  • After executing the command, a text editor opens with a list of commits and their actions. Each commit is prefixed with pick. To squash commits, change pick to squash (or s) for the commits you want to combine.
    pick abc123 Commit message 1
    squash def456 Commit message 2
    squash xyz789 Commit message 3
  • Save and close the editor to proceed.

Managing Interactive Rebase

1. Editing Commit Messages

  • Once you squash commits, Git prompts you to edit the resulting commit message. Modify the combined commit message if needed, then save and close the editor.

    # This is a combination of 3 commits.
    # The commit message of the squashed commit.
    
    Changed some feature
  • This message summarizes the changes from the squashed commits.

2. Resolving Conflicts

  • During rebase, Git may pause to resolve conflicts if changes overlap between squashed commits or with the base branch. Resolve conflicts as prompted, add changes with git add, and continue with git rebase --continue.
    git add 
    git rebase --continue
  • Repeat until all conflicts are resolved.

Pushing Squashed Commits

1. Force Pushing

  • After squashing commits locally, force push changes to update the remote branch with the new commit history.
    git push origin  --force
  • Replace “ with your branch name. Exercise caution when force pushing, as it overwrites remote history.

2. Communicating Changes

  • Inform team members about the squashed commits and the new commit history to ensure everyone is aligned with the latest changes in the repository.

Best Practices for Squashing Commits

1. Grouping Related Changes

  • Squash commits that logically belong together, focusing on grouping related changes into meaningful commits for clearer history.

2. Reviewing History

  • Before squashing, review commit history to understand the changes and their relationships. Ensure that squashing enhances clarity and readability.

Handling Mistakes and Rollbacks

1. Undoing Rebase

  • If you make a mistake during rebase, use git rebase --abort to cancel the rebase operation and return to the previous state.
    git rebase --abort
  • This command restores the repository to its state before the rebase started, preserving original commit history.

2. Backing Up Commits

  • Before squashing, create a backup branch (git branch backup-branch) to safeguard against unintended changes or mistakes during the interactive rebase process.

Visualizing Changes

1. Using Git GUI Tools

  • Git GUI tools like GitKraken, SourceTree, or Git GUI provide visual interfaces for managing interactive rebase operations, simplifying the process of squashing commits with graphical representations and options.

2. Checking Git Log

  • After squashing commits, use git log to verify the updated commit history, ensuring that the squashed commits appear as a single commit with a combined message.

Summary

Squashing commits in Git streamlines commit history by consolidating multiple commits into cohesive units, enhancing readability and maintaining a cleaner repository history. By leveraging interactive rebase (git rebase -i), developers can efficiently manage and squash commits, ensuring that each commit accurately reflects cohesive changes and aligns with project standards. Understanding how to squash commits empowers teams to maintain a concise and organized Git history, facilitating collaboration, code review, and project management in software development workflows.

Posted in Uncategorized