What is a Branch?
A branch is NOT a copy of the files. It is simply a movable pointer to a commit.
The "HEAD" Pointer
Git uses a special pointer called HEAD to know "You Are Here".
- HEAD→main→Commit A
- // You are on 'main', looking at Commit A
Visualizing Pointers
Types of Merges
1. Fast-Forward
Git simply moves the pointer forward. No new commit is created.
Happens when
main hasn't changed while you were working on feature.2. Recursive (Merge Commit)
Git creates a new "Merge Commit" to tie the two histories together.
Happens when
main HAS changed (e.g., someone else pushed code).Essential Commands
Create & Switch
Create a new branch and switch to it immediately.
git checkout -b feature-nameMerge
Combine changes from another branch into your current one.
git merge feature-nameDelete
Remove a branch after it has been merged.
git branch -d feature-nameBranching Strategy
✅ Do This
- • Name branches descriptively (e.g.,
fix/login-bug). - • Keep branches short-lived (merge often).
- • Delete branches after merging.
❌ Avoid This
- • Committing broken code to
main. - • Working on one "mega-branch" for months.
- • Naming branches "test", "temp", or "changes".