Junior — Middle
How can a sequence of commits be combined into a single merge?
sobes.tech AI
Answer from AI
To combine a sequence of commits into one (called squash) in Git, an interactive rebase is used:
# Start an interactive rebase for the last N commits
git rebase -i HEAD~N
In the opened editor, specify pick for the first commit, and for the rest, replace with squash or s — this will merge them into one commit. After saving and closing the editor, Git will prompt you to edit the message of the final commit.
This is convenient for simplifying history before merging branches.
You can also use git merge --squash to combine branch changes into a single commit without preserving individual commit history:
git checkout main
git merge --squash feature-branch
git commit -m "Merged commit"