Skip to main content

Command Palette

Search for a command to run...

πŸš€Day-02: Branching & Merging

Git-Zero-To-Hero-Series

Updated
β€’5 min read
πŸš€Day-02: Branching & Merging

πŸ• 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 main hasn’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 --abort
    
  • Reset 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

ConceptDescriptionCommand(s)
BranchPointer to commitgit branch, git switch
Fast-forward mergeMoves pointer onlygit merge
3-way mergeCreates new merge commitgit merge
ConflictOverlapping change in same linemanual resolve
Delete branchClean workspacegit branch -d
Visualize graphUnderstand DAGgit 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

More from this blog

thiru's blog

43 posts