Sobes.tech
Junior — Middle

How can multiple changes be combined into a single commit in a version control system?

sobes.tech AI

Answer from AI

To combine multiple changes into a single commit in Git, you can use the git rebase command or git reset followed by creating a new commit.

Interactive rebase method:

  1. Execute git rebase -i HEAD~N, where N is the number of recent commits you want to combine.
  2. In the opened editor, replace pick with squash (or s) for all commits that should be merged with the previous one.
  3. Save and close the editor, after which Git will prompt you to edit the message of the final commit.

Example:

git rebase -i HEAD~3
# In the editor:
pick abc123 Commit 1
squash def456 Commit 2
squash 789abc Commit 3

Alternative method:

  • Use git reset --soft HEAD~N to rollback N commits, keeping changes in the index, then make a new commit with the combined changes:
git reset --soft HEAD~3
git commit -m "Combined commit"

Both methods allow you to merge several commits into one for a cleaner history.

How can multiple changes be combined into a single… - sobes.tech