Merge Branches
After you finish working on a branch, you usually want to bring those changes back into the main branch (or another branch). This process is called merging.
What is Merging?
Merging takes the changes from one branch and integrates them into another. Git does this by creating a new commit (called a merge commit) that combines the histories of both branches.
Fast‑Forward Merge
If the target branch (e.g.,
main) has no new commits since you branched off, Git can simply move the pointer forward. This is a fast‑forward merge.Example:
git switch main
git merge feature-loginIf main has not diverged, Git fast‑forwards.Three‑Way Merge
If both branches have new commits, Git performs a three‑way merge and creates a merge commit. This commit has two parents – one from each branch.
How to Merge
1. Switch to the branch you want to merge into (usually
main).2. Run
git merge .For example:
git switch main
git merge feature-loginViewing Merged Branches
After merging, you can delete the feature branch if it’s no longer needed:
git branch -d feature-loginThe -d option deletes the branch only if it has been merged. Use -D to force delete.Two Minute Drill
- Merging integrates changes from one branch into another.
- Switch to the target branch, then run
git merge source-branch. - Fast‑forward merge occurs when the target hasn’t moved.
- Three‑way merge creates a merge commit when both branches have new changes.
- Delete a merged branch with
git branch -d branch-name.
Need more clarification?
Drop us an email at career@quipoinfotech.com
