Maintain clean Git history with strategic squashing and merging techniques.
Use squash and merge for pull requests to maintain clean Git history on main branches.
Use interactive rebase to manually squash commits before creating pull requests.
# Interactive rebase to squash commits
git checkout feature/user-dashboard
git rebase -i develop
# In the interactive editor, change 'pick' to 'squash' for commits to combine:
pick abc1234 feat: initial dashboard structure
squash def5678 wip: add user stats
squash ghi9012 fix: typo in component name
squash jkl3456 refactor: improve data fetching
# Save and exit. Git will prompt for the final commit message:
feat(dashboard): implement user dashboard with statistics
# Result: One clean commit with all changes
# Push the squashed branch
git push --force-with-lease origin feature/user-dashboard Most Git hosting platforms (GitHub, GitLab, Bitbucket) offer squash and merge options.
# When merging PR/MR, use platform's "Squash and merge" option
# This combines all commits into one with a clean message:
feat(auth): implement user authentication system (#42)
* Initial login form implementation
* Add password validation
* Integrate with OAuth2 provider
* Add comprehensive tests
* Address code review feedback Use "Squash and merge" button. GitHub will combine all commits and let you edit the final commit message before merging.
Enable "Squash commits when merge request is accepted" in the merge request settings.
Squash all commits in a feature branch into a single commit when merging to develop.
# Feature branch commit history
* f7f3f6d feat: add login form
* 310154e wip: form validation
* a5f4a0d fix: typo in validation message
* c5f4a0d fix: address code review feedback
* d6g7h8i test: add authentication tests
# After squash merge to develop
* 1a2b3c4 feat(auth): implement user login with validation (#45) Group related commits together while keeping logically separate changes distinct.
# Original commits
* abc1234 feat: add user model
* def5678 feat: add user validation
* ghi9012 feat: add user controller
* jkl3456 test: add user model tests
* mno7890 test: add user controller tests
* pqr1234 docs: update API documentation
# After logical squashing
* xyz7890 feat(user): implement user management system
* abc4567 test(user): add comprehensive user tests
* def8901 docs(api): update user endpoint documentation When squashing commits from multiple authors, include co-author credits:
feat(auth): implement OAuth2 authentication (#42)
Co-authored-by: Jane Smith <[email protected]>
Co-authored-by: Bob Johnson <[email protected]>