How to Merge Git Commits |Video upload date:  · Duration: PT4M0S  · Language: EN

Step by step guide to combine multiple Git commits into a clean single commit using interactive rebase reset and amend

If your git history looks like a messy diary of tiny mistakes and awkward experiments you do not need to live with it. This guide shows how to fold noisy commits into a single neat commit using interactive rebase, soft reset, amend and a cautious force push. It is accurate, a little snarky, and will save future you from regret.

When to merge commits

Merge commits when several small commits represent one logical change. That keeps version control readable and makes bisecting less painful. Do not rewrite commits that others rely on unless you like awkward team meetings.

Interactive rebase method

This is the classic approach when you want control over which commits get combined and how the commit message looks.

  1. Pick the range of commits to clean up. For the last three commits run git rebase -i HEAD~3.
  2. An editor opens with lines that start with pick. Change the extra lines to squash or s to fold them into the first commit.
  3. Save and close the editor. Git will prompt to merge commit messages. Tweak the final message or accept the default.
  4. If you need to abort at any time run git rebase --abort and breathe.

Soft reset method

If you hate text editors or you just want a quick fold without line by line fiddling use a soft reset. This moves the commits back into the index so you can make one new commit.

git reset --soft HEAD~2
git commit -m "New combined message"

This is handy for simple merges of recent commits.

Tweak the tip with amend

Want to change the message of the commit you just created or update its contents without creating new history noise use amend.

git commit --amend --no-edit

Drop --no-edit if you want to rewrite the message. The amend command replaces the tip with a new commit object so the ID changes.

Pushing rewritten history safely

If the branch is on a remote you must update it with a force push. Use the safer form which checks for unexpected remote changes first.

git push --force-with-lease

Communicate with your teammates before doing this. Surprise history rewrites are only fun for the person doing them.

Quick safety net

  • Create a temporary branch before you rewrite history for an easy rollback with git branch backup-before-squash HEAD.
  • Or note the current commit id with git rev-parse HEAD so you can find it in reflog if needed.

Final thoughts

Squeeze the noise out of your commit history when it helps clarity. Use interactive rebase for granular control, soft reset for quick fixes and amend for small tweaks. Always keep a backup or a note of the original ref and warn your team before you push rewritten history to a shared remote. Your future self will thank you or at least be less annoyed.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.