Git 中的错误-致命:'/Users/username/Downloads/folder_name' 在存储库之外

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19908216/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 09:20:41  来源:igfitidea点击:

Error in Git- fatal: '/Users/username/Downloads/folder_name' is outside repository

git

提问by Dinesh Kumar

When I am adding a new project to git, I am getting this error.

当我向 git 添加新项目时,出现此错误。

Error - fatal: '/Users/username/Downloads/folder_name' is outside repository

How to get ride of this error? I think, I need to give the current working directory. But, don't know how to do that..Can anyone help me out of this? But when I add new file directly, it works fine.. Your help is highly appreciated..Thanks in advance.

如何摆脱这个错误?我想,我需要给出当前的工作目录。但是,不知道该怎么做..谁能帮我解决这个问题?但是当我直接添加新文件时,它工作正常..非常感谢您的帮助..提前致谢。

回答by kostix

Git is not able to track files outside of the repository, and so when you're doing

Git 无法跟踪存储库外的文件,因此当您执行此操作时

git add /Users/dineshkumar/Documents/sampleios

Git tells you just that, literally: the path you're trying to add lies outside of the initialized repository you're in.

从字面上看,Git 告诉您:您尝试添加的路径位于您所在的初始化存储库之外。

The term repository is a bit moot so let's make it more clear.

术语存储库有点没有实际意义,所以让我们更清楚地说明一下。

Usually you work with Git like this:

通常你像这样使用 Git:

$ mkdir project; cd project
$ git init
$ vim upload.php
...
$ git add upload.php
...
$ git commit

When you run git initin a directory ("project" in our case) Git creates a special subdirectory named ".git" in it — which contains the actual repository data (objects and configuration) — and then turns the enclosing directory into a work tree.The work tree contains the files which you actually edit and submit to Git to build up the next commit.

当你git init在一个目录(在我们的例子中是“project”)中运行时,Git 会在其中创建一个名为“.git”的特殊子目录——它包含实际的存储库数据(对象和配置)——然后将封闭目录变成一个工作树。工作树包含您实际编辑并提交给 Git 以构建下一次提交的文件。

The crucial propety of this design is that only files located in the work tree are managed by Git. You can't git adda file from somewhere else — only from within the work tree. Consequently, no one calls git addwith full paths because this has no sense.

这种设计的关键特性是只有位于工作树中的文件由 Git 管理。您不能git add从其他地方获取文件——只能从工作树中获取。因此,没有人git add使用完整路径调用,因为这毫无意义。

I'm not sure where your confusion is rooted. If you think that the git addcommand is supposed to copya file/directory from somewhere into the work tree, you're wrong. If you were't aware of the work tree and thought that the repository is able to track arbitrary files on a filesystem tree, you're wrong again — how would cloning work if this was true?

我不确定您的困惑源于何处。如果您认为该git add命令应该文件/目录从某处复制到工作树中,那您就错了。如果您不知道工作树并认为存储库能够跟踪文件系统树上的任意文件,那么您又错了——如果这是真的,克隆将如何工作?

You should make yourself familiar with the idea that git addis not for adding filesto the repository, it's rather for adding changesin files into the so-called staging area, from which the next commit is made. Adding a presently untracked file is just a special case of this — this addition is just a whole-file "change".

您应该让自己熟悉这个想法,这git add不是为了将文件添加到存储库,而是文件中的更改添加到所谓的暂存区,从中进行下一次提交。添加当前未跟踪的文件只是一个特例——这个添加只是整个文件的“更改”。

Looking at your attempts I might make a guess that you want to turn /Users/dineshkumar/Documents/sampleiosinto a Git repository. If this is the case, do

看着你的尝试,我可能会猜测你想/Users/dineshkumar/Documents/sampleios变成一个 Git 存储库。如果是这种情况,请执行

$ cd /Users/dineshkumar/Documents/sampleios
$ git init
$ git add .
$ git commit

But before you do that pleasestart with the bookas you seem to lack the most basic Git knowledge.

但在你这样做之前,请先从这本书开始,因为你似乎缺乏最基本的 Git 知识。

回答by uml?ute

assuming your directory /Users/username/Downloads/folder_name/is within a git-repository (e.g. you are tracking /Users/username/Downloads/), simply do:

假设您的目录/Users/username/Downloads/folder_name/在 git-repository 中(例如您正在跟踪/Users/username/Downloads/),只需执行以下操作:

cd /Users/username/Downloads/
git add folder_name/
git commit

NOTE: i would not recommend tracking the Downloads/folder, as this is volatile by nature. instead it might make more sense to track Projects/

注意:我不建议跟踪Downloads/文件夹,因为它本质上是易变的。相反,跟踪可能更有意义Projects/

a git workbench repository is a directory that contains a .gitfolder. to add anything to the repository, it has to be within a repository (that is: in a subdirectory of the repository): you cannot track a folder/file that is outside of a git-repository.

git workbench 存储库是一个包含.git文件夹的目录。要将任何内容添加到存储库,它必须在存储库内(即:在存储库的子目录中):您无法跟踪 git-repository 之外的文件夹/文件