Why am I getting the message, "fatal: This operation must be run in a work tree?"

Viewed 237617

Just installed git on Windows. I set the GIT_DIR variable to be c:\git\ and verified that this environment variable is maintained by cygwin (i.e. echo $GIT_DIR is what it should be). I went to the folder that I wanted to create the git repository for, let's say c:\www, and then ran:

git init
git add .

Then I get the error:

fatal: This operation must be run in a work tree

I'm not sure what went wrong, but the c:\git directory has a config file that says:

[core]
    repositoryformatversion = 0
    filemode = false
    bare = true
    symlinks = false
    ignorecase = true

I'm pretty sure this shouldn't be bare and that's our problem.

18 Answers

This should solve it:

git config --unset core.bare

In my case, I was in the same folder as ".git" file for my repo. I had to go one directory level up, it solved it.

If nothing else seems to work, double-check the path in git config core.worktree. If that path doesn't point to your working directory, you may need to update it.

The way I got this error was that I created a Git repository on a network drive. It worked fine on one computer but returned this error on another. It turned out that I had the drive mapped to a Windows drive letter on the computer where I created it, but not on the other computer, and Git saved the path to the work tree as the mapped path and not the UNC path.

Simple 2 liner solution:

1- Execute this command on your code repo: git config --unset core.bare

2- Go to config file of your git repo and add this under core tag:

bare = false worktree = /webroot/repo [Path to your root directory,one step above the git folder]

If an existing (non-bare) checkout begins giving this error, check your .git/config file; if core.bare is true, remove that config line

The following command in my project directory fixed my issue

git --work-tree=/path/to/work/tree

To get your project path, enter the pwd command in your project directory if you are using Linux or MacOs

Same issue i got, i did following steps,

  1. git init
  2. git add .
  3. git commit -m"inital setup"
  4. git push -f origin master

then it work starts working.

Edited the config file and changed bare = true to bare = false

In addition, apparently, this error will happen if you clone into NTFS Ram Drive.

In my case it was from github's side their servers were under maintenance or having an update you can check the status of github services in this link: https://www.githubstatus.com/

Related