πDay-02: Branching & Merging
Git-Zero-To-Hero-Series

π Hour 1 β Branching Concepts & Practice
1οΈβ£ What is a Branch?
A branch is just a pointer (ref) to a specific commit.
Example:
(main) ββAββBββC
If you create a new branch:
(main) ββAββBββC
\
(feature)
Both branches point to the same commit initially.
When you make new commits on feature:
(main) ββAββBββC
\
DββE (feature)
Now feature has diverged.
2οΈβ£ Create and List Branches
git branch # list branches
git branch feature1 # create branch
git switch feature1 # switch to branch (preferred modern)
# or: git checkout feature1
3οΈβ£ Create Branch + Switch (shortcut)
git switch -c feature2
4οΈβ£ Verify Current Branch
git status
git branch --show-current
The current branch will appear with an asterisk * when you run git branch.
5οΈβ£ Make Commits in New Branch
echo "Feature line 1" > feature.txt
git add feature.txt
git commit -m "Add feature.txt in feature1"
git log --oneline --graph --decorate --all
Youβll see diverging histories.
6οΈβ£ Go Back to Main Branch
git switch main
ls # notice feature.txt might not be here (depends on branch contents)
Branches have their own working directories relative to tracked files β switching branches updates the working tree.
π Hour 2 β Merging & Conflict Resolution
7οΈβ£ Merge Basics
Merging brings the changes from one branch into another.
Start on the branch that should receive the changes:
git switch main
git merge feature1
Two types of merges can happen:
Fast-forward merge β if
mainhasnβt diverged:- Git just moves the branch pointer forward.
3-way merge β if both branches have unique commits:
- Git creates a new merge commit combining changes.
8οΈβ£ Visualize Merge
git log --oneline --graph --decorate --all
Youβll see:
* 6d21e3c (HEAD -> main) Merge branch 'feature1'
|\
| * 49af23a (feature1) Add feature.txt
* | e7a62e9 Update README
|/
* 0a12b11 Initial commit
9οΈβ£ Merge Conflicts (How to Resolve)
Scenario:
You edited the same line in the same file on both branches.
# on main
echo "This is main line" > conflict.txt
git add conflict.txt
git commit -m "Add conflict.txt from main"
# create branch and modify same file
git switch -c feature2
echo "This is feature2 line" > conflict.txt
git commit -am "Edit conflict.txt in feature2"
# merge back into main
git switch main
echo "This is main line updated" > conflict.txt
git commit -am "Update conflict.txt in main"
git merge feature2
Youβll get:
Auto-merging conflict.txt
CONFLICT (content): Merge conflict in conflict.txt
Automatic merge failed; fix conflicts and then commit the result.
Open conflict.txt:
<<<<<<< HEAD
This is main line updated
=======
This is feature2 line
>>>>>>> feature2
You decide what to keep β edit file to desired content, e.g.:
This is resolved content
Then:
git add conflict.txt
git commit -m "Resolved merge conflict between main and feature2"
β Conflict resolved!
π Aborting a Merge
If you realize the merge is too messy:
git merge --abort
This returns the branch to the pre-merge state.
πΉ Delete a Branch
git branch -d feature1 # delete merged branch safely
git branch -D feature1 # force delete unmerged branch
πΉ View Branch Graph Clearly
git log --oneline --graph --decorate --all
OR install a visual tool:
sudo apt install gitk
gitk --all
π‘ Deep-Dive Explanation (Internals)
Each branch is just a file under
.git/refs/heads/that stores a commit hash.cat .git/refs/heads/mainβ Shows commit hash.
When you commit on a branch:
Git creates a new commit object.
The branch ref file updates to the new commit hash.
Merging:
Git performs a recursive 3-way merge (by default).
Creates a merge commit with two parent hashes.
βοΈ Undoing / Resetting
Undo a merge before committing:
git merge --abortReset to previous commit:
git reset --hard HEAD~1
π§ͺ Hands-on Exercises (~45 min total)
1οΈβ£ Branch playground
mkdir day2 && cd day2
git init
echo "main content" > app.txt
git add . && git commit -m "Initial commit"
git switch -c login
echo "login feature" > login.txt
git add . && git commit -m "Add login feature"
git switch main
git merge login
β Confirm merge success, check graph.
2οΈβ£ Conflict resolution practice
Follow the conflict example above until you can fix conflicts confidently.
3οΈβ£ Delete merged branches
Clean up your repo after merges.
4οΈβ£ View commit DAG
Run:
git log --oneline --graph --decorate --all
Try reading the graph visually.
π§ Day 2 Concepts Checklist
| Concept | Description | Command(s) |
| Branch | Pointer to commit | git branch, git switch |
| Fast-forward merge | Moves pointer only | git merge |
| 3-way merge | Creates new merge commit | git merge |
| Conflict | Overlapping change in same line | manual resolve |
| Delete branch | Clean workspace | git branch -d |
| Visualize graph | Understand DAG | git log --oneline --graph --decorate --all |
β End of Day 2 Outcome
You can now:
Create and work on multiple branches
Merge them safely
Resolve conflicts manually
Visualize and interpret your projectβs Git graph


