What Is a Branch?
A branch is a movable pointer to a commit. Creating a branch is instant and cheap, so you can try an idea or build a feature without touching main.
Branch Commands
feature/login-page, fix/null-pointer, chore/update-deps, release/1.4.
Merging
Merging brings the work of one branch into another. Switch to the receiving branch first.
Fast-forward merge
If main has no new commits since you branched, Git simply moves the main pointer forward. No merge commit is created.
Three-way merge
If both branches have new commits, Git creates a merge commit with two parents.
Merge Conflicts
A conflict happens when both branches changed the same lines. Git cannot choose, so it asks you.
Open the file. Git marks the conflict:
The part between <<<<<<< and ======= is your current branch. The part below is the incoming branch. Edit the file to the final correct content and delete the marker lines.
Rebase
Rebase replays your commits on top of another branch, giving a straight, linear history without merge commits.
| Merge | Rebase | |
|---|---|---|
| History | Keeps true history with merge commits | Linear, cleaner history |
| Commit ids | Unchanged | Rewritten (new ids) |
| Conflicts | Resolved once | May be resolved per commit |
| Safe on shared branches | Yes | No |
| Best for | Merging into main, preserving context | Updating your own branch before a pull request |
git push --force-with-lease only on your own branches.
Interactive Rebase: Clean Up Before a Pull Request
Cherry-Pick
Copy one specific commit from another branch, for example to apply a hotfix to a release branch.
Typical Feature Flow
git switch -c feature/xfrom an updatedmain- Commit small changes while you work
git fetchandgit rebase origin/mainto catch upgit push -u origin feature/x- Open a pull request, get review and passing CI
- Merge (squash, merge commit or rebase, per team rule)
- Delete the branch
Practice Tasks
- Create two branches that change the same line and resolve the conflict
- Do the same merge once with
--no-ffand once as a fast-forward and comparegit log --graph - Make five messy commits, then squash them into one with
git rebase -i - Cherry-pick a bug fix from
mainto arelease/1.0branch - Abort a conflicted rebase and a conflicted merge safely
Conclusion
Branches let you work in parallel. Merge keeps history exactly as it happened, rebase makes it linear, and conflicts are just two edits you need to combine. Remember never to rebase shared commits.

