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

前端 未结 15 755
野趣味
野趣味 2020-11-27 12:33

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

相关标签:
15条回答
  • 2020-11-27 13:08

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

    0 讨论(0)
  • 2020-11-27 13:09

    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. 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.

    0 讨论(0)
  • 2020-11-27 13:11

    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

    0 讨论(0)
  • 2020-11-27 13:13

    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.

    0 讨论(0)
  • 2020-11-27 13:15

    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!

    0 讨论(0)
  • 2020-11-27 13:15

    This should solve it:

    git config --unset core.bare
    
    0 讨论(0)
提交回复
热议问题