Sobes.tech
Junior — Middle

Explain the differences between Git amend and squash commands in version control.

sobes.tech AI

Answer from AI

git commit --amend allows you to modify the last commit: add changes, fix the message or files. This is useful if you've just made a commit and want to adjust it without creating a new one.

git rebase -i with the squash option is used to combine multiple commits into one. This is often done before publishing a branch to clean up the history and make it more readable.

Key differences:

  • amend only changes the last commit.
  • squash merges several commits into one.

Example:

# Fix the last commit
git commit --amend

# Combine the last 3 commits
git rebase -i HEAD~3
# In the interactive editor, replace 'pick' with 'squash' for the 2nd and 3rd commits

Thus, amend is for small fixes to the last commit, squash is for organizing and compressing the history of multiple commits.