Merge & Rebase

Merging a branch back in

When your feature branch is ready, you merge it into main. First switch to the branch you want to merge into (usually main), then merge the feature branch.

main merge feature
A merge commit ties two branches together — main now contains the feature's work.

Merge feature into main

Terminal
$ git switch main
$ git merge feature-login
In VS Code
  1. Switch to main (status-bar branch picker)
  2. Open Command Palette → Git: Merge Branch…
  3. Pick the branch to merge in

Order matters: you stand on the destination branch and pull the other one in.

Fast-forward vs merge commit

If main hasn't changed since you branched, Git just slides main forward to catch up — a fast-forward, no extra commit. If both branches moved on, Git creates a merge commit that has two parents, joining the two histories. Both are normal.

Merge vs rebase — the short version

  • Merge keeps both histories exactly as they happened and adds a merge commit to join them. Honest and safe — great default for beginners.
  • Rebase rewrites your branch's commits so they appear to start from the latest main, giving a clean, straight line. Tidier history, but it rewrites commits.

Rebase your feature onto the latest main

Terminal
$ git switch feature-login
$ git rebase main
In VS Code
  1. Open Command Palette → Git: Rebase Branch…
  2. Choose main as the base

Golden rule: never rebase commits you've already pushed and shared, unless you really know why — it rewrites history others may depend on.

When in doubt, merge. It's the safe choice and you can always learn rebase later. Many teams happily use merge forever.

Hit a conflict? Two branches changed the same lines and Git paused to ask which to keep. It's a question, not an error — Fix Merge Conflicts walks through reading the markers, VS Code's Merge Editor, and the git merge --abort escape hatch.

After a successful merge

With the feature merged into main, delete the branch to keep things tidy — the work is safely in main's history now.

Clean up the merged branch

Terminal
$ git branch -d feature-login
$ git push origin --delete feature-login   # if it was also pushed to GitHub
In VS Code
  1. Delete locally from the branch picker; on GitHub, the PR page shows a 'Delete branch' button after merging

Two handy extras: after staging resolved files you can finish a conflicted merge with git merge --continue (same effect as committing). And git pull --rebase updates your branch without adding a merge commit, for a cleaner straight line.

Up next: the moment every beginner dreads and shouldn't — a merge conflict, handled calmly in the Merge Editor.

FAQ

Frequently Asked Questions

What is the difference between merge and rebase?
Merge keeps both histories and adds a merge commit to join them — the safe default. Rebase rewrites your commits onto the latest main for a straight line; tidier, but never rebase commits you've already shared.
What is a fast-forward merge?
When main hasn't moved since you branched, Git doesn't need a merge commit — it just slides the main label forward to your branch's tip. That's a fast-forward: same result, no extra commit.
Should I delete a branch after merging it?
Usually yes — once it's merged, git branch -d feature keeps things tidy. Its commits live on inside main.
Should I merge or rebase to update my branch with the latest main?
Either works. Merging main in is the safe default; git rebase main gives a straighter history but rewrites your commits — fine while the branch is only yours, risky once it's shared.