Skip to main content

Git Worktree Magic: Do Commits Show Up in Other Worktrees? πŸͺ„

Β· 10 min read
Mahmut Salman
Software Developer

The Confusion πŸ˜•β€‹

Backend Dev: "I have two worktrees - one for frontend and one for backend. When I commit in the backend worktree, does it show up in the original worktree? And when I merge backend into main, what happens to the backend branch?"

Frontend Mentor: "Great question! This is where Git worktrees become really magical. Let me show you exactly what's happening in your setup."

Your Current Worktree Setup πŸ—‚οΈβ€‹

Frontend Mentor: "First, let's visualize what you have:"

πŸ“ EcommerceWebsite3/                    (Worktree 1 - Original)
└── Currently on: frontend branch

πŸ“ EcommerceWebsite3-backend/ (Worktree 2 - Backend)
└── Currently on: backend branch ← YOU ARE HERE

Shared Git Repository:
β€’ main branch
β€’ frontend branch
β€’ backend branch

Backend Dev: "So both worktrees are separate folders?"

Frontend Mentor: "Yes! But here's the magic - they share the same Git repository. This is crucial to understanding how worktrees work."

The Critical Insight πŸ’‘β€‹

Frontend Mentor: "Think of it like this:"

Traditional Git Setup:
πŸ—‚οΈ Folder β†’ πŸ—„οΈ Git Repository (1:1 relationship)

Git Worktrees:
πŸ—‚οΈ Worktree 1 ┐
πŸ—‚οΈ Worktree 2 β”œβ†’ πŸ—„οΈ Same Git Repository (Many:1 relationship)
πŸ—‚οΈ Worktree 3 β”˜

Backend Dev: "So they're separate working directories but the same Git database?"

Frontend Mentor: "EXACTLY! That's the key to understanding everything else!"

Question 1: Do Commits Show Up Immediately? πŸ€”β€‹

Backend Dev: "When I commit in the backend worktree, does it show up in the original worktree?"

Frontend Mentor: "YES! Immediately! Let me show you:"

The Scenario​

# In EcommerceWebsite3-backend/ (backend worktree)
git status
# On branch backend

git add .
git commit -m "Add image update script"
# [backend abc123] Add image update script

Immediately in the original worktree:

# Switch to EcommerceWebsite3/ (original worktree)
cd ../EcommerceWebsite3

git log backend
# commit abc123
# Author: You
# Date: Just now
#
# Add image update script

Backend Dev: "Wait, I didn't have to do git pull or git fetch?"

Frontend Mentor: "NOPE! Because they share the same repository! The commit is already there!"

Visual Timeline ⏰​

Let me show you exactly what happens:

TIME: 10:00 AM - Make Commit in Backend Worktree​

πŸ”΅ Backend Worktree (EcommerceWebsite3-backend/)
β”œβ”€β”€ On branch: backend
β”œβ”€β”€ Action: git commit -m "Add images"
└── Result: Commit created (hash: abc123)

Shared Git Repository:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ backend: A---B---C---[abc123] ← NEW!β”‚
β”‚ main: A---B---C β”‚
β”‚ frontend: A---B---D---E β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 Frontend Worktree (EcommerceWebsite3/)
β”œβ”€β”€ On branch: frontend
β”œβ”€β”€ Status: Working on different branch
└── Can see: git log backend shows abc123 βœ“

Backend Dev: "So the commit is in the repository, but my frontend worktree's working directory doesn't change because I'm on a different branch?"

Frontend Mentor: "PERFECT! You've got it! The commit exists in the repository, but your working directory only shows files from your current branch."

Question 2: What Happens When You Merge? πŸ”€β€‹

Backend Dev: "When I merge backend into main, what happens to the backend branch?"

Frontend Mentor: "Let's walk through it step by step:"

TIME: 10:15 AM - Merge Backend into Main​

# In EcommerceWebsite3-backend/ (backend worktree)
git checkout main
# Switched to branch 'main'

git merge backend
# Updating C...abc123
# Fast-forward
# images/product1.jpg | binary
# scripts/update.sh | 20 ++++++++++++++++++++

What Just Happened?​

Before Merge:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ main: A---B---C β”‚
β”‚ backend: A---B---C---abc123 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

After Merge:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ main: A---B---C---abc123 ← MOVED!β”‚
β”‚ backend: A---B---C---abc123 β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Backend Dev: "So main now has the commits, and backend branch still exists?"

Frontend Mentor: "YES! Merging doesn't delete the source branch. It just moves the target branch pointer forward."

Seeing Changes in Other Worktrees πŸ‘€β€‹

Frontend Mentor: "Now, here's the magic moment. Let's go to your frontend worktree:"

# Switch to EcommerceWebsite3/ (original worktree)
cd ../EcommerceWebsite3

# Currently on frontend branch
git status
# On branch frontend

# Switch to main to see the merged changes
git checkout main
# Switched to branch 'main'
# Your branch is up to date with 'origin/main'.

# Look at the files
ls images/
# product1.jpg ← IT'S HERE!

Backend Dev: "Wait, I didn't have to git pull? The changes are already there?"

Frontend Mentor: "EXACTLY! No git pull needed! When you git checkout main, Git just updates your working directory to show main's files. The commits are already in the shared repository!"

The Aha Moment πŸ’‘β€‹

Backend Dev: "So when I commit in one worktree, the commit goes into the shared repository, and I can switch to that branch in any worktree to see it?"

Frontend Mentor: "YES! Let me illustrate with both worktrees side by side:"

TIME: 10:20 AM - Complete View
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

πŸ”΅ Backend Worktree (EcommerceWebsite3-backend/)
β”œβ”€β”€ Current branch: main
β”œβ”€β”€ Working directory: Shows main's files (including merged changes)
└── Can switch to: frontend, backend

Shared Git Repository (THE BRAIN):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ main: A---B---C---abc123 β”‚
β”‚ backend: A---B---C---abc123 β”‚
β”‚ frontend: A---B---D---E β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

🟒 Frontend Worktree (EcommerceWebsite3/)
β”œβ”€β”€ Current branch: main
β”œβ”€β”€ Working directory: Shows main's files (including merged changes)
└── Can switch to: frontend, backend

━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Backend Dev: "So each worktree is like a different 'view' into the same repository?"

Frontend Mentor: "PERFECT analogy! Each worktree is a window showing a different branch, but they're all looking at the same Git database."

Frontend Mentor: "Here's the best way to work with your setup:"

# πŸ”΅ BACKEND WORKTREE (EcommerceWebsite3-backend/)
# Work on backend features here

# 1. Work on backend branch
git checkout backend
git add .
git commit -m "Add feature X"
git push origin backend # Share with team

# 2. When ready to merge to main
git checkout main
git pull origin main # Get latest from remote
git merge backend # Merge backend β†’ main
git push origin main # Share merged changes

# ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

# 🟒 FRONTEND WORKTREE (EcommerceWebsite3/)
# Work on frontend features here

cd ../EcommerceWebsite3

# 1. Work on frontend branch
git checkout frontend
git add .
git commit -m "Add UI feature"
git push origin frontend

# 2. When ready to merge to main
git checkout main
git pull origin main
git merge frontend
git push origin main

Option 2: Sync Main Between Worktrees​

Backend Dev: "After merging in backend worktree, how do I see the changes in frontend worktree?"

Frontend Mentor: "Just checkout main!"

# After merging in backend worktree...
cd ../EcommerceWebsite3-backend
git checkout main
git merge backend
# main now has backend's changes

# Switch to frontend worktree
cd ../EcommerceWebsite3
git checkout main
# ← Changes are ALREADY here! Just checkout main to see them

Key Rules to Remember πŸ“β€‹

Frontend Mentor: "Here are the golden rules:"

βœ… DO These Things​

  1. Commits are Instant

    # In any worktree
    git commit -m "Fix"
    # ← Visible in ALL worktrees immediately via git log
  2. Each Worktree Can Be on Different Branch

    # Backend worktree
    git checkout backend

    # Frontend worktree
    git checkout frontend
    # ← This is the whole point of worktrees!
  3. Push/Pull from Any Worktree

    # In any worktree
    git push origin main
    # ← Affects the shared repository
  4. Merge from Any Worktree

    # In any worktree
    git merge backend
    # ← Updates are instant everywhere

❌ DON'T Do These Things​

  1. Don't Checkout Same Branch in Both Worktrees

    # Backend worktree
    git checkout main

    # Frontend worktree
    git checkout main
    # ❌ Git prevents this!
    # fatal: 'main' is already checked out at '.../backend'
  2. Don't Forget to Push

    git commit -m "Important fix"
    # βœ“ Visible in your worktrees
    # ❌ NOT visible to team until you push!
    git push origin main # ← Don't forget!
  3. Don't Assume Files Exist

    # Frontend worktree on 'frontend' branch
    cat backend-only-file.txt
    # ❌ File doesn't exist in this branch!

Real-World Workflow Example πŸŒβ€‹

Frontend Mentor: "Let me show you a complete day of work:"

Morning: Backend Work β˜•β€‹

# 9:00 AM - Backend Worktree
cd ~/EcommerceWebsite3-backend

git checkout backend
# Work on API endpoints
git add src/api/
git commit -m "Add product API"
git push origin backend

# Result: backend branch updated in repository
# Visible in all worktrees via: git log backend

Afternoon: Frontend Work πŸŽ¨β€‹

# 2:00 PM - Frontend Worktree
cd ~/EcommerceWebsite3

git checkout frontend
# Work on UI components
git add src/components/
git commit -m "Add product cards"
git push origin frontend

# Result: frontend branch updated in repository
# Visible in all worktrees via: git log frontend

Evening: Merge Both to Main πŸš€β€‹

# 5:00 PM - Backend Worktree
cd ~/EcommerceWebsite3-backend

git checkout main
git pull origin main # Get latest
git merge backend # Merge backend work
git push origin main

# 5:05 PM - Frontend Worktree
cd ~/EcommerceWebsite3

git checkout main
# ← Backend changes already here! (just git pull if remote updated)
git pull origin main # Get latest (including backend merge)
git merge frontend # Merge frontend work
git push origin main

# Result: main has BOTH backend and frontend changes!

Backend Dev: "So I can work independently in each worktree and merge them together at the end?"

Frontend Mentor: "Exactly! That's the power of worktrees!"

Common Misconceptions βŒβ€‹

Misconception 1: "I Need to Pull in Other Worktrees"​

# ❌ WRONG THINKING:
# In backend worktree
git commit -m "Add feature"

# In frontend worktree
git pull # ← NOT NEEDED!

Frontend Mentor: "The commit is already in the shared repository! You only need to checkout the branch to see it."

Misconception 2: "Merging Deletes the Source Branch"​

# ❌ WRONG THINKING:
git merge backend
# Backend branch is deleted? NO!

Frontend Mentor: "Merging just moves the target branch pointer. The source branch stays intact."

Misconception 3: "Files Appear in All Worktrees"​

# ❌ WRONG THINKING:
# In backend worktree (on backend branch)
touch new-file.txt
git add new-file.txt
git commit -m "Add file"

# In frontend worktree (on frontend branch)
ls new-file.txt
# ← File NOT here until you checkout backend!

Frontend Mentor: "Files only appear when you checkout the branch that has them!"

The Mental Model πŸ§ β€‹

Frontend Mentor: "Here's the mental model you should have:"

Git Repository (The Brain):
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ All commits, branches, history β”‚
β”‚ Shared by all worktrees β”‚
β”‚ Updates are instant across worktrees β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
↑ ↑ ↑
β”‚ β”‚ β”‚
Worktree 1 Worktree 2 Worktree 3
(Window 1) (Window 2) (Window 3)
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Branch: β”‚ β”‚ Branch: β”‚ β”‚ Branch: β”‚
β”‚ frontendβ”‚ β”‚ backend β”‚ β”‚ feature β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Backend Dev: "So worktrees are just different 'windows' into the same repository?"

Frontend Mentor: "YES! Perfect! Each window shows a different branch, but the repository is shared!"

Advanced Scenario: Merge Conflicts πŸ’₯​

Backend Dev: "What if there's a merge conflict?"

Frontend Mentor: "Great question! Let's see what happens:"

# Backend worktree
git checkout main
git merge backend
# Auto-merging src/config.ts
# CONFLICT (content): Merge conflict in src/config.ts

What you see in frontend worktree:

# Frontend worktree
cd ../EcommerceWebsite3
git status
# nothing to commit, working tree clean

# But if you checkout main:
git checkout main
# error: you need to resolve your current index first

Frontend Mentor: "Git prevents you from checking out the conflicted branch in other worktrees until you resolve it!"

Backend Dev: "So I have to fix it in the backend worktree first?"

Frontend Mentor: "Exactly! Resolve the conflict where it happened, then you can access the branch from other worktrees."

Debugging Tips πŸ”β€‹

Frontend Mentor: "Here are some useful commands for debugging worktree issues:"

Check All Worktrees​

git worktree list
# /Users/you/EcommerceWebsite3 abc123 [frontend]
# /Users/you/EcommerceWebsite3-backend def456 [backend]

See All Branches and Their Commits​

git log --all --oneline --graph
# * def456 (backend) Add image update
# * abc123 (main) Merge backend
# * | 123456 (frontend) Add UI
# |/
# * 789abc Initial commit

Check Branch Status Across Worktrees​

git branch -vv
# * backend def456 [origin/backend] Add image update
# frontend 123456 [origin/frontend] Add UI
# main abc123 [origin/main] Merge backend

Key Takeaways πŸŽ―β€‹

  1. Shared Repository

    • All worktrees share the same Git repository
    • Commits are instantly visible across worktrees
  2. Independent Working Directories

    • Each worktree has its own files
    • Files change when you checkout different branches
  3. No Pulling Between Worktrees

    • Commits are already in the shared repository
    • Just checkout the branch to see changes
  4. Merging Updates Repository

    • Merge in any worktree updates the shared repository
    • Other worktrees see the merge immediately
  5. Branch Checkout Changes View

    • git checkout updates your working directory
    • Shows files from that branch

The Final Aha Moment πŸ’‘β€‹

Backend Dev: "So the workflow is:

  1. Commit in any worktree β†’ updates shared repository
  2. Merge in any worktree β†’ updates shared repository
  3. Checkout branch in any worktree β†’ updates that worktree's files"

Frontend Mentor: "PERFECT! You've mastered Git worktrees! πŸŽ‰"

Backend Dev: "This is amazing! I can work on backend and frontend simultaneously without constantly switching branches!"

Frontend Mentor: "That's exactly why worktrees exist! Welcome to the next level of Git productivity! πŸš€"


Have you been confused by Git worktrees? Share your experience in the comments below! πŸ’­