Explain git rebase -i (interactive rebase). Describe its primary use cases and walk through an example of squashing multiple commits into one, including the steps and commands involved.
#Reveal answer guidance
git rebase -i <base-commit-hash> (or <branch-name>) allows you to rewrite commit history interactively. The <base-commit-hash> specifies the point in history where you want to start rewriting. Primary use cases include: (1) **Squashing commits**: Combining multiple small, WIP commits into a single, meaningful commit. (2) **Reordering commits**: Changing the order of commits. (3) **Editing commit messages**: Rewriting a commit message. (4) **Splitting commits**: Breaking a large commit into smaller ones. (5) **Deleting commits**: Removing unwanted commits. When you run git rebase -i, Git opens an editor with a list of commits and commands (pick, squash, edit, reword, drop, fixup). To squash multiple commits: 1. Identify the commits you want to squash. Let's say you have commit-A, commit-B, commit-C, and you want to squash B and C into A. 2. Run git rebase -i <commit-parent-of-A>. This opens an editor with commit-A, commit-B, commit-C listed. 3. Change the actions: pick <hash-A> commit-A; squash <hash-B> commit-B; fixup <hash-C> commit-C. squash combines the commit into the previous one and prompts for a new commit message. fixup combines and discards the commit's message. 4. Save and close the editor. Git will then apply the changes. If you chose squash, a new editor will open to combine commit messages. If conflicts arise, resolve them, git add ., and git rebase --continue. The result is a cleaner, linear history with fewer, more atomic commits.