DEV Community

Cover image for 🚀 Introduction to Git Worktree (With Oh My Zsh Aliases)
Prakhar Yadav
Prakhar Yadav

Posted on

🚀 Introduction to Git Worktree (With Oh My Zsh Aliases)

If you’ve ever had to switch branches in the middle of work, stash your changes, or juggle multiple clones of the same repository… you already know the pain.

Good news: Git Worktree makes all of this much easier.

This guide will introduce:
• What Git Worktree is
• What Oh My Zsh is (and why its aliases help)
• Real workflows for single-repo apps
• Real workflows for monorepos

Let’s make your Git life smoother and your workflow faster. ✨


🌱 What is Git Worktree?

A Git worktree is a feature that lets you check out multiple branches of the same repo at the same time — each in its own folder.

Think of it like having multiple “workspaces” linked to the same Git repository.

Why use it?
• No need to clone the repo again
• No need to stash or reset your local changes
• You can develop, test, or review PRs in parallel
• Saves time and disk space
• Cleaner workflow

Example:
You’re working on feature/homepage-redesign but your teammate asks you to check hotfix/login-bug.

Instead of stashing everything:

git worktree add ../login-hotfix origin/hotfix/login-bug
Enter fullscreen mode Exit fullscreen mode

Boom — a new folder opens up with that branch, while your original branch stays untouched.


🌀 What is Oh My Zsh? And Why Are We Talking About It?

If you use Zsh (default shell on macOS), then Oh My Zsh is like a power-up.

It’s a framework that adds:
• Useful plugins
• Completion
• Themes
• Shortcuts
• And… Git aliases that save a LOT of typing

Instead of typing:

git worktree add ../some-folder feature-branch
Enter fullscreen mode Exit fullscreen mode

With the Oh My Zsh git plugin, you can type:

gwta ../some-folder feature-branch
Enter fullscreen mode Exit fullscreen mode

Here are the key aliases for Git Worktree:

Alias Full command Meaning
gwt git worktree Base command
gwta git worktree add Add a worktree
gwtl git worktree list List worktrees
gwtr git worktree remove Remove worktree
gwtp git worktree prune Remove stale entries

To enable it, make sure your .zshrc has:

plugins=(git)
Enter fullscreen mode Exit fullscreen mode

Reload:

source ~/.zshrc
Enter fullscreen mode Exit fullscreen mode

And you’re set.


⚙️ Workflow Scenario 1 — Single-Repo App

Let’s say you have a normal project:

my-app/
  src/
  package.json
  ...
Enter fullscreen mode Exit fullscreen mode

You’re working on a big feature:

feature/new-ui
Enter fullscreen mode Exit fullscreen mode

But someone needs your help reviewing:

feature/navbar-fix
Enter fullscreen mode Exit fullscreen mode

Instead of stopping your work:

➕ Add a worktree

gwta ../navbar-fix-wt origin/feature/navbar-fix
Enter fullscreen mode Exit fullscreen mode

Now you have:

my-app/                  ← your main work continues here
navbar-fix-wt/           ← completely separate workspace
Enter fullscreen mode Exit fullscreen mode

👇 Inside navbar-fix-wt/
• Open code
• Run tests
• Try builds
• Review changes

When done:

gwtr ../navbar-fix-wt
Enter fullscreen mode Exit fullscreen mode

Back to your original branch, untouched.

🔥 Why this is awesome
• No stashing
• No losing uncommitted changes
• No slow clones
• You can have 3–4 worktrees open if you want


🏗️ Workflow Scenario 2 — A Monorepo with Multiple Apps

A common setup:

company-monorepo/
  apps/
    web/
    admin/
    mobile/
  libs/
    ui/
    utils/
Enter fullscreen mode Exit fullscreen mode

Now imagine you need to:
• Work on web-app-feature
• Test something in admin
• Also prepare a PR fix for mobile

Instead of checking out branches back and forth:

1️⃣ Create worktrees for each branch

gwta ../wt-web-feature web-app-feature
gwta ../wt-admin-experiments admin-dashboard-refactor
gwta ../wt-mobile-fix mobile/login-crash
Enter fullscreen mode Exit fullscreen mode

Folder layout becomes:

company-monorepo/           ← your main repo
wt-web-feature/
wt-admin-experiments/
wt-mobile-fix/
Enter fullscreen mode Exit fullscreen mode

Each folder is:
• A checkout of a different branch
• Still shares the same .git data
• Fast to create (no re-cloning!)

2️⃣ Work in each independently

cd ../wt-web-feature
npm run dev:web

cd ../wt-admin-experiments
npm run dev:admin

cd ../wt-mobile-fix
npm run dev:mobile
Enter fullscreen mode Exit fullscreen mode

Yes, you can run multiple apps side-by-side — perfect for monorepos.

3️⃣ Clean up when you’re done

gwtr ../wt-web-feature
gwtr ../wt-admin-experiments
gwtr ../wt-mobile-fix
Enter fullscreen mode Exit fullscreen mode

🎯 Final Thoughts

Git Worktree is one of those features that feels small but saves huge amounts of time once you start using it.

Combined with Oh My Zsh’s aliases:
• Faster workflows
• Cleaner development environments
• No more “stash → switch → test → switch back” loop
• Perfect for multitasking across branches
• Perfect for monorepos where multiple apps run at once

If you work on anything slightly complex - frontend frameworks, backend services, monorepos (& ServiceNow apps in my case) - Git Worktree is a superpower 💪

Top comments (0)