Branching and Merging
Harry
· 14 Sep 2026
· 3 views
Advertisement
Why branches
A branch is a movable pointer to a line of development. Because Git branches are just lightweight references to commits, creating one is instant. You keep main stable and do each new feature or fix on its own branch, then merge it back when it is ready.
Create and switch
git switch -c feature/login # create and switch in one step
# ...edit, add, commit on the branch...
git switch main # go back to main
(git checkout -b feature/login is the older equivalent of switch -c.)
Merging back
When the feature is done, merge it into main:
git switch main
git merge feature/login
- If
mainhas not moved, Git does a fast-forward – it simply advances the pointer. - If both branches have new commits, Git creates a merge commit that ties the two histories together (a three-way merge).
Resolving conflicts
A conflict happens when the same lines changed on both branches. Git pauses the merge and marks the file:
<<<<<<< HEAD
color: blue;
=======
color: green;
>>>>>>> feature/login
Edit the file to the version you want, delete the marker lines, then finish the merge:
git add styles.css
git commit # completes the merge
Key points
- Branches are cheap pointers – keep
mainstable and build features on their own branch. git switch -c namecreates and switches;git merge namebrings work back.- Merges fast-forward when possible, otherwise create a merge commit.
- Conflicts are edited by hand, then staged and committed to finish the merge.