重命名存储库文件夹导致 "git status failed with code 128 this operation must be run in a work tree"

Renaming repository folder causes "git status failed with code 128 this operation must be run in a work tree"

我已经通过文件系统(Windows 文件资源管理器)重命名了本地 Git 存储库文件夹,现在 Git 状态(通过 SourceTree 检查存储库时)每次都失败

git status failed with code 128 this operation must be run in a work tree

基本上我只更改了外部文件夹名称,并没有触及内部存储库结构。

OldRepoName    
│   .gitattributes
│   .gitignore
│   README.md
│   │   ...
│   
└───.git
│   │   config
│   │   ...
│   
└─── ...


NewRepoName    
│   .gitattributes
│   .gitignore
│   README.md
│   │   ...
│   
└───.git
│   │   config
│   │   ...
│   
└─── ...

我能找到的关于“此操作必须在工作树中 运行”错误的所有搜索都在处理从未有工作树结构的裸存储库。此存储库不是裸存储库。

如何在不从远程再次克隆的情况下修复存​​储库?


注意:作为 Mercurial 用户,我很习惯重命名存储库文件夹,没有任何负面影响。

Git 存储库包含使用工作树绝对路径的配置文件。修复该路径以指向新的、重命名的文件夹修复存储库。

  1. 在文本编辑器中打开 .git/config 文件
  2. [core] 配置下找到 worktree 条目
  3. 将绝对回购路径更新为新路径并保存更改

来自(这是 Windows 示例,但同样的原则适用于其他 OS)

worktree = C:/OldPath/OldRepoName

worktree = C:/NewPath/NewRepoName