git 为什么我会收到消息“致命:此操作必须在工作树中运行?”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1456923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting the message, "fatal: This operation must be run in a work tree?"
提问by Bialecki
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:
刚刚在 Windows 上安装了 git。我将 GIT_DIR 变量设置为 c:\git\ 并验证该环境变量是否由 cygwin 维护(即 echo $GIT_DIR 应该是这样)。我去了我想为其创建 git 存储库的文件夹,比如说 c:\www,然后运行:
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:
我不确定出了什么问题,但是 c:\git 目录有一个配置文件,上面写着:
[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.
我很确定这不应该是裸露的,那是我们的问题。
采纳答案by Cascabel
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.
错误的直接原因是,是的,无法git-add
与裸存储库一起使用。根据定义,裸存储库没有工作树。git-add
从工作树中获取文件并将它们添加到索引中,为提交做准备。
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.
不过,您可能需要在此处考虑一下您的设置。GIT_DIR 是用于所有 git 命令的存储库目录。你真的想为你跟踪的所有东西创建一个单一的存储库,也许是你系统中的所有东西?git 存储库本质上跟踪单个目录的内容。您需要设置GIT_WORK_TREE
一个包含您想要跟踪的所有内容的路径,然后您需要一个.gitignore
来阻止您不想跟踪的所有内容。
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".
也许您正在尝试创建一个只会跟踪的存储库c:\www
?然后你应该把它放进去c:\www
(不要设置 GIT_DIR)。这是git的正常用法,仓库在你的“模块”顶级目录的.git目录下。
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!
除非你有很好的理由,否则我建议坚持使用 git 喜欢的工作方式。如果您有几件事情要跟踪,您可能需要多个存储库!
回答by blj
Also, you are probably inside the .git subfolder, move up one folder to your project root.
此外,您可能在 .git 子文件夹中,向上移动一个文件夹到您的项目根目录。
回答by Jacob Zimmerman
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.
以防万一发生在我身上的事情发生在其他人身上,我需要这样说:当我收到这个错误时,我在我的项目中的 .git 目录中。我搜索并寻找答案,但没有任何效果。我所要做的就是回到正确的目录。对我来说,这有点像面对面的时刻。如果有其他人像我一样愚蠢,我希望你发现这个答案有帮助。
回答by Festus Tamakloe
Just clonethe same project in another folder and copythe .git/folder to your project.
只是克隆在另一个文件夹相同的项目和复制的git的/文件夹到您的项目。
Example
例子
Create temp folder:
创建临时文件夹:
mkdir temp
switch to tempfolder
切换到临时文件夹
cd temp/
clone the same project in the tempfolder:
在临时文件夹中克隆同一个项目:
git clone [-b branchName] git@path_to_your_git_repository
copy .git folder to your projet:
将 .git 文件夹复制到您的项目:
cp -R .git/ path/to/your/project/
cp -R .git/ path/to/your/project/
switch to your project and run git status
切换到您的项目并运行 git status
deletethe tempfolder if your are finished.
删除的临时文件夹,如果你完成。
hope this will help someone
希望这会帮助某人
回答by Babajide M. Moibi
This should solve it:
这应该解决它:
git config --unset core.bare
回答by CB Bailey
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.
显式设置GIT_DIR
环境变量会强制 git 使用给定目录作为 git 存储库。在正常使用期间从不需要它。
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
.
在您的示例中,因为已经指定了 aGIT_DIR
并且它没有命名.git
(前导点很重要)并且您没有提供--work-tree
选项或设置GIT_WORK_TREE
环境变量,所以当您说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.
因为裸存储库没有工作树,所以大量命令选择对于裸存储库没有意义。git add
只是其中之一。
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.
是否有特殊原因需要为 git 存储库使用非标准位置,而不是在.git
工作树根目录下的子文件夹中?虽然可以安排它,但往往需要更多的工作,并且更容易出现用户错误。
回答by user1329261
Create a bare GIT repository
创建一个裸 GIT 存储库
A small rant: git is unable to create a normal bare repository by itself. Stupid git indeed.
一个小小的抱怨:git 无法自己创建一个普通的裸仓库。真是愚蠢的混蛋。
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 add 是不可能的:
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
希望这可以帮助你
回答by chethan jain
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.
就我而言,我与我的 repo 的“.git”文件位于同一文件夹中。我不得不向上一级目录,它解决了它。
回答by koppor
I had this issue, because .git/config
contained worktree = D:/git-repositories/OldName
. I just changed it into worktree = D:/git-repositories/NewName
我有这个问题,因为.git/config
包含worktree = D:/git-repositories/OldName
. 我只是把它改成worktree = D:/git-repositories/NewName
I discovered that, because I used git gui, which showed a more detailed error message:
我发现,因为我使用了git gui,它显示了更详细的错误消息:
回答by ThorSummoner
If an existing (non-bare) checkout begins giving this error, check your .git/config file; if core.bare
is true, remove that config line
如果现有(非裸)结帐开始出现此错误,请检查您的 .git/config 文件;如果core.bare
为真,请删除该配置行