Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
Git

Git: Fix a Worktree Locking a Branch Needed Somewhere Else

- 19.11.26 - ErcanOPAK

🔀 Fix a Worktree Locking a Branch Needed Somewhere Else

`git worktree add` checks out a branch into a second working directory while registering that checkout in the main repository’s internal worktree administration, and Git enforces a rule that the SAME branch can’t be checked out in two worktrees at once. If you later try to check that branch out back in your main working directory, Git refuses with “already checked out” — even if the OTHER worktree’s folder was deleted by hand with a plain file-delete instead of through Git itself, because deleting the folder directly never told Git’s administration that the checkout ended.

🔎 The Problem

$ rm -rf ../hotfix     # deleted the worktree's folder directly
$ git checkout hotfix-branch
fatal: 'hotfix-branch' is already checked out at '../hotfix'

$ git worktree list
/repo            abc1234 [main]
/repo/../hotfix  def5678 [hotfix-branch]   <- folder is gone, Git doesn't know yet

✅ Fix: Prune or Remove – Never Just Delete the Folder

  • Run `git worktree list` first to see every worktree Git still believes exists, including ones whose directory was deleted by hand — it will show as present but pointing at a path that no longer exists.
  • For a worktree whose folder is confirmed gone, run `git worktree prune` to have Git clean up its own administrative records, rather than trying to force the branch lock open from the other side.
  • If the worktree’s directory still exists and is simply done being used, remove it properly with `git worktree remove <path>` — which also releases the branch lock — instead of deleting the folder manually.

🚨 Why This Is Dangerous

  • Reaching for a forced branch delete or a forced checkout to work around the “already checked out” error, without first finding and properly removing the other worktree, can delete or detach history that worktree still had uncommitted or unpushed changes sitting on.

Deleting a worktree’s folder tells your filesystem it’s gone – it doesn’t tell Git.

— Version Control Tip

Related posts:

The Clean Way to Undo a Pushed Commit (Without Breaking History)

The Clean Way to Undo a Pushed Commit (Without Breaking History)

Git Hooks Prevent Bad Commits Early

Git Hooks Prevent Bad Commits Early

Git: Using Git Hooks for Automation

Git: Using Git Hooks for Automation

Views: 2

Post navigation

Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

November 2026
M T W T F S S
 1
2345678
9101112131415
16171819202122
23242526272829
30  
« Oct    

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (658)
  • Add Constraint to SQL Table to ensure email contains @ (613)
  • Average of all values in a column that are not zero in SQL (571)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (549)
  • Find numbers with more than two decimal places in SQL (477)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK
  • Git: Fix a Worktree Locking a Branch Needed Somewhere Else
  • Asp.Net Core: Fix a Background Service Leaking Scoped Dependencies
  • SQL Server: Fix a Columnstore Index Fragmenting After Bulk Deletes
  • Docker: Fix a Container Losing Logs After a Restart Policy
  • Asp.Net Core: Fix Output Caching Serving a Stale Authorized Page

Most Viewed Posts

  • How to make theater mode the default for Youtube (1,059)
  • Get the User Name and Domain Name from an Email Address in SQL (983)
  • How to add default value for Entity Framework migrations for DateTime and Bool (981)
  • Get the First and Last Word from a String or Sentence in SQL (861)
  • How to select distinct rows in a datatable in C# (856)

Recent Posts

  • Visual Studio: Fix Quick Actions Hiding Behind a Stale Squiggle
  • Photoshop: Fix a Smart Filter Resetting After You Relink It
  • Ajax: Fix a Cancelled Request Still Updating Stale UI State
  • C#: Fix a Span Captured by a Lambda Throwing at Runtime
  • The AI Prompt That Converts a REST Endpoint Into a Typed SDK

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com