为什么我收到消息“致命:此操作必须在工作树中运行?”

发布于 2024-08-05 06:58:41 字数 494 浏览 11 评论 0原文

刚刚在 Windows 上安装了 git。我将 GIT_DIR 变量设置为 c:\git\ 并验证该环境变量是由 cygwin 维护的(即 echo $GIT_DIR 是它应该的样子)。我转到我想要为其创建 git 存储库的文件夹,比如说 c:\www,然后运行:

git init
git add .

然后我收到错误:

fatal: This operation must be run in a work tree

我不确定出了什么问题,但是 c:\git 目录有一个配置文件中写着:

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

我很确定这不应该是空的,这就是我们的问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

↘人皮目录ツ 2024-08-12 06:58:41

另外,您可能位于 .git 子文件夹内,将一个文件夹上移到项目根目录。

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

挽清梦 2024-08-12 06:58:41

错误的直接原因是,是的,不可能在裸存储库中使用 git-add 。根据定义,裸存储库没有工作树。 git-add 从工作树中获取文件并将它们添加到索引中,为提交做准备。

不过,您可能需要在此处的设置中花点心思。 GIT_DIR 是用于所有 git 命令的存储库目录。您是否真的想为您跟踪的所有内容(也许是系统中的所有内容)创建一个存储库? git 存储库本质上跟踪单个目录的内容。您需要将 GIT_WORK_TREE 设置为包含您想要跟踪的所有内容的路径,然后您需要一个 .gitignore 来阻止您不感兴趣的所有内容追踪。

也许您正在尝试创建一个仅跟踪 c:\www 的存储库?然后你应该把它放在 c:\www 中(不要设置 GIT_DIR)。这是 git 的正常用法,存储库位于“模块”顶级目录的 .git 目录中。

除非你有充分的理由,否则我建议坚持使用 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!

花落人断肠 2024-08-12 06:58:41

这应该可以解决它:

git config --unset core.bare

编辑:发生这种情况是因为您的存储库是空的/新的,因此。正如您从配置中看到的那样,bare 设置为 true(空/新存储库的默认值)。您的 git-add 命令尝试将所有文​​件添加到空存储库中,但这是不可能的。因此,抛出错误消息。通过将此配置值设置为 false,您可以覆盖此行为。

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.

木落 2024-08-12 06:58:41

以防万一发生在我身上的事情也发生在其他人身上,我需要这样说:
当我收到此错误时,我正在项目中的 .git 目录中。
我搜索并寻找答案,但没有任何效果。
我所要做的就是返回到正确的目录( cd .. )
这对我来说是一个激动人心的时刻。
如果还有其他人像我一样愚蠢,我希望这个答案对您有所帮助。

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.

也只是曾经 2024-08-12 06:58:41

只需克隆另一个文件夹中的同一项目,然后将.git/文件夹复制到您的项目即可。

示例

创建临时文件夹:

mkdir temp

切换到 temp 文件夹

cd temp/

temp 文件夹中克隆同一项目:

git clone [-b branchName] git@path_to_your_git_repository

将 .git 文件夹复制到您的项目:

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

切换到您的项目并运行 git status

删除 temp 文件夹(如果完成)。

希望这会帮助某人

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

久夏青 2024-08-12 06:58:41

我遇到了这个问题,因为 .git/config 包含 worktree = D:/git-repositories/OldName。我刚刚将其更改为 worktree = D:/git-repositories/NewName

我发现了这一点,因为我使用了 git gui,显示了更详细的错误消息:

git gui 错误

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

思念满溢 2024-08-12 06:58:41

创建一个裸的 GIT 存储库

一个小抱怨:git 无法自己创建一个普通的裸存储库。确实是愚蠢的吉特。

准确地说,克隆空存储库是不可能的。所以一个空的存储库是一个无用的存储库。事实上,您通常会创建一个空存储库并立即填充它:

git init
git add .

但是,当您创建裸存储库时,git add 是不可能的:

git --bare init
git add .

给出错误“致命:此操作必须在工作树中运行”。

您也无法查看它:

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

解决方案是在其他地方创建另一个存储库,在该存储库中添加一个文件,然后将其推送到裸存储库。

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

希望这可以帮助你

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 you

倾城花音 2024-08-12 06:58:41

显式设置 GIT_DIR 环境变量会强制 git 使用给定目录作为 git 存储库。正常使用期间不需要它。

在您的示例中,因为指定了 GIT_DIR 并且它未命名为 .git (前导点很重要)并且您没有提供 -- work-tree 选项或设置 GIT_WORK_TREE 环境变量,当您说 git init 时,您需要一个裸存储库。

由于裸存储库没有工作树,因此大量命令选择对于裸存储库没有意义。 git add 只是其中之一。

是否有特殊原因需要为 git 存储库使用非标准位置,而不是工作树根下的 .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.

苦妄 2024-08-12 06:58:41

就我而言,我的存储库与“.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.

相守太难 2024-08-12 06:58:41

如果其他方法似乎不起作用,请仔细检查 git config core.worktree 中的路径。如果该路径没有指向您的工作目录,您可能需要更新它。

我收到此错误的原因是我在网络驱动器上创建了一个 Git 存储库。它在一台计算机上运行良好,但在另一台计算机上返回此错误。事实证明,我将驱动器映射到了创建它的计算机上的 Windows 驱动器号,但在另一台计算机上却没有,并且 Git 将工作树的路径保存为映射路径而不是 UNC 路径。

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.

誰認得朕 2024-08-12 06:58:41

简单的 2 行解决方案:

1- 在代码存储库上执行此命令:
git config --unset core.bare

2- 转到 git 存储库的配置文件并将其添加到 core 标签下:

bare = false
worktree = /webroot/repo [根目录的路径,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]

乖不如嘢 2024-08-12 06:58:41

如果现有(非裸)结帐开始出现此错误,请检查您的 .git/config 文件;如果 core.bare 为 true,则删除该配置行

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

明媚殇 2024-08-12 06:58:41

我的项目目录中的以下命令解决了我的问题

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

要获取项目路径,如果您使用的是 LinuxMacOspwd 命令代码>

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

安人多梦 2024-08-12 06:58:41

如果上述常用方法都无法帮助您,请查看此错误消息下方的调用跟踪(“致命:此操作...”)并找到引发实际错误的脚本和行。一旦找到该 error() 调用,请将其禁用并查看您正在尝试的操作是否完成,即使有一些警告/消息 - 暂时忽略它们。如果是这样,最后完成后可能会提到未成功完成的操作部分。现在,如果适用的话,单独解决这一部分。

将上述逻辑与我的案例联系起来,当我尝试使用 reposync 获取 Android-x86 代码时,我收到此错误消息 “致命:此操作......”。 。 .。并且调用跟踪显示 raise GitError("cannotinitialize work tree") 作为 error() 调用导致上述错误消息("fatal: . . .")。因此,在 .repo/repo/project.py 中评论 GitError() 后, reposync 。 。 . 继续并最终指示三个未正确同步的项目出现错误。我刚刚从本地 Android-x86 源代码树中的相关路径中删除了它们的 *.git 文件夹,并运行了 reposync 。 。 .再次尝到了成功的滋味!

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!

漆黑的白昼 2024-08-12 06:58:41

我遇到了同样的问题,我执行了以下步骤,

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

然后它就开始工作了。

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.

烟燃烟灭 2024-08-12 06:58:41

编辑配置文件并更改
裸=真

裸=假

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

伴我心暖 2024-08-12 06:58:41

此外,显然,如果您克隆到 NTFS Ram 驱动器,则会发生此错误。

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

调妓 2024-08-12 06:58:41

就我而言,它来自 github 方面
他们的服务器正在维护或正在更新
您可以在此链接中检查 github 服务的状态:
https://www.githubstatus.com/

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/

枫以 2024-08-12 06:58:41

对我来说,这是因为我的存储库的路径已更改(它现在位于新服务器上)。如果您运行 git config --list ,您可以看到各种设置,其中之一是 core.workingtree ,我注意到它仍然设置为旧的存储库路径,而不是新的一。将此设置更新为新路径解决了该问题。

For me it was because the path to my repo had changed (it now lives on a new server). If you run git config --list you can see various settings and one of these is core.workingtree which I noticed was still set to the old repo path and not the new one. Updating this setting to the new path fixed the issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文