Video

Work on Multiple Git Branches Simultaneously | Git Worktree Explained

Work on Multiple Git Branches Simultaneously | Git Worktree Explained

Ever needed to work on two Git branches at the same time without constantly switching between them? Git Worktree makes it possible.

A normal Git workflow gives you one working directory and one checked-out branch. So when production needs an urgent fix while you're mid-feature, you stop, save your changes, switch branches, fix the bug, and come back. That's unnecessary context switching.

Git worktree lets you check out multiple branches into separate working directories, all connected to the same Git repository. In this video we use a practical Ruby on Rails example: building a payment feature in one directory while fixing a production bug in another.

What you'll learn:
- What Git Worktree is and why it exists
- Why one working directory becomes a bottleneck
- How to create and list worktrees
- How to work on separate branches at the same time
- The benefits, and the trade-offs worth knowing

The commands:

# which branch is this directory on?
git branch --show-current

# add a second working directory, checked out on main
git worktree add ../my-rails-app-hotfix main

# see every worktree and the branch it uses
git worktree list

# clean it up when you're done
git worktree remove ../my-rails-app-hotfix

Three things worth remembering:

1. Worktrees share Git history, not project environments. The objects and commits are shared; dependencies, environment variables, local databases, and running services may need separate setup in each directory.
2. Git normally prevents checking out the same branch in two worktrees at once.
3. Removing a worktree does not delete its branch — and `git worktree remove` may refuse if the directory has uncommitted changes.

Chapters:

00:00 — Work on Multiple Branches at Once
00:10 — The Problem with Switching Branches
00:25 — What Is Git Worktree?
00:40 — How Git Worktree Works
01:00 — Practical Rails Example
01:25 — Benefits and Trade-offs
01:45 — Key Takeaway

One repository. Multiple worktrees. Work without switching.

#git #gitworktree #github #versioncontrol #developer #programming #softwareengineering #devops #rubyonrails #coding #webdevelopment #gitcommands #codingtips #softwaredevelopment #backend

— #Astmation —
#software #systems, #automation, and #AI-powered #applications.
Build. Automate. Evolve.
https://astmation.com/


Watch on YouTube

Back to videos