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

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

Also, you are probably inside the .git subfolder, move up one folder to your project root.

Solution 2 - Git

The direct reason for the error is that yes, it's impossible to use git-add with a bare repository. A bare repository, by definition, has no work tree. git-add takes files from the work tree and adds them to the index, in preparation for committing.

You may need to put a bit of thought into your setup here, though. GIT_DIR is the repository directory used for all git commands. Are you really trying to create a single repository for everything you track, maybe things all over your system? A git repository by nature tracks the contents of a single directory. You'll need to set GIT_WORK_TREE to a path containing everything you want to track, and then you'll need a .gitignore to block out everything you're not interested in tracking.

Maybe you're trying to create a repository which will track just c:\www? Then you should put it in c:\www (don't set GIT_DIR). This is the normal usage of git, with the repository in the .git directory of the top-level directory of your "module".

Unless you have a really good reason, I'd recommend sticking with the way git likes to work. If you have several things to track, you probably want several repositories!

Solution 3 - Git

Just in case what happened to me is happening to somebody else, I need to say this:
I was in my .git directory within my project when I was getting this error.
I searched and scoured for answers, but nothing worked.
All I had to do was get back to the right directory ( cd .. ).
It was kind of a face-palm moment for me.
In case there's anyone else out there as silly as me, I hope you found this answer helpful.

Solution 4 - Git

This should solve it:

git config --unset core.bare

Solution 5 - Git

Just clone the same project in another folder and copy the .git/ folder to your project.

Example

Create temp folder:

mkdir temp

switch to temp folder

cd temp/

clone the same project in the temp folder:

git clone [-b branchName] git@path_to_your_git_repository

copy .git folder to your projet:

cp -R .git/ path/to/your/project/

switch to your project and run git status

delete the temp folder if your are finished.

hope this will help someone

Solution 6 - Git

Explicitly setting the GIT_DIR environment variable forces git to use the given directory as the git repository. It is never needed during normal use.

In your example, because have specified a GIT_DIR and it isn't named .git (the leading dot is important) and you haven't provided a --work-tree option or set the GIT_WORK_TREE environment variable, that you want a bare repository when you said git init.

Because a bare repository has no working tree a large selection of commands don't make sense with a bare repository. git add is just one.

Is there a particular reason that you need to use a non-standard location for your git repository, rather than in a .git subfolder under the working tree root? While it's possible to arrange this it tends to be more work and more liable to user mistakes.

Solution 7 - Git

I had this issue, because .git/config contained worktree = D:/git-repositories/OldName. I just changed it into worktree = D:/git-repositories/NewName

I discovered that, because I used git gui, which showed a more detailed error message:

git gui error

Solution 8 - Git

Create a bare GIT repository

A small rant: git is unable to create a normal bare repository by itself. Stupid git indeed.

To be precise, it is not possible to clone empty repositories. So an empty repository is a useless repository. Indeed, you normally create an empty repository and immediately fill it:

git init
git add .

However, git add is not possible when you create a bare repository:

git --bare init
git add .

gives an error "fatal: This operation must be run in a work tree".

You can't check it out either:

Initialized empty Git repository in /home/user/myrepos/.git/
fatal: http://repository.example.org/projects/myrepos.git/info/refs not found: did you run git update-server-info on the server?

git --bare init
git update-server-info # this creates the info/refs file
chown -R <user>:<group> . # make sure others can update the repository

The solution is to create another repository elsewhere, add a file in that repository and, push it to the bare repository.

mkdir temp; cd temp
git init
touch .gitignore
git add .gitignore
git commit -m "Initial commit"
git push (url or path of bare repository) master
cd ..; rm -rf temp

hope this can help u

Solution 9 - Git

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.

Solution 10 - Git

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.

Solution 11 - Git

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]

Solution 12 - Git

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

Solution 13 - Git

If none of the above usual ways help you, look at the call trace underneath this error message ("fatal: This operation . . .") and locate the script and line which is raising the actual error. Once you locate that error() call, disable it and see if the operation you are trying completes even with some warnings/messages - ignore them for now. If so, finally after completing it might mention the part of the operation that was not completed successfully. Now, address this part separately as applicable.

Relating above logic to my case, I was getting this error message "fatal: This operation . . ." when I was trying to get the Android-x86 code with repo sync . . .. and the call trace showed raise GitError("cannot initialize work tree") as the error() call causing the above error message ("fatal: . . ."). So, after commenting that GitError() in .repo/repo/project.py, repo sync . . . continued and finally indicated error for three projects that were not properly synced. I just deleted their *.git folders from their relevant paths in the Android-x86 source tree locally and ran repo sync . . . again and tasted success!

Solution 14 - Git

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.

Solution 15 - Git

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

Solution 16 - Git

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

Solution 17 - Git

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/

Solution 18 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBialeckiView Question on Stackoverflow
Solution 1 - GitbljView Answer on Stackoverflow
Solution 2 - GitCascabelView Answer on Stackoverflow
Solution 3 - GitJacob ZimmermanView Answer on Stackoverflow
Solution 4 - GitBabajide M. MoibiView Answer on Stackoverflow
Solution 5 - GitFestus TamakloeView Answer on Stackoverflow
Solution 6 - GitCB BaileyView Answer on Stackoverflow
Solution 7 - GitkopporView Answer on Stackoverflow
Solution 8 - Gituser1329261View Answer on Stackoverflow
Solution 9 - Gitchethan jainView Answer on Stackoverflow
Solution 10 - GitSoren BjornstadView Answer on Stackoverflow
Solution 11 - Gitxikandar hayyatView Answer on Stackoverflow
Solution 12 - GitThorSummonerView Answer on Stackoverflow
Solution 13 - Gitgeekie12View Answer on Stackoverflow
Solution 14 - GitraghavendraView Answer on Stackoverflow
Solution 15 - GitMurt MoriartyView Answer on Stackoverflow
Solution 16 - GitMendi BarelView Answer on Stackoverflow
Solution 17 - Gitmahdi komaihaView Answer on Stackoverflow
Solution 18 - GitGed FlodView Answer on Stackoverflow