git merge
Merging combines the changes from one branch into another. Typically, you merge a feature branch back into `main` once the feature is complete and tested.
Git performs a 'fast-forward' merge when possible (simply moving the branch pointer forward), or creates a merge commit when both branches have diverged with separate new commits.
git switch main
git merge feature-branchPerforming a merge
Switch to the branch you want to merge into (usually main), then run `git merge branch-name` to bring in the other branch's changes.
Fast-forward vs merge commit
If main hasn't changed since the feature branch was created, Git does a fast-forward merge. If both branches have new commits, Git creates a merge commit joining both histories.
git switch main
git merge feature-loginUpdating a3f5c9e..9c2e1aa
Fast-forward
app.js | 10 +++++-----Merges feature-login into main using a fast-forward merge since main had no new commits.
git merge feature-paymentsMerge made by the 'ort' strategy.
payments.js | 20 ++++++++++++++++++++Creates a merge commit because both branches had diverged with separate commits.
Key points
- git merge combines changes from one branch into another.
- A fast-forward merge simply moves the branch pointer.
- A merge commit is created when branches have diverged.
- Always merge into the target branch after switching to it.
