← Workflows

阅读中文版本

Git Rebase Interactive Workflow

Use interactive rebase to squash, reorder, or edit commits before merging a feature branch into main.

beginner~20 mingitrebaseworkflow

Objective

Clean up commit history on a feature branch before merging to maintain a readable main branch.

Environment

  • A feature branch with multiple commits
  • Git CLI with interactive rebase support
  • Understanding of rebase commands

Steps

1. Start rebase

Run git rebase -i main to open the interactive rebase editor for the feature branch.

Expected state: An editor opens with all feature branch commits listed.

2. Edit commits

Use squash, reword, or drop to clean up the commit history as desired.

Expected state: The commit list is modified according to the rebase plan.

3. Complete rebase

Save and close the editor to apply the rebase, resolving conflicts if needed.

Expected state: The feature branch has a clean commit history.

4. Force push

Force push the rebased branch with git push --force-with-lease.

Expected state: The remote branch is updated with the clean history.

Decision points

Squash all commits into one?

  • Yes, for simple features
  • No, keep logical commits

Squash trivial commits; keep commits that represent logical, independently understandable changes.

Expected outcome

The feature branch has a clean, readable commit history ready for merge into main.

Verification

Run git log to confirm the commit history is clean; verify the branch builds and tests pass after rebase.

Cleanup

Use git rebase --abort to cancel the rebase if something goes wrong.

Clean history helps reviewers

A clean commit sequence makes code review faster and the project history easier to understand.

git rebase -i main
# In editor: pick, squash, reword as needed
git push --force-with-lease

Implementation Notes

  1. Start rebase — Run git rebase -i main to open the interactive rebase editor for the feature branch. Expected state: An editor opens with all feature branch commits listed.

  2. Edit commits — Use squash, reword, or drop to clean up the commit history as desired. Expected state: The commit list is modified according to the rebase plan.

  3. Complete rebase — Save and close the editor to apply the rebase, resolving conflicts if needed. Expected state: The feature branch has a clean commit history.

  4. Force push — Force push the rebased branch with git push –force-with-lease. Expected state: The remote branch is updated with the clean history.

Common Pitfalls

  • Squash all commits into one? Options: Yes, for simple features / No, keep logical commits. Recommendation: Squash trivial commits; keep commits that represent logical, independently understandable changes.