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

前端 未结 15 757
野趣味
野趣味 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:15

    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!

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

    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.

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

    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:

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

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

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

    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

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

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

    0 讨论(0)
提交回复
热议问题