125

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.

Bialecki
  • 30,061
  • 36
  • 87
  • 109

18 Answers18

263

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

blj
  • 3,254
  • 2
  • 15
  • 6
63

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!

Cascabel
  • 479,068
  • 72
  • 370
  • 318
  • 11
    1. We're just following the 'Everyday GIT in 20 commands' and git-init(1) manpPage by Linus T. (which unfortunately may be out-dated?) 2. Your post only suggests what is wrong but gives us no clue to what to do about it –  Apr 16 '10 at 13:58
  • 72
    Just `git config --unset core.bare`. – Matthias Urlichs Jan 10 '15 at 10:42
  • 1
    My issue was resolved by blj's answer but inside that subfolder, by using the `git branch` command I was able to list branches, yet I wasn't able to do `git checkout -b feature22`. Does that mean that to _list_ the branches it doesn't need a working tree, but to checkout it does? – mfaani Feb 14 '19 at 01:34
42

This should solve it:

git config --unset core.bare

EDIT: This happened because your repository is empty/new, thus bare. As you cal also see from your configuration, bare is set to true (default for empty/new repository). Your git-add command tries to add all files in an empty repository, which is not possible. So, error message is thrown. By setting this config value to false, you override this behavior.

33

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.

fcm
  • 1,247
  • 15
  • 28
Jacob Zimmerman
  • 1,521
  • 11
  • 20
  • 1
    Similar experience. In my case I was using GitExtensions (for windows), and in trying to open the repository, I'd clicked to open the .git file instead of the containing folder. Looked fine at first but gave me the tree error when I started to actually do anything. – thund Aug 30 '17 at 22:59
  • 1
    I love that this has a dozen upvotes. I'm glad that I posted this answer, even though it seemed kind of obvious. – Jacob Zimmerman Aug 14 '18 at 03:31
  • "This operation must be run in a work tree" error message could be elaborated differently. Should not it be something like :this operation must be run right above the .git directory which is called "the work tree". "In a" sounds somehow wrong phrasing. A normal tree has lots of branches and depth and this has different meaning. Millions of people bang their heads to this when they first start to work with git. So the error message could give educational description right away what is "the work tree". Great you dared to bring the matter to daylight. – Tonecops Jun 06 '23 at 11:03
12

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

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
  • 1
    I found this useful, but I have to say that the first line was enough to make me realise what was wrong. The rest of it actually confused me a little bit. Thank a lot anyway :) – randombee Apr 27 '16 at 13:09
  • @randombee This worked for me . . . but why? What does this do? – Yatharth Agarwal Oct 01 '20 at 13:59
10

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

koppor
  • 19,079
  • 15
  • 119
  • 161
8

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

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
user1329261
  • 111
  • 1
  • 5
  • 1
    This is simpy untrue. You can create a bare repository and clone it in. Even if it was true it still doesn't address the question. (E.g. `$ git --bare init bare.git` `Initialized empty Git repository in /home18/cbailey/gittest8/bare.git/` `$ git clone bare.git non-bare` `Cloning into 'non-bare'... \\ done. \\ warning: You appear to have cloned an empty repository.` – CB Bailey Jul 09 '12 at 08:04
  • I am sorry ,if i use "<>"to package the "url or path of bare repository",it will be invisiable. and what i said upside is really usefull.I have test and verify meself. – user1329261 Jul 12 '12 at 02:57
8

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.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • Setting GIT_WORK_TREE solved it for me. Reason I'm using GIT_DIR is that I want a separate git repo for my working copy where I have a background task that just adds/commits every few minutes. This process sets GIT_DIR to .gitsave. My actual repo work is in .git. Now I have a local live tracking repo so I have versions of all files as I change them. – MikeJansen Aug 30 '19 at 15:06
5

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.

chethan jain
  • 91
  • 1
  • 3
3

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.

Soren Bjornstad
  • 1,292
  • 1
  • 14
  • 25
3

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]

1

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

ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
1

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

Gedeon Mutshipayi
  • 2,871
  • 3
  • 21
  • 42
0

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!

geekie12
  • 11
  • 12
0

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.

0

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

0

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

Mendi Barel
  • 3,350
  • 1
  • 23
  • 24
0

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/

mahdi komaiha
  • 61
  • 1
  • 5