2

I've got many Git repositories cloned via Sourcetree on Windows. One particular repository works properly (I can commit, push, and pull, etc.) except that its Git directory is completely bare and gives me errors about git status not working. All my other (many) repositories are working fully.

The error message in SourceTree is:

'git status' failed with error code 128: fatal: this operation must be run in a work tree
(C:\{full path to directory})

SourceTree: 'git status' failed with error code 128: fatal: this operation must be run in a work tree

The error message from running git status in the same directory (which is completely empty) is:

C:\{full path to directory}>git status
fatal: this operation must be run in a work tree

How do I find out where Sourcetree is referencing this particular Git repository so I can move it to the correct location or otherwise fix the git status error?

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69

2 Answers2

0

It depends on the exact error git status is giving you, but you should:

  • check in command line (not SourceTree)
  • see if there is a .gitmodules which could declare that repo as a submodule.

The OP adds the error:

 this operation must be run in a work tree

This is typical of a bare repository, one where there is no files checked out, but only the Git repo internal folders (refs/, objects/, ...).

If that is the case, the error is expected:

  • Clone that repository again, this time without the --bare option.
  • Or convert the local cloned bare repo to a non-bare repository.

Specifically, this answer which includes:

git config --local --bool core.bare false
git config --local remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch origin
git branch -u origin/master master

# reset the index (not the working tree)
git reset HEAD -- .
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • The command line actually gives less information than SourceTree: `fatal: this operation must be run in a work tree` vs. `'git status' failed with error code 128: fatal: this operation must be run in a work tree` then the full directory path. – CJ Dennis Sep 20 '19 at 05:51
  • @CJDennis less information? Or the *same* information? Anyway, are you in an actual bare repository, one where there is no files checked out, but only the Git repo internal folders (refs/, objects/, ...): if so, the error is expected. Clone that repository again, this time without the --bare option. Or convert it to a non-bare repo: https://stackoverflow.com/a/10637882/6309 – VonC Sep 20 '19 at 06:02
  • I didn't see `'git status' failed with error code 128` in the command line. – CJ Dennis Sep 20 '19 at 06:09
  • @CJDennis You are right, you don't see it right away: You would need to type `echo $?` just after the `git status` returned output. The point of my original answer was that "gives me errors about `git status` not working" was a bit short for me to guess the root cause. Thank you for having included the exact error message. I have edited the answer with a possible root cause. – VonC Sep 20 '19 at 06:15
  • Is that a Linux thing? It prints `$?` on Windows. – CJ Dennis Sep 20 '19 at 06:17
  • on Windows: `echo %ERRORLEVEL%` (just after `git status` returns) – VonC Sep 20 '19 at 06:24
0

Somehow, this repository got checked out as a bare repository. In case you're wondering, this is not a normal thing!

At the directory, run:

git config --local --bool core.bare false
git reset

I actually edited the Git config file and manually changed true to false, then ran the second command (git reset), but the above commands should have the same effect.

Afterwards, all the files on the current branch should be visible.

CJ Dennis
  • 4,226
  • 2
  • 40
  • 69
  • Thanks @VonC! I can't vote on my own answer, but I can accept it in two days' time. Your answer pointed me in the right direction but didn't solve the problem. It's more of a hint than an answer. – CJ Dennis Sep 20 '19 at 06:33
  • I have edited my answer to include the missing commands (which were included in the link I mentioned before): that will make it a full answer, that you can accept. – VonC Sep 20 '19 at 06:54