gitatlassian-sourcetreegit-worktree

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


I have renamed my local Git repository folder through file system (Windows File Explorer) and now Git status (when inspecting repository through SourceTree) fails every time with

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

Basically I only changed outside folder name, and I haven't touched inner repository structure.

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


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

All searches about "this operation must be run in a work tree" error I could find are dealing with bare repositories that never had work tree structure. This repository was not bare repository.

How do I fix the repo, without cloning it again from the remote?


Note: As a Mercurial user, I am quite used to renaming the repository folders, without any negative consequences.


Solution

  • Git repository contains configuration file which uses absolute path to the work tree. Fixing that path to point to new, renamed, folder fixes the repository.

    1. Open .git/config file in text editor
    2. locate worktree entry under [core] configuration
    3. update absolute repo path to new one and save changes

    from (this is Windows example, but same principle applies to other OS)

    worktree = C:/OldPath/OldRepoName
    

    to

    worktree = C:/NewPath/NewRepoName