🔧 Programmierung 🕛 vor 2 Monaten 5 Min Lesezeit
0

A beginner's guide to Git worktrees: What they are, why they matter, and how to use them without breaking anything

↗ Quelle (dev.to)
🗣️ Stimme:
📑 Inhaltsübersicht

Ahnii!



If you have ever needed to work on two branches at the same time, Git worktrees can save you a lot of friction. This post covers what worktrees are, why they exist, and how you can use them safely as a beginner.






Prerequisites




  • You have Git installed

  • You can run commands in your terminal

  • You already have a local repository






What Is a Git Worktree?



A Git worktree is an extra working folder connected to the same repository history.


You can think of it as another checkout of your project, without making another full clone.



Your main folder still exists. A worktree gives you a second folder where a different branch can be checked out at the same time.





Why Worktrees Exist



Git worktrees solve a practical problem. You may be in the middle of feature work, then need to fix a bug on another branch right away.



Without worktrees, you usually do one of these:




  • Stash or commit unfinished work, then switch branches

  • Open a second full clone of the same repository



Both options work, but both add overhead. Worktrees give you a cleaner path.





Normal Clone vs Branch Checkout vs Worktree



Here is the simple difference:





Normal clone



A clone is a separate copy of a repository with its own .git directory.




CODE
git clone https://github.com/example/project.git






You use this when you need the repository on your machine for the first time. It is fully independent from other clones.






Branch checkout



A branch checkout changes which branch is active in your current folder.




CODE
git checkout feature/foo






This is fast, but only one branch can be active in that folder at a time.






Worktree



A worktree creates another folder tied to the same repository, usually on a different branch.




CODE
git worktree add ../feature-foo feature/foo






Now you have two folders open at once: your main folder and ../feature-foo. Each can point at a different branch.






Core Benefits of Worktrees






1) Multiple branches checked out at once



You can keep your main branch open in one folder and your feature branch in another.


No constant branch switching.





2) Isolated environments for experiments



You can test risky changes in one worktree without touching the working state in another folder.





3) No need for multiple full clones



Worktrees share repository data, so you avoid duplicate clones for everyday branch work.





A Safe Beginner Workflow



This is a clean workflow you can use right away.





1) Create a worktree





CODE
git worktree add ../feature-foo feature/foo





This command creates a new folder named ../feature-foo and checks out feature/foo there.


If feature/foo does not exist yet, create it first with git branch feature/foo.





2) Switch into it





CODE
cd ../feature-foo





Now every Git command runs inside that worktree folder.


Before you edit files, confirm where you are with pwd and git branch --show-current.





3) Commit from it





CODE
git add .
git commit -m "Add first pass of feature foo"





These commits belong to the branch checked out in that worktree.


You do not need to return to your original folder to commit.





4) Remove it safely



First leave the worktree folder, then remove it with Git.




CODE
cd ../your-main-repo
git worktree remove ../feature-foo






This tells Git to unregister the worktree and remove the directory safely.


Always prefer this over deleting the folder manually.





How to See Your Current Worktrees





CODE
git worktree list





This shows every registered worktree path and branch.


Use this often, especially when you are learning.





Common Beginner Mistakes





Deleting the directory without removing the worktree



If you run rm -rf on a worktree folder first, Git can keep stale metadata.


Remove worktrees with git worktree remove <path> whenever possible.





Forgetting which worktree you are in



It is easy to commit to the wrong branch when two folders look similar.


Check pwd and git branch --show-current before making changes.





Trying to check out the same branch twice



Git does not allow the same branch to be active in two worktrees at once.


Create a new branch if you need a second experimental space.





A Simple Mental Model for .git/worktrees



Your main repository still has the real Git database.


Inside it, .git/worktrees stores small records that point to each extra working folder.



Think of it like a clipboard that says:




  • Which extra folders exist

  • Which branch each one uses

  • Whether Git still expects them to be present



That is why manual deletion can confuse Git. The clipboard still has an entry, even if the folder is gone.





How to Clean Up Orphaned Worktrees



Sometimes a worktree folder gets deleted outside Git.


You can clean this up safely in a few steps.





Step 1: List what Git thinks exists





CODE
git worktree list





Look for paths that no longer exist on disk.


Those are likely orphans.





Step 2: Prune stale metadata





CODE
git worktree prune





This removes stale worktree entries that no longer point to valid folders.


It is a safe maintenance command for this situation.





Step 3: Verify cleanup





CODE
git worktree list





Run the list command again to confirm orphan entries are gone.


If everything looks clean, you are done.





Verify It Works



Run this quick check whenever you start using worktrees:




CODE
git worktree list
git branch --show-current
pwd






These three commands tell you what worktrees exist, which branch is active, and which folder you are in.


That simple habit prevents most beginner mistakes.



Baamaapii

Vollständiger Original-Artikel
Den kompletten Beitrag mit allen Details direkt auf dev.to lesen.
↗ Original-Artikel auf dev.to lesen
Wie bewertest du diesen Beitrag?
1 Klick Feedback
Teilen mit Netzwerk & Team:

Community-Analysen & Experten-Meinungen 0

Verfasse deine eigene Analyse, teile Workarounds oder diskutiere diesen Vorfall im Blog.
Noch keine Community-Analyse verfasst. Markiere einen Textabschnitt oder klicke oben auf Eigene Analyse verfassen“!
Community Pulse: Relevanz-Einschätzung
1 Klick Experten-Votum
🔴 Akute Relevanz 0%
🟡 In Evaluierung 0%
🟢 Keine Auswirkung 0%
Spannende Innovation 0%
Verwandte Story-Cluster & Quellen (Vektor-KI)
Port 8095 Engine
6 Quellen
CVE-2022-44255 | TOTOLINK LR350 9.3.5u.6369_B20220309 buffer overflow (EUVD-2022-47204)
2 Quellen
CVE-2026-68426 | Linux Kernel up to 6.18.41/7.1.5/7.2-rc3 xfrm validate_xmit_skb_list use after free (Nessus ID 346426)
1 Quelle
Windows 11 Probleme mit gültiger Domänenanmeldung nach September-Update [Workaround]
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten A beginner's guide to Git worktrees: What they are, why they matter, and how to use them without breaking anything

Thematisch verwandte Begriffe: beginners, guide, worktrees, What · 6 Treffer

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...